diff --git a/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java b/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java new file mode 100644 index 0000000000..c740303204 --- /dev/null +++ b/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javolution.util.FastMap; + +public class UtilGenerics { + + public static final String module = UtilMisc.class.getName(); + + private static > C checkCollectionCast(Object object, Class clz) { + return clz.cast(object); + } + + public static > void checkCollectionContainment(Object object, Class clz, Class type) { + if (object != null) { + if (!(clz.isInstance(object))) throw new ClassCastException("Not a " + clz.getName()); + int i = 0; + for (Object value: (Collection) object) { + if (!type.isInstance(value)) { + throw new IllegalArgumentException("Value(" + i + "), with value(" + value + ") is not a " + type.getName()); + } + i++; + } + } + } + + public static Collection checkCollection(Object object) { + return (Collection) checkCollectionCast(object, Collection.class); + } + + public static Collection checkCollection(Object object, Class type) { + checkCollectionContainment(object, Collection.class, type); + return checkCollection(object); + } + + public static List checkList(Object object) { + return (List) checkCollectionCast(object, List.class); + } + + public static List checkList(Object object, Class type) { + checkCollectionContainment(object, List.class, type); + return checkList(object); + } + + public static Map checkMap(Object object) { + if (object != null && !(object instanceof Map)) throw new ClassCastException("Not a map"); + return (Map) object; + } + + public static Map checkMap(Object object, Class keyType, Class valueType) { + if (object != null) { + if (!(object instanceof Map)) throw new ClassCastException("Not a map"); + Map map = (Map) object; + int i = 0; + for (Map.Entry entry: map.entrySet()) { + if (!keyType.isInstance(entry.getKey())) { + throw new IllegalArgumentException("Key(" + i + "), with value(" + entry.getKey() + ") is not a " + keyType); + } + if (!valueType.isInstance(entry.getValue())) { + throw new IllegalArgumentException("Value(" + i + "), with value(" + entry.getValue() + ") is not a " + valueType); + } + i++; + } + } + return checkMap(object); + } + + public static Set checkSet(Object object) { + return (Set) checkCollectionCast(object, Set.class); + } + + public static Set checkSet(Object object, Class type) { + checkCollectionContainment(object, Set.class, type); + return checkSet(object); + } + + public static Map toMap(Class keyType, Class valueType, Object... data) { + if (data == null) { + return null; + } + if (data.length % 2 == 1) { + throw new IllegalArgumentException("You must pass an even sized array to the toMap method"); + } + Map map = FastMap.newInstance(); + for (int i = 0; i < data.length; ) { + Object key = data[i]; + if (!(keyType.isInstance(key))) throw new IllegalArgumentException("Key(" + i + ") is not a " + keyType.getName() + ", was(" + key.getClass().getName() + ")"); + i++; + Object value = data[i]; + if (!(valueType.isInstance(value))) throw new IllegalArgumentException("Value(" + i + ") is not a " + keyType.getName() + ", was(" + key.getClass().getName() + ")"); + i++; + map.put(keyType.cast(key), valueType.cast(value)); + } + return map; + } + + public static Map toMap(Class keyType, Object... data) { + if (data == null) { + return null; + } + if (data.length % 2 == 1) { + throw new IllegalArgumentException("You must pass an even sized array to the toMap method"); + } + Map map = FastMap.newInstance(); + for (int i = 0; i < data.length; ) { + Object key = data[i]; + if (!(keyType.isInstance(key))) throw new IllegalArgumentException("Key(" + i + ") is not a " + keyType.getName() + ", was(" + key.getClass().getName() + ")"); + i++; + Object value = data[i]; + map.put(keyType.cast(key), value); + } + return map; + } +}