Class ObjectUtil
- java.lang.Object
-
- org.brijframework.util.objects.ObjectUtil
-
public abstract class ObjectUtil extends Object
-
-
Constructor Summary
Constructors Constructor Description ObjectUtil()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Object[]arrayByAddingObjAtZeroIndex(Object object, Object... paramObjects)static <T> T[]arrayByAppending(T[] _array, T _element)If _array is not null, returns a copy of array which has one more element than _array, _element being the last element in this array (even if _element is null).static <T> T[]arrayByAppending(T[] _array, T _element, boolean createGenericArray)static <T> T[]arrayByRemovingFirst(T[] _array)static List<?>asList(Object _array)Converts Collections and primitive arrays to List's.static ListbatchToList(Object[] _array, int _batchSize)This methods takes an array and separates it into a set of batches.static List<?>batchToList(List<?> _list, int _batchSize)This methods takes a list and separates it into a set of batches.ObjectcastObject(Object object, String type)static booleancompareCollectionSize(Collection<?> coll1, Collection<?> coll2)static booleancontains(Object[] _array, Object _object)Searches the given array for the given object (which can be null) by calling equals() on each item.static HashtableconvertCollectionToMap(Collection<?> vt, String keys)static VectorconvertHashtoVector(Hashtable h)static VectorConvertStringArrayToVector(String[] lines)static Vector<Object>convertStringtoArray(String buffer)static Vector<Object>convertStringtoArray(String buffer, String token1)static Vector<Object>convertStringtoArray1(String buffer)static Vector<Object>convertStringToVectorNoWhiteSpace(String buffer, String delim)static HashtableconvertVectorToHash(Vector vt, String keys)static String[]ConvertVectorToStringArray(Vector lines)intcount(Collection<?> collectionObj)To calculate size of the specified collection.static String[]dynArray(int size, String[] array)static HashMapextractRecordFromArray(Object[] _array, Object[] _keys, int[] _keyIndices, Format[] _keyFormats, boolean _excludeNulls)Extracts a Map from an array of values.static List<Map>extractRecordsFromArrays(Object[][] _array, Object[] _keys, int[] _keyIndices, Format[] _keyFormats, boolean _excludeNulls)Extracts a List of Maps from an two-dimensional array of values.static ArrayList<Object>getArrayFromHash(Map hash, String key, boolean isMandatory, ArrayList defaultValue)static booleangetBoolean(Map<String,Object> map, String key)static booleangetBooleanFromHash(Map hash, String key, boolean isMandatory, boolean defaultValue)static StringgetCharSeparatedText(Iterable iterable, String c)static doublegetDoubleFromHash(Map hash, String key, boolean isMandatory, double min, double max, double defaultValue)Map<String,Object>getFieldsMapFromObject(Object obj)static ArrayListgetFilteredList(Collection _beanList, String keyPath, Object filterValue)static HashtablegetHashFromHash(Map hash, String key, boolean isMandatory, Hashtable defaultValue)static intgetIntFromHash(Map hash, String key, boolean isMandatory, int min, int max, int defaultValue)static Vector<Object>getLinesForFile(File file)static Vector<Object>getLinesForString(String value)Map<?,?>getMultiValuesFormCollection(Collection<?> list, String property)this method get List of values for property of objectstatic Vector<Object>getPartVectorForString(String str, int parts)static Map<String,Object>getPropertiesFromCollection(Collection<?> list, String[] properties)static Map<String,Object>getPropertiesFromMap(Map<?,?> map, Object[] properties)this method get Map of values of objectsstatic Object[]getPropertyValue(Object[] _objects, String _path)Iterates over all objects in the array, retrieves the value for the given key path and stores it into a new array in the same order.static <T> List<T>getPropertyValue(Collection<?> _objects, String _key)Iterates over all objects in the list, retrieves the value for the given key and stores it into a new list in the same order.static <T> List<T>getPropertyValue(List<Object> _objects, String _path)Iterates over all objects in the list, retrieves the value for the given key path and stores it into a new list in the same order.static StringgetUncommentedValue(String str)static CollectiongetValuesFormCollection(Collection<?> list, String property)static VectorgetVectorFromHash(Map hash, String key, boolean isMandatory, Vector defaultValue)static HashMap<Object,List<Object>>groupByKey(Collection<?> _objects, String _key)This method walks over a collection and groups the contained object based on the value of some key.static HashMap<Object,List<Object>>groupByKeyPath(Collection<?> _objects, String _keyPath)This method walks over a collection and groups the contained object based on the value of some keypath.static MapgroupByKeyPathes(Collection _objects, String[] _keyPathes)This method groups a collection based on a set of criterias.static byte[]hashToBytes(Hashtable<?,?> hash)static int[]intValuesForObjects(Object[] _values)Converts an array of objects into an array of int's.static booleanisEmpty(Object[] _array)static booleanisEmpty(Collection<?> _collection)static booleanisEmpty(Map<?,?> _map)booleanisEquivalent(Object object1, Object object2, Map<String,String> keyMap, boolean isDebug)static intlastIndexOfObjectEqualTo(Object[] _array, Object _object)Searches the given array for the given object backwards and returns the index of the last slot containing that object.static intlastIndexOfObjectIdenticalTo(Object[] _array, Object _object)Searches the given array for the given object backwards and returns the index of the last slot containing that object.static <T> List<T>listDifference(Class<T> objectClass, List<?> list1, List<?> list2)static ArrayList<String>listForArray(String[] stringArray)This method is used to convert stringArrayintoFNArrayListstatic List<String>listForArray(List<String[]> listOfStringArray)This method is used to convertFNArrayListcontaining stringArrayintoFNArrayList.static List<Number>listForCount(int _count)Returns a List which contains a sequence of Integer objects.static voidmain(String[] args)static Map<Object,Object>mapForKey(String key, Collection<?> objects)static Map<Object,Object>mapForKey(List<?> list, String key)static Map<String,Object>mapForKeyValues(String keys, Object... values)static Collection<?>mapToCollection(Map<?,?> map)static Numbermax(Number[] array)Return maximum value from array.static Numbermin(Number[] array)Return minimum value from array.static Hashtable<String,Object>readHashTable(File file)static Number[]removeElement(Number[] array, Number element)Remove the all element that match the specified element from integer array.static Object[]removeElement(Object[] array, Object element)Remove the all element that match the specified element from Object array.static Object[]removeElement(Object[] array, String property, String val)Remove the all element that match the specified element from Object array.static voidreverseArray(Object[] array)Reverse the Object array;static voidreverseIntArray(int[] array)Reverse the integer array;static voidsetPropertiesFromCollection(Collection<?> list, Map<String,Object> map)static voidsetPropertiesFromMap(Map<String,Object> origMap, Map<String,Object> map)this method get Map of values of objectsstatic voidsort(List _beanObjects, String key, boolean isReverse)static voidsortArray(Object[] array, boolean isDesc)Sorting the Object array;static voidsortIntegerArray(int[] array)Sort the array using bubble sort.static voidswapArray(Object[] array, int index1, int index2)Swap the Object arraystatic voidswapIntArray(int[] array, int index1, int index2)Swap the Integer array valuestatic Collection<?>textToCollection(String buffer)static Collection<?>textToCollection(String buffer, String text)static Collection<?>textToCollectionNoWhiteSpace(String buffer, String delim)voidupdateObjectFromMap(Object aObject, Map<String,Object> map)voidupdateObjectFromMap(Object object, Map<String,Object> map, boolean skipNullValues)voidupdateObjectGraphFromMap(Object object, Map<?,?> _map)static Object[]valuesForKey(Object[] _objects, String _key)Iterates over all objects in the array, retrieves the value for the given key and stores it into a new array in the same order.static voidwriteHashTable(File file, Hashtable<String,Object> hash)
-
-
-
Method Detail
-
swapArray
public static void swapArray(Object[] array, int index1, int index2)
Swap the Object array- Parameters:
array-index1-index2-
-
reverseArray
public static void reverseArray(Object[] array)
Reverse the Object array;- Parameters:
array-
-
sortArray
public static void sortArray(Object[] array, boolean isDesc)
Sorting the Object array;- Parameters:
array-
-
removeElement
public static Object[] removeElement(Object[] array, String property, String val)
Remove the all element that match the specified element from Object array.- Parameters:
array- Object array.property- and val will be match then it to be removed- Returns:
- array after removing element.
-
removeElement
public static Object[] removeElement(Object[] array, Object element)
Remove the all element that match the specified element from Object array.- Parameters:
array- Object array.element- to be removed- Returns:
- array after removing element.
-
swapIntArray
public static void swapIntArray(int[] array, int index1, int index2)Swap the Integer array value- Parameters:
array-index1-index2-
-
reverseIntArray
public static void reverseIntArray(int[] array)
Reverse the integer array;- Parameters:
array-
-
max
public static Number max(Number[] array)
Return maximum value from array.- Parameters:
array-- Returns:
- maximum value
-
min
public static Number min(Number[] array)
Return minimum value from array.- Parameters:
array-- Returns:
- minimum value
-
removeElement
public static Number[] removeElement(Number[] array, Number element)
Remove the all element that match the specified element from integer array.- Parameters:
array- integer array.element- to be removed- Returns:
- array after removing element.
-
sortIntegerArray
public static void sortIntegerArray(int[] array)
Sort the array using bubble sort.- Parameters:
array-
-
dynArray
public static String[] dynArray(int size, String[] array)
- Parameters:
size-array-- Returns:
-
contains
public static boolean contains(Object[] _array, Object _object)
Searches the given array for the given object (which can be null) by calling equals() on each item.- Parameters:
_array- - array of Objects to be searched_object- - object to search- Returns:
- true if the array contains the _object, false otherwise
-
lastIndexOfObjectIdenticalTo
public static int lastIndexOfObjectIdenticalTo(Object[] _array, Object _object)
Searches the given array for the given object backwards and returns the index of the last slot containing that object. 'null' values are allowed.- Parameters:
_array- - an array of objects_object- - the object to be searched for (or null)- Returns:
- the last index of object in the array, or -1 if it wasn't found
-
lastIndexOfObjectEqualTo
public static int lastIndexOfObjectEqualTo(Object[] _array, Object _object)
Searches the given array for the given object backwards and returns the index of the last slot containing that object. 'null' values are allowed.- Parameters:
_array- - an array of objects_object- - the object to be searched for (or null)- Returns:
- the last index of object in the array, or -1 if it wasn't found
-
batchToList
public static List<?> batchToList(List<?> _list, int _batchSize)
This methods takes a list and separates it into a set of batches.Example: batchToList(source, 2):
source: ( 1, 2, 3, 4, 5 ) result: ( ( 1, 2 ), ( 3, 4 ), ( 5 ) )
- Parameters:
_list- - the list which should be split_batchSize- - the size of a batch- Returns:
- a list of lists containing the objects
-
batchToList
public static List batchToList(Object[] _array, int _batchSize)
This methods takes an array and separates it into a set of batches.Example: batchToList(source, 2):
source: [ 1, 2, 3, 4, 5 ] result: ( ( 1, 2 ), ( 3, 4 ), ( 5 ) )
- Parameters:
_array- - the array which should be split_batchSize- - the size of a batch- Returns:
- a list of lists containing the objects
-
getPropertyValue
public static <T> List<T> getPropertyValue(Collection<?> _objects, String _key)
Iterates over all objects in the list, retrieves the value for the given key and stores it into a new list in the same order. This method is slightly faster than using getPropertyValue, which of course can also be used for simple keys.Example:
List projectIds = valuesForKeys(projects, "logID");
The returned List should be considered immutable.
- Parameters:
_objects- the objects to retrieve the values from_key- the key of the values to be retrieved- Returns:
- a list containing the values for the given key
- See Also:
getPropertyValue()
-
getPropertyValue
public static <T> List<T> getPropertyValue(List<Object> _objects, String _path)
Iterates over all objects in the list, retrieves the value for the given key path and stores it into a new list in the same order.Example:
List ownerIds = getPropertyValue(projects, "owner.id");
The returned List should be considered immutable.
- Parameters:
_objects- the objects to retrieve the values from_path- the keypath of the values to be retrieved- Returns:
- a list containing the values for the given key
- See Also:
valuesForKey()
-
valuesForKey
public static Object[] valuesForKey(Object[] _objects, String _key)
Iterates over all objects in the array, retrieves the value for the given key and stores it into a new array in the same order. This method is slightly faster than using getPropertyValue, which of course can also be used for simple keys.Example:
Object[] projectIds = valuesForKeys(projects, "logID");
- Parameters:
_objects- the objects to retrieve the values from_key- the key of the values to be retrieved- Returns:
- an array containing the values for the given key
- See Also:
getPropertyValue()
-
getPropertyValue
public static Object[] getPropertyValue(Object[] _objects, String _path)
Iterates over all objects in the array, retrieves the value for the given key path and stores it into a new array in the same order.Example:
Object[] ownerIds = getPropertyValue(projects, "owner.id");
- Parameters:
_objects- the objects to retrieve the values from_path- the keypath of the values to be retrieved- Returns:
- an array containing the values for the given key
- See Also:
valuesForKey()
-
groupByKeyPath
public static HashMap<Object,List<Object>> groupByKeyPath(Collection<?> _objects, String _keyPath)
This method walks over a collection and groups the contained object based on the value of some keypath. Example:Map groupedByEntity = groupByKeyPath(myObjects, "entity.name")
This will iterate over the given objects and group them by the entity, a result could look like:{ Person = [ xyz, def ]; Team = [ abc, def ]; }- Parameters:
_objects- - objects which should be grouped by the given keypath_keyPath- - the criteria to group on- Returns:
- a Map which contains the group values at the keys
-
groupByKey
public static HashMap<Object,List<Object>> groupByKey(Collection<?> _objects, String _key)
This method walks over a collection and groups the contained object based on the value of some key. Example:Map groupedByCity = groupByKey(myObjects, "city")
This will iterate over the given objects and group them by the city, a result could look like:{ Magdeburg = [ { city = Madgeburg; name = "SWM"; }, { city = Magdeburg; name = "Skyrix AG"; } ]; Dortmund = [ ... ]; }This method currently calls groupByKeyPath() with the given key as the keypath.
- Parameters:
_objects- objects which should be grouped by the given key_key- the criteria to group on (eg 'city' or 'lastname')- Returns:
- a Map which contains the group values at the keys
-
groupByKeyPathes
public static Map groupByKeyPathes(Collection _objects, String[] _keyPathes)
This method groups a collection based on a set of criterias. Eg given those keys: [ city, zip ] a result could look like:{ Magdeburg = { 39104 = [ { name = Skyrix; ...}, { name = TLG; ...} ]; 39122 = [ { name = Telekom; ...} ]; }; Berlin = { 10233 = ... 23882 = ... }; }Note that only the last object in the tree is a List, all the parent groups are Maps.- Parameters:
_objects- the objects to be grouped_keyPathes- the keypathes to group the objects on- Returns:
- a Map of Maps or Lists representing the grouping-tree
-
extractRecordFromArray
public static HashMap extractRecordFromArray(Object[] _array, Object[] _keys, int[] _keyIndices, Format[] _keyFormats, boolean _excludeNulls)
Extracts a Map from an array of values.Using the _keyIndices array you can use one value of the _array multiple times, eg:
array = [ 'Donald', '1954-12-12' ] keys = [ 'name', 'birthdayAsText', 'birthdayAsDate' ] indices = [ 0, 1, 1 ] formats = [ null, null, new SimpleDateFormat() ]
Results in:{ name = Donald; birthdayAsText = '1954-12-12'; birthdayAsDate = <CalendarDate: 1954-12-12>; }And another simple example:extractRecordFromArray(new Object[] { 'Donald', '1954-12-12' ] });Results in:{ 0 = Donald; 1 = '1954-12-12'; }If no keys are specified, numeric Integer keys are generated (0,1,2,3,...).
If a format throws a ParseException, the exception is stored in the slot.
- Parameters:
_array- - array of values_keys- - array of Map keys to generate_keyIndices- - if null, _keys and _array indices must match_keyFormats- - value Format objects, when null, use value as-is_excludeNulls- - whether null values should be added- Returns:
- a Map constructed according to the specification
-
extractRecordsFromArrays
public static List<Map> extractRecordsFromArrays(Object[][] _array, Object[] _keys, int[] _keyIndices, Format[] _keyFormats, boolean _excludeNulls)
Extracts a List of Maps from an two-dimensional array of values. This is useful for converting parsed CSV files to a set of records.Using the _keyIndices array you can use one value of the _array multiple times, eg:
array = [ [ 'Donald', '1954-12-12' ] ] keys = [ 'name', 'birthdayAsText', 'birthdayAsDate' ] indices = [ 0, 1, 1 ] formats = [ null, null, new SimpleDateFormat() ]
Results in:[ { name = Donald; birthdayAsText = '1954-12-12'; birthdayAsDate = <CalendarDate: 1954-12-12>; } ]If no keys are specified, numeric Integer keys are generated (0,1,2,3,...).
- Parameters:
_array- - an array of arrays of values_keys- - array of Map keys to generate_keyIndices- - if null, _keys and _array indices must match_keyFormats- - value Format objects, when null, use value as-is_excludeNulls- - whether null values should be added- Returns:
- a List of Maps constructed according to the specification
-
asList
public static List<?> asList(Object _array)
Converts Collections and primitive arrays to List's. If the object already is a List, its returned directly.- Parameters:
_array- - an arbitary Java array (eg int[], Object[]), or Collection- Returns:
- a List containing the object items
-
arrayByAppending
public static <T> T[] arrayByAppending(T[] _array, T _element)If _array is not null, returns a copy of array which has one more element than _array, _element being the last element in this array (even if _element is null). If _array is null but _element isn't null, returns an array consisting just of _element. If both _array and _element are null, returns null.- Parameters:
_array- - a Java array or null (see above)_element- - a Java object of same type as _array or null (see above)- Returns:
- an extended array or null (see above)
-
arrayByAppending
public static <T> T[] arrayByAppending(T[] _array, T _element, boolean createGenericArray)
-
arrayByRemovingFirst
public static <T> T[] arrayByRemovingFirst(T[] _array)
-
intValuesForObjects
public static int[] intValuesForObjects(Object[] _values)
Converts an array of objects into an array of int's. If a slot is a Number object, this will store the intValue() of the number. If its a String object, the String is parsed using Integer.parseInt(). If a NumberFormatException occures, we store 0. For other objects we invoke UObject.intValue() to determine the 'int' representation of the object.- Parameters:
_values- - an array of objects to convert to integers- Returns:
- an array of int's
-
listForCount
public static List<Number> listForCount(int _count)
Returns a List which contains a sequence of Integer objects.Example:
List<Number> a = listForCount(10);
This will return the List "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]".- Parameters:
_count- - number of items in the List- Returns:
- a List
-
count
public int count(Collection<?> collectionObj)
To calculate size of the specified collection.
- Parameters:
collectionObj- of which size is to be known- Returns:
- size of the specified collection.
- Since:
- Brijframework 1.0
-
compareCollectionSize
public static boolean compareCollectionSize(Collection<?> coll1, Collection<?> coll2)
-
listDifference
public static <T> List<T> listDifference(Class<T> objectClass, List<?> list1, List<?> list2)
-
getValuesFormCollection
public static Collection getValuesFormCollection(Collection<?> list, String property)
-
getMultiValuesFormCollection
public Map<?,?> getMultiValuesFormCollection(Collection<?> list, String property)
this method get List of values for property of object- Parameters:
Collection-- Returns:
- Map
-
setPropertiesFromCollection
public static void setPropertiesFromCollection(Collection<?> list, Map<String,Object> map)
-
getPropertiesFromCollection
public static Map<String,Object> getPropertiesFromCollection(Collection<?> list, String[] properties)
-
textToCollection
public static Collection<?> textToCollection(String buffer, String text)
-
textToCollectionNoWhiteSpace
public static Collection<?> textToCollectionNoWhiteSpace(String buffer, String delim)
-
textToCollection
public static Collection<?> textToCollection(String buffer)
-
mapToCollection
public static Collection<?> mapToCollection(Map<?,?> map)
-
getPropertiesFromMap
public static Map<String,Object> getPropertiesFromMap(Map<?,?> map, Object[] properties)
this method get Map of values of objects- Parameters:
Map- , Object[]- Returns:
- Map
-
setPropertiesFromMap
public static void setPropertiesFromMap(Map<String,Object> origMap, Map<String,Object> map)
this method get Map of values of objects- Parameters:
origMap- ,map
-
convertCollectionToMap
public static Hashtable convertCollectionToMap(Collection<?> vt, String keys)
-
updateObjectFromMap
public void updateObjectFromMap(Object object, Map<String,Object> map, boolean skipNullValues)
-
arrayByAddingObjAtZeroIndex
public Object[] arrayByAddingObjAtZeroIndex(Object object, Object... paramObjects)
-
getLinesForFile
public static Vector<Object> getLinesForFile(File file) throws IOException
- Throws:
IOException
-
hashToBytes
public static byte[] hashToBytes(Hashtable<?,?> hash)
-
convertStringtoArray
public static Vector<Object> convertStringtoArray(String buffer, String token1)
-
isEquivalent
public boolean isEquivalent(Object object1, Object object2, Map<String,String> keyMap, boolean isDebug)
-
convertStringToVectorNoWhiteSpace
public static Vector<Object> convertStringToVectorNoWhiteSpace(String buffer, String delim)
-
getIntFromHash
public static int getIntFromHash(Map hash, String key, boolean isMandatory, int min, int max, int defaultValue)
-
getDoubleFromHash
public static double getDoubleFromHash(Map hash, String key, boolean isMandatory, double min, double max, double defaultValue)
-
getBooleanFromHash
public static boolean getBooleanFromHash(Map hash, String key, boolean isMandatory, boolean defaultValue)
-
getVectorFromHash
public static Vector getVectorFromHash(Map hash, String key, boolean isMandatory, Vector defaultValue)
-
getHashFromHash
public static Hashtable getHashFromHash(Map hash, String key, boolean isMandatory, Hashtable defaultValue)
-
getArrayFromHash
public static ArrayList<Object> getArrayFromHash(Map hash, String key, boolean isMandatory, ArrayList defaultValue)
-
getFilteredList
public static ArrayList getFilteredList(Collection _beanList, String keyPath, Object filterValue)
-
listForArray
public static ArrayList<String> listForArray(String[] stringArray)
This method is used to convert string
ArrayintoFNArrayList- Parameters:
stringArray- array of strings.- Returns:
FNArrayList- Since:
- Foundation 1.0
-
listForArray
public static List<String> listForArray(List<String[]> listOfStringArray)
This method is used to convert
FNArrayListcontaining stringArrayintoFNArrayList.- Parameters:
listOfStringArray-FNArrayListcontainingArrayof strings .- Returns:
FNArrayList- Since:
- Foundation 1.0
-
isEmpty
public static boolean isEmpty(Object[] _array)
-
isEmpty
public static boolean isEmpty(Collection<?> _collection)
-
isEmpty
public static boolean isEmpty(Map<?,?> _map)
-
mapForKey
public static Map<Object,Object> mapForKey(String key, Collection<?> objects)
-
main
public static void main(String[] args)
-
-