From dac2c1bbc9d08b25deb33a83e77efcd8a3d15369 Mon Sep 17 00:00:00 2001 From: Adam Heath Date: Wed, 17 Oct 2007 20:51:21 +0000 Subject: [PATCH] The java compiler in 1.5 and 1.6 is not as smart as it should be, when it comes to inferring types for generics. This class has several helper methods, so that unnescessary warnings are only issued for this file, and not every other file in the respository. Once javac becomes smarter, than this class can be removed. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@585690 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/ofbiz/base/util/UtilGenerics.java | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java 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; + } +}