svn commit: r585690 - /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r585690 - /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java

doogie-3
Author: doogie
Date: Wed Oct 17 13:51:21 2007
New Revision: 585690

URL: http://svn.apache.org/viewvc?rev=585690&view=rev
Log:
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.

Added:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java

Added: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java?rev=585690&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java (added)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilGenerics.java Wed Oct 17 13:51:21 2007
@@ -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 extends Collection<?>> C checkCollectionCast(Object object, Class<C> clz) {
+        return clz.cast(object);
+    }
+
+    public static <C extends Collection<?>> void checkCollectionContainment(Object object, Class<C> 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 <T> Collection<T> checkCollection(Object object) {
+        return (Collection<T>) checkCollectionCast(object, Collection.class);
+    }
+
+    public static <T> Collection<T> checkCollection(Object object, Class<T> type) {
+        checkCollectionContainment(object, Collection.class, type);
+        return checkCollection(object);
+    }
+
+    public static <T> List<T> checkList(Object object) {
+        return (List<T>) checkCollectionCast(object, List.class);
+    }
+
+    public static <T> List<T> checkList(Object object, Class<T> type) {
+        checkCollectionContainment(object, List.class, type);
+        return checkList(object);
+    }
+
+    public static <K, V> Map<K, V> checkMap(Object object) {
+        if (object != null && !(object instanceof Map)) throw new ClassCastException("Not a map");
+        return (Map<K, V>) object;
+    }
+
+    public static <K, V> Map<K, V> checkMap(Object object, Class<K> keyType, Class<V> 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 <T> Set<T> checkSet(Object object) {
+        return (Set<T>) checkCollectionCast(object, Set.class);
+    }
+
+    public static <T> Set<T> checkSet(Object object, Class<T> type) {
+        checkCollectionContainment(object, Set.class, type);
+        return checkSet(object);
+    }
+
+    public static <K, V> Map<K, V> toMap(Class<K> keyType, Class<V> 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<K, V> 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 <K, Object> Map<K, Object> toMap(Class<K> 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<K, Object> 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;
+    }
+}