forked from phoedos/pmd
new facilities in prep for property management framework
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4652 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
68
pmd/src/net/sourceforge/pmd/util/ClassUtil.java
Normal file
68
pmd/src/net/sourceforge/pmd/util/ClassUtil.java
Normal file
@ -0,0 +1,68 @@
|
||||
package net.sourceforge.pmd.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* Various class-related utility methods
|
||||
*
|
||||
* @author Brian Remedios
|
||||
*/
|
||||
public class ClassUtil {
|
||||
|
||||
private ClassUtil() {};
|
||||
|
||||
private static final Map primitiveTypesByName = CollectionUtil.mapFrom( new Object[][] {
|
||||
{"int", int.class },
|
||||
{"byte", byte.class },
|
||||
{"long", long.class },
|
||||
{"short", short.class },
|
||||
{"float", float.class },
|
||||
{"double", double.class },
|
||||
{"char", char.class },
|
||||
{"boolean", boolean.class },
|
||||
});
|
||||
|
||||
private static final Map typesByShortName = CollectionUtil.mapFrom( new Object[][] {
|
||||
{"Integer", Integer.class },
|
||||
{"Byte", Byte.class },
|
||||
{"Long", Long.class },
|
||||
{"Short", Short.class },
|
||||
{"Float", Float.class },
|
||||
{"Double", Double.class },
|
||||
{"Character", Character.class },
|
||||
{"Boolean", Boolean.class },
|
||||
{"BigDecimal", BigDecimal.class },
|
||||
{"String", String.class },
|
||||
{"Object", Object.class },
|
||||
{"Object[]", Object[].class }
|
||||
});
|
||||
|
||||
/**
|
||||
* Method getPrimitiveTypeFor.
|
||||
* @param name String
|
||||
* @return Class
|
||||
*/
|
||||
public static Class getPrimitiveTypeFor(String name) {
|
||||
return (Class)primitiveTypesByName.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to determine the actual class given the short name.
|
||||
*
|
||||
* @param shortName String
|
||||
* @return Class
|
||||
*/
|
||||
public static Class getTypeFor(String shortName) {
|
||||
|
||||
Class cls = (Class)typesByShortName.get(shortName);
|
||||
if (cls != null) return cls;
|
||||
|
||||
cls = (Class)primitiveTypesByName.get(shortName);
|
||||
if (cls != null) return cls;
|
||||
|
||||
return CollectionUtil.getCollectionTypeFor(shortName);
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +1,48 @@
|
||||
package net.sourceforge.pmd.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* Generic collection and array-related utility functions.
|
||||
*
|
||||
* @author Brian Remedios
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class CollectionUtil {
|
||||
|
||||
public static final Map collectionTypesByShortName = mapFrom( new Object[][] {
|
||||
{"List", java.util.List.class },
|
||||
{"Collection", java.util.Collection.class },
|
||||
{"Map", java.util.Map.class },
|
||||
{"ArrayList", java.util.ArrayList.class },
|
||||
{"LinkedList", java.util.LinkedList.class },
|
||||
{"Vector", java.util.Vector.class },
|
||||
{"HashMap", java.util.HashMap.class },
|
||||
{"TreeMap", java.util.TreeMap.class },
|
||||
{"Set", java.util.Set.class },
|
||||
{"HashSet", java.util.HashSet.class }
|
||||
});
|
||||
|
||||
private CollectionUtil() {};
|
||||
|
||||
/**
|
||||
* Returns the collection type if we know it by its short name.
|
||||
*
|
||||
* @param name String
|
||||
* @return Class
|
||||
*/
|
||||
public static Class getCollectionTypeFor(String name) {
|
||||
return (Class)collectionTypesByShortName.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method asSet.
|
||||
* Returns the items as a populated set.
|
||||
*
|
||||
* @param items Object[]
|
||||
* @return Set
|
||||
*/
|
||||
@ -24,4 +54,86 @@ public class CollectionUtil {
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a map populated with the keyValuesSets where
|
||||
* the value held by the tuples are they key and value in that order.
|
||||
*
|
||||
* @param keyValueSets Object[][]
|
||||
* @return Map
|
||||
*/
|
||||
public static Map mapFrom(Object[][] keyValueSets) {
|
||||
Map map = new HashMap(keyValueSets.length);
|
||||
for (int i=0; i<keyValueSets.length; i++) {
|
||||
map.put(keyValueSets[i][0], keyValueSets[i][1]);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map based on the source but with the key & values swapped.
|
||||
*
|
||||
* @param source Map
|
||||
* @return Map
|
||||
*/
|
||||
public static Map invertedMapFrom(Map source) {
|
||||
Map map = new HashMap(source.size());
|
||||
Iterator iter = source.entrySet().iterator();
|
||||
Entry entry;
|
||||
while (iter.hasNext()) {
|
||||
entry = (Entry)iter.next();
|
||||
map.put(entry.getValue(), entry.getKey());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the objects are array instances and each of their elements compares
|
||||
* via equals as well.
|
||||
*
|
||||
* @param value Object
|
||||
* @param otherValue Object
|
||||
* @return boolean
|
||||
*/
|
||||
public static final boolean arraysAreEqual(Object value, Object otherValue) {
|
||||
if (value instanceof Object[]) {
|
||||
if (otherValue instanceof Object[]) return valuesAreTransitivelyEqual((Object[])value, (Object[])otherValue);
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the arrays are equal by examining each of their elements, even if they are
|
||||
* arrays themselves.
|
||||
*
|
||||
* @param thisArray Object[]
|
||||
* @param thatArray Object[]
|
||||
* @return boolean
|
||||
*/
|
||||
public static final boolean valuesAreTransitivelyEqual(Object[] thisArray, Object[] thatArray) {
|
||||
if (thisArray == thatArray) return true;
|
||||
if ((thisArray == null) || (thatArray == null)) return false;
|
||||
if (thisArray.length != thatArray.length) return false;
|
||||
for (int i = 0; i < thisArray.length; i++) {
|
||||
if (!areEqual(thisArray[i], thatArray[i])) return false; // recurse if req'd
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* A comprehensive isEqual method that handles nulls and arrays safely.
|
||||
*
|
||||
* @param value Object
|
||||
* @param otherValue Object
|
||||
* @return boolean
|
||||
*/
|
||||
public static final boolean areEqual(Object value, Object otherValue) {
|
||||
if (value == otherValue) return true;
|
||||
if (value == null) return false;
|
||||
if (otherValue == null) return false;
|
||||
|
||||
if (value.getClass().getComponentType() != null) return arraysAreEqual(value, otherValue);
|
||||
return value.equals(otherValue);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user