You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2013/04/06 16:44:35 UTC
svn commit: r1465253 -
/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilMisc.java
Author: adrianc
Date: Sat Apr 6 14:44:34 2013
New Revision: 1465253
URL: http://svn.apache.org/r1465253
Log:
Removed Javolution from UtilMisc.java. Also removed the SimpleMap class - which provided no benefit because it was converted to a "real Map" 99.8% of the time.
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilMisc.java
Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilMisc.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilMisc.java?rev=1465253&r1=1465252&r2=1465253&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilMisc.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilMisc.java Sat Apr 6 14:44:34 2013
@@ -26,8 +26,11 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@@ -37,10 +40,6 @@ import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
-import javolution.util.FastList;
-import javolution.util.FastMap;
-import javolution.util.FastSet;
-
import org.ofbiz.base.util.collections.MapComparator;
/**
@@ -101,11 +100,7 @@ public class UtilMisc {
* @return The resulting Map
*/
public static <V, V1 extends V> Map<String, V> toMap(String name1, V1 value1) {
- return new UtilMisc.SimpleMap<V>(name1, value1);
-
- /* Map fields = FastMap.newInstance();
- fields.put(name1, value1);
- return fields;*/
+ return populateMap(new HashMap<String, V>(), name1, value1);
}
/**
@@ -113,12 +108,7 @@ public class UtilMisc {
* @return The resulting Map
*/
public static <V, V1 extends V, V2 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2) {
- return new UtilMisc.SimpleMap<V>(name1, value1, name2, value2);
-
- /* Map fields = FastMap.newInstance();
- fields.put(name1, value1);
- fields.put(name2, value2);
- return fields;*/
+ return populateMap(new HashMap<String, V>(), name1, value1, name2, value2);
}
/**
@@ -126,13 +116,7 @@ public class UtilMisc {
* @return The resulting Map
*/
public static <V, V1 extends V, V2 extends V, V3 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2, String name3, V3 value3) {
- return new UtilMisc.SimpleMap<V>(name1, value1, name2, value2, name3, value3);
-
- /* Map fields = FastMap.newInstance();
- fields.put(name1, value1);
- fields.put(name2, value2);
- fields.put(name3, value3);
- return fields;*/
+ return populateMap(new HashMap<String, V>(), name1, value1, name2, value2, name3, value3);
}
/**
@@ -140,14 +124,7 @@ public class UtilMisc {
* @return The resulting Map
*/
public static <V, V1 extends V, V2 extends V, V3 extends V, V4 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2, String name3, V3 value3, String name4, V4 value4) {
- return new UtilMisc.SimpleMap<V>(name1, value1, name2, value2, name3, value3, name4, value4);
-
- /* Map fields = FastMap.newInstance();
- fields.put(name1, value1);
- fields.put(name2, value2);
- fields.put(name3, value3);
- fields.put(name4, value4);
- return fields;*/
+ return populateMap(new HashMap<String, V>(), name1, value1, name2, value2, name3, value3, name4, value4);
}
/**
@@ -155,14 +132,7 @@ public class UtilMisc {
* @return The resulting Map
*/
public static <V, V1 extends V, V2 extends V, V3 extends V, V4 extends V, V5 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2, String name3, V3 value3, String name4, V4 value4, String name5, V5 value5) {
- Map<String, V> fields = FastMap.newInstance();
-
- fields.put(name1, value1);
- fields.put(name2, value2);
- fields.put(name3, value3);
- fields.put(name4, value4);
- fields.put(name5, value5);
- return fields;
+ return populateMap(new HashMap<String, V>(), name1, value1, name2, value2, name3, value3, name4, value4, name5, value5);
}
/**
@@ -170,15 +140,7 @@ public class UtilMisc {
* @return The resulting Map
*/
public static <V, V1 extends V, V2 extends V, V3 extends V, V4 extends V, V5 extends V, V6 extends V> Map<String, V> toMap(String name1, V1 value1, String name2, V2 value2, String name3, V3 value3, String name4, V4 value4, String name5, V5 value5, String name6, V6 value6) {
- Map<String, V> fields = FastMap.newInstance();
-
- fields.put(name1, value1);
- fields.put(name2, value2);
- fields.put(name3, value3);
- fields.put(name4, value4);
- fields.put(name5, value5);
- fields.put(name6, value6);
- return fields;
+ return populateMap(new HashMap<String, V>(), name1, value1, name2, value2, name3, value3, name4, value4, name5, value5, name6, value6);
}
/**
@@ -187,17 +149,25 @@ public class UtilMisc {
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<String, V> toMap(Object... data) {
- if (data == null) {
- return null;
- }
if (data.length == 1 && data[0] instanceof Map) {
- // Fix for javac's broken type inferring
+ // Logging a warning here because a lot of code misuses this method and that code needs to be fixed.
+ //Debug.logWarning("UtilMisc.toMap called with a Map. Use UtilGenerics.checkMap instead.", module);
return UtilGenerics.<String, V>checkMap(data[0]);
}
if (data.length % 2 == 1) {
- throw new IllegalArgumentException("You must pass an even sized array to the toMap method");
+ IllegalArgumentException e = new IllegalArgumentException("You must pass an even sized array to the toMap method (size = " + data.length + ")");
+ Debug.logInfo(e, module);
+ throw e;
+ }
+ Map<String, V> map = new HashMap();
+ for (int i = 0; i < data.length;) {
+ map.put((String) data[i++], (V) data[i++]);
}
- Map<String, V> map = FastMap.newInstance();
+ return map;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <K, V> Map<String, V> populateMap(Map<String, V> map, Object... data) {
for (int i = 0; i < data.length;) {
map.put((String) data[i++], (V) data[i++]);
}
@@ -216,19 +186,22 @@ public class UtilMisc {
}
public static <T> List<T> makeListWritable(Collection<? extends T> col) {
- List<T> result = FastList.newInstance();
+ List<T> result = new LinkedList<T>();
if (col != null) result.addAll(col);
return result;
}
public static <K, V> Map<K, V> makeMapWritable(Map<K, ? extends V> map) {
- Map<K, V> result = FastMap.newInstance();
- if (map != null) result.putAll(map);
+ if (map == null) {
+ return new HashMap<K, V>();
+ }
+ Map<K, V> result = new HashMap<K, V>(map.size());
+ result.putAll(map);
return result;
}
public static <T> Set<T> makeSetWritable(Collection<? extends T> col) {
- Set<T> result = FastSet.newInstance();
+ Set<T> result = new HashSet<T>();
if (col != null) result.addAll(col);
return result;
}
@@ -241,7 +214,7 @@ public class UtilMisc {
*/
public static <V> void makeMapSerializable(Map<String, V> map) {
// now filter out all non-serializable values
- Set<String> keysToRemove = FastSet.newInstance();
+ Set<String> keysToRemove = new HashSet<String>();
for (Map.Entry<String, V> mapEntry: map.entrySet()) {
Object entryValue = mapEntry.getValue();
if (entryValue != null && !(entryValue instanceof Serializable)) {
@@ -268,7 +241,7 @@ public class UtilMisc {
public static List<Map<Object, Object>> sortMaps(List<Map<Object, Object>> listOfMaps, List<? extends String> sortKeys) {
if (listOfMaps == null || sortKeys == null)
return null;
- List<Map<Object, Object>> toSort = FastList.newInstance();
+ List<Map<Object, Object>> toSort = new ArrayList<Map<Object, Object>>(listOfMaps.size());
toSort.addAll(listOfMaps);
try {
MapComparator mc = new MapComparator(sortKeys);
@@ -286,7 +259,7 @@ public class UtilMisc {
public static <K, IK, V> Map<IK, V> getMapFromMap(Map<K, Object> outerMap, K key) {
Map<IK, V> innerMap = UtilGenerics.<IK, V>checkMap(outerMap.get(key));
if (innerMap == null) {
- innerMap = FastMap.newInstance();
+ innerMap = new HashMap<IK, V>();
outerMap.put(key, innerMap);
}
return innerMap;
@@ -298,7 +271,7 @@ public class UtilMisc {
public static <K, V> List<V> getListFromMap(Map<K, Object> outerMap, K key) {
List<V> innerList = UtilGenerics.<V>checkList(outerMap.get(key));
if (innerList == null) {
- innerList = FastList.newInstance();
+ innerList = new LinkedList<V>();
outerMap.put(key, innerList);
}
return innerList;
@@ -340,7 +313,7 @@ public class UtilMisc {
if (c instanceof Set<?>) {
theSet = (Set<T>) c;
} else {
- theSet = FastSet.newInstance();
+ theSet = new HashSet<T>();
c.remove(null);
theSet.addAll(c);
}
@@ -352,7 +325,7 @@ public class UtilMisc {
* @return The resulting Set
*/
public static <T> Set<T> toSet(T obj1) {
- Set<T> theSet = FastSet.newInstance();
+ Set<T> theSet = new HashSet<T>();
theSet.add(obj1);
return theSet;
}
@@ -362,7 +335,7 @@ public class UtilMisc {
* @return The resulting Set
*/
public static <T> Set<T> toSet(T obj1, T obj2) {
- Set<T> theSet = FastSet.newInstance();
+ Set<T> theSet = new HashSet<T>();
theSet.add(obj1);
theSet.add(obj2);
return theSet;
@@ -373,7 +346,7 @@ public class UtilMisc {
* @return The resulting Set
*/
public static <T> Set<T> toSet(T obj1, T obj2, T obj3) {
- Set<T> theSet = FastSet.newInstance();
+ Set<T> theSet = new HashSet<T>();
theSet.add(obj1);
theSet.add(obj2);
theSet.add(obj3);
@@ -385,7 +358,7 @@ public class UtilMisc {
* @return The resulting Set
*/
public static <T> Set<T> toSet(T obj1, T obj2, T obj3, T obj4) {
- Set<T> theSet = FastSet.newInstance();
+ Set<T> theSet = new HashSet<T>();
theSet.add(obj1);
theSet.add(obj2);
theSet.add(obj3);
@@ -398,7 +371,7 @@ public class UtilMisc {
* @return The resulting Set
*/
public static <T> Set<T> toSet(T obj1, T obj2, T obj3, T obj4, T obj5) {
- Set<T> theSet = FastSet.newInstance();
+ Set<T> theSet = new HashSet<T>();
theSet.add(obj1);
theSet.add(obj2);
theSet.add(obj3);
@@ -412,7 +385,7 @@ public class UtilMisc {
* @return The resulting Set
*/
public static <T> Set<T> toSet(T obj1, T obj2, T obj3, T obj4, T obj5, T obj6) {
- Set<T> theSet = FastSet.newInstance();
+ Set<T> theSet = new HashSet<T>();
theSet.add(obj1);
theSet.add(obj2);
theSet.add(obj3);
@@ -423,7 +396,7 @@ public class UtilMisc {
}
public static <T> Set<T> toSet(T obj1, T obj2, T obj3, T obj4, T obj5, T obj6, T obj7, T obj8) {
- Set<T> theSet = FastSet.newInstance();
+ Set<T> theSet = new HashSet<T>();
theSet.add(obj1);
theSet.add(obj2);
theSet.add(obj3);
@@ -440,7 +413,7 @@ public class UtilMisc {
if (collection instanceof Set<?>) {
return (Set<T>) collection;
} else {
- Set<T> theSet = FastSet.newInstance();
+ Set<T> theSet = new HashSet<T>();
theSet.addAll(collection);
return theSet;
}
@@ -450,7 +423,7 @@ public class UtilMisc {
if (data == null) {
return null;
}
- Set<T> set = FastSet.newInstance();
+ Set<T> set = new HashSet<T>();
for (T value: data) {
set.add(value);
}
@@ -462,7 +435,7 @@ public class UtilMisc {
* @return The resulting List
*/
public static <T> List<T> toList(T obj1) {
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
list.add(obj1);
return list;
@@ -473,7 +446,7 @@ public class UtilMisc {
* @return The resulting List
*/
public static <T> List<T> toList(T obj1, T obj2) {
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
list.add(obj1);
list.add(obj2);
@@ -485,7 +458,7 @@ public class UtilMisc {
* @return The resulting List
*/
public static <T> List<T> toList(T obj1, T obj2, T obj3) {
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
list.add(obj1);
list.add(obj2);
@@ -498,7 +471,7 @@ public class UtilMisc {
* @return The resulting List
*/
public static <T> List<T> toList(T obj1, T obj2, T obj3, T obj4) {
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
list.add(obj1);
list.add(obj2);
@@ -512,7 +485,7 @@ public class UtilMisc {
* @return The resulting List
*/
public static <T> List<T> toList(T obj1, T obj2, T obj3, T obj4, T obj5) {
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
list.add(obj1);
list.add(obj2);
@@ -527,7 +500,7 @@ public class UtilMisc {
* @return The resulting List
*/
public static <T> List<T> toList(T obj1, T obj2, T obj3, T obj4, T obj5, T obj6) {
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
list.add(obj1);
list.add(obj2);
@@ -539,7 +512,7 @@ public class UtilMisc {
}
public static <T> List<T> toList(T obj1, T obj2, T obj3, T obj4, T obj5, T obj6, T obj7, T obj8, T obj9) {
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
list.add(obj1);
list.add(obj2);
@@ -558,7 +531,7 @@ public class UtilMisc {
if (collection instanceof List<?>) {
return (List<T>) collection;
} else {
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
list.addAll(collection);
return list;
}
@@ -568,7 +541,7 @@ public class UtilMisc {
if (data == null) {
return null;
}
- List<T> list = FastList.newInstance();
+ List<T> list = new LinkedList<T>();
for (T value: data) {
list.add(value);
}
@@ -578,7 +551,7 @@ public class UtilMisc {
public static <K, V> void addToListInMap(V element, Map<K, Object> theMap, K listKey) {
List<V> theList = UtilGenerics.checkList(theMap.get(listKey));
if (theList == null) {
- theList = FastList.newInstance();
+ theList = new LinkedList<V>();
theMap.put(listKey, theList);
}
theList.add(element);
@@ -587,7 +560,7 @@ public class UtilMisc {
public static <K, V> void addToSetInMap(V element, Map<K, Set<V>> theMap, K setKey) {
Set<V> theSet = UtilGenerics.checkSet(theMap.get(setKey));
if (theSet == null) {
- theSet = FastSet.newInstance();
+ theSet = new HashSet<V>();
theMap.put(setKey, theSet);
}
theSet.add(element);
@@ -791,251 +764,6 @@ public class UtilMisc {
return availableLocaleList;
}
- /** This is meant to be very quick to create and use for small sized maps, perfect for how we usually use UtilMisc.toMap */
- @SuppressWarnings("serial")
- protected static class SimpleMap<V> implements Map<String, V>, java.io.Serializable {
- protected Map<String, V> realMapIfNeeded = null;
-
- String[] names;
- Object[] values;
-
- public SimpleMap() {
- names = new String[0];
- values = new Object[0];
- }
-
- public SimpleMap(String name1, Object value1) {
- names = new String[1];
- values = new Object[1];
- this.names[0] = name1;
- this.values[0] = value1;
- }
-
- public SimpleMap(String name1, Object value1, String name2, Object value2) {
- names = new String[2];
- values = new Object[2];
- this.names[0] = name1;
- this.values[0] = value1;
- this.names[1] = name2;
- this.values[1] = value2;
- }
-
- public SimpleMap(String name1, Object value1, String name2, Object value2, String name3, Object value3) {
- names = new String[3];
- values = new Object[3];
- this.names[0] = name1;
- this.values[0] = value1;
- this.names[1] = name2;
- this.values[1] = value2;
- this.names[2] = name3;
- this.values[2] = value3;
- }
-
- public SimpleMap(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) {
- names = new String[4];
- values = new Object[4];
- this.names[0] = name1;
- this.values[0] = value1;
- this.names[1] = name2;
- this.values[1] = value2;
- this.names[2] = name3;
- this.values[2] = value3;
- this.names[3] = name4;
- this.values[3] = value4;
- }
-
- @SuppressWarnings("unchecked")
- protected void makeRealMap() {
- realMapIfNeeded = FastMap.newInstance();
- for (int i = 0; i < names.length; i++) {
- realMapIfNeeded.put(names[i], (V) values[i]);
- }
- this.names = null;
- this.values = null;
- }
-
- public void clear() {
- if (realMapIfNeeded != null) {
- realMapIfNeeded.clear();
- } else {
- realMapIfNeeded = FastMap.newInstance();
- names = null;
- values = null;
- }
- }
-
- public boolean containsKey(Object obj) {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.containsKey(obj);
- } else {
- for (String name: names) {
- if (obj == null && name == null) return true;
- if (name != null && name.equals(obj)) return true;
- }
- return false;
- }
- }
-
- public boolean containsValue(Object obj) {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.containsValue(obj);
- } else {
- for (Object value: values) {
- if (obj == null && value == null) return true;
- if (value != null && value.equals(obj)) return true;
- }
- return false;
- }
- }
-
- public java.util.Set<Map.Entry<String, V>> entrySet() {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.entrySet();
- } else {
- this.makeRealMap();
- return realMapIfNeeded.entrySet();
- }
- }
-
- @SuppressWarnings("unchecked")
- public V get(Object obj) {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.get(obj);
- } else {
- for (int i = 0; i < names.length; i++) {
- if (obj == null && names[i] == null) return (V) values[i];
- if (names[i] != null && names[i].equals(obj)) return (V) values[i];
- }
- return null;
- }
- }
-
- public boolean isEmpty() {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.isEmpty();
- } else {
- if (this.names.length == 0) return true;
- return false;
- }
- }
-
- public java.util.Set<String> keySet() {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.keySet();
- } else {
- this.makeRealMap();
- return realMapIfNeeded.keySet();
- }
- }
-
- public V put(String obj, V obj1) {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.put(obj, obj1);
- } else {
- this.makeRealMap();
- return realMapIfNeeded.put(obj, obj1);
- }
- }
-
- public void putAll(java.util.Map<? extends String, ? extends V> map) {
- if (realMapIfNeeded != null) {
- realMapIfNeeded.putAll(map);
- } else {
- this.makeRealMap();
- realMapIfNeeded.putAll(map);
- }
- }
-
- public V remove(Object obj) {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.remove(obj);
- } else {
- this.makeRealMap();
- return realMapIfNeeded.remove(obj);
- }
- }
-
- public int size() {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.size();
- } else {
- return this.names.length;
- }
- }
-
- public java.util.Collection<V> values() {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.values();
- } else {
- this.makeRealMap();
- return realMapIfNeeded.values();
- }
- }
-
- @Override
- public String toString() {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.toString();
- } else {
- StringBuilder outString = new StringBuilder("{");
- for (int i = 0; i < names.length; i++) {
- if (i > 0) outString.append(',');
- outString.append('{');
- outString.append(names[i]);
- outString.append(',');
- outString.append(values[i]);
- outString.append('}');
- }
- outString.append('}');
- return outString.toString();
- }
- }
-
- @Override
- public int hashCode() {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.hashCode();
- } else {
- int hashCode = 0;
- for (int i = 0; i < names.length; i++) {
- //note that this calculation is done based on the calc specified in the Java java.util.Map interface
- int tempNum = (names[i] == null ? 0 : names[i].hashCode()) ^
- (values[i] == null ? 0 : values[i].hashCode());
- hashCode += tempNum;
- }
- return hashCode;
- }
- }
-
- @Override
- public boolean equals(Object obj) {
- if (realMapIfNeeded != null) {
- return realMapIfNeeded.equals(obj);
- } else {
- Map<String, V> mapObj = UtilGenerics.<String, V>checkMap(obj);
-
- //first check the size
- if (mapObj.size() != names.length) return false;
-
- //okay, same size, now check each entry
- for (int i = 0; i < names.length; i++) {
- //first check the name
- if (!mapObj.containsKey(names[i])) return false;
-
- //if that passes, check the value
- Object mapValue = mapObj.get(names[i]);
- if (mapValue == null) {
- if (values[i] != null) return false;
- } else {
- if (!mapValue.equals(values[i])) return false;
- }
- }
-
- return true;
- }
- }
- }
-
/** @deprecated use Thread.sleep() */
@Deprecated
public static void staticWait(long timeout) throws InterruptedException {