Class ObjectUtils
- java.lang.Object
-
- com.attivio.util.ObjectUtils
-
public final class ObjectUtils extends java.lang.ObjectContains utility functions for working with the Object type
-
-
Constructor Summary
Constructors Constructor Description ObjectUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static intbinarySearch(double[] array, double key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.static intbinarySearch(float[] array, float key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.static intbinarySearch(int[] array, int key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.static intbinarySearch(long[] array, long key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.static <T extends java.lang.Comparable<T>>
intbinarySearch(T[] array, T key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.static java.util.Dateclone(java.util.Date value)Null safe clone of a date.static <T> intcompare(java.lang.Comparable<T> a, T b)Null safe equivalent of a.compareTo(b)static intcompareTo(java.lang.Comparable a, java.lang.Comparable b)Deprecated.Use#compare(Comparable, Comparable)instead.static <T> java.util.List<T>copyOf(java.util.List<T> value)Null safe copy of a list.static booleanequals(double a, double b)Returns true if a is equivalent to b.static booleanequals(float a, float b)Returns true if a is equivalent to b.static booleanequals(java.lang.Object a, java.lang.Object b)Returns true if a is equivalent to b.static booleanequals(java.lang.Throwable a, java.lang.Throwable b)Compares equality of 2Throwables.static booleanequals(java.util.List<? extends java.lang.Throwable> a, java.util.List<? extends java.lang.Throwable> b)Compares if 2 lists ofThrowables are equivalent.static <T extends java.lang.Throwable>
TgetCause(java.lang.Throwable ex, java.lang.Class<T> type)Returns the exception of 'type' if anywhere in the exception's cause tree a class of 'type' is found, null otherwisestatic booleangetDefault(java.lang.Boolean value, boolean defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.static doublegetDefault(java.lang.Double value, double defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.static floatgetDefault(java.lang.Float value, float defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.static intgetDefault(java.lang.Integer value, int defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.static longgetDefault(java.lang.Long value, long defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.static <T> TgetDefault(T value, T defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.static inthashCode(boolean value)Compute the hash code of a primitiveboolean.static inthashCode(double value)Compute the hash code of a primitivedouble.static inthashCode(double[] data, int start, int count)Compute the hash code of a segment of an int[].static inthashCode(float value)Compute the hash code of a primitivefloat.static inthashCode(int[] data, int start, int count)Compute the hash code of a segment of an int[].static inthashCode(int hash, boolean value)Add the hashCode forvaluetohash.static inthashCode(int hash, double value)Add the hashCode forvaluetohash.static inthashCode(int hash, float value)Add the hashCode forvaluetohash.static inthashCode(int hash, int value)Add the hashCode forvaluetohash.static inthashCode(int hash, long value)Add the hashCode forvaluetohash.static inthashCode(int hash, java.lang.Object value)Add the hashCode forvaluetohash.static inthashCode(long value)Compute the hash code of a primitivelong.static inthashCode(java.lang.Class<?> clazz)Compute the hash code of a Class by using the hashcode ofClass.getName().static inthashCode(java.lang.Object value)Returns the value's hash code or 0 if null.static inthashCode(java.util.List<?> list)Compute the hash code of a list of objects, using our internal routines when possible.static java.lang.Stringintern(java.lang.String value)Intern and returnvalue(handles nulls).static booleanisFalse(java.lang.String value)Returns true if the stringvalueis a boolean value indicatingfalse.static booleanisTrue(java.lang.String value)Returns true if the stringvalueis a boolean value indicatingtrue.static <T> java.util.List<T>newList(T... vals)Creates a newListimplementation based on ordered input values.static <K,V>
java.util.Map<K,V>newMap(K key, V val, java.lang.Object... args)Creates a newLinkedHashMapimplementation based on key value pairs specified as paired arguments.static java.lang.BooleanparseBoolean(java.lang.Object value)Convertvalueto a Boolean (or null if not parseable).static java.lang.BooleanparseBoolean(java.lang.String value)Parse a boolean value (more lenient than Boolean.valueOf()).static java.math.BigDecimalparseDecimal(java.lang.Object value)Parse an arbitrary object as aBigDecimal.static doubleparseDouble(java.lang.Object value)Parse an arbitrary object as a double.static floatparseFloat(java.lang.Object value)Parse an arbitrary object as a float.static intparseInt(java.lang.Object value)Parse an arbitrary object as an integer.static longparseLong(java.lang.Object value)Parse an arbitrary object as a long.static intparseUnsigned(java.lang.Object value)Parses an arbitrary object into an unsigned integer.static voidprettyPrint(int[][] matrix)Pretty prints a 2-D int arraystatic <T> voidprettyPrint(T[][] matrix)Pretty prints a 2-D object arraystatic <T> intsize(java.util.List<java.util.List<T>> matrix)Returns the total number of cells in a matrixstatic <T> intsize(T[][] matrix)Returns the total number of cells in a matrixstatic <T> T[]toArray(T... args)Autobox-like creating of array.static booleantoBooleanValue(java.lang.Object value, boolean defaultValue)Converts an arbitrary object to a primitive boolean.static doubletoDoubleValue(java.lang.Object value, double defaultValue)Converts an arbitrary object to a primitive double.static floattoFloatValue(java.lang.Object value, float defaultValue)Converts an arbitrary object to a primitive float.static inttoIntValue(java.lang.Object value, int defaultValue)Converts an arbitrary object to a primitive integer.static longtoLongValue(java.lang.Object value, long defaultValue)Converts an arbitrary object to a primitive long.static <T extends java.lang.Comparable<? super T>>
java.util.List<T>toSortedList(java.util.Collection<T> collec)Converts an arbitrary collection into a sorted list.static java.lang.StringtoStringValue(java.lang.Object value, java.lang.String defaultValue)Converts an arbitrary object to a String.static inttoUnsignedValue(java.lang.Object value, int defaultValue)Converts an arbitrary object to a primitive unsigned integer.
-
-
-
Method Detail
-
getDefault
public static <T> T getDefault(T value, T defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.
-
intern
public static java.lang.String intern(java.lang.String value)
Intern and returnvalue(handles nulls).
-
getDefault
public static boolean getDefault(java.lang.Boolean value, boolean defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.
-
getDefault
public static int getDefault(java.lang.Integer value, int defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.
-
getDefault
public static long getDefault(java.lang.Long value, long defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.
-
getDefault
public static float getDefault(java.lang.Float value, float defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.
-
getDefault
public static double getDefault(java.lang.Double value, double defaultValue)ReturnsdefaultValueifvalueis null,valueotherwise.
-
toArray
public static <T> T[] toArray(T... args)
Autobox-like creating of array.
-
binarySearch
public static int binarySearch(int[] array, int key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.WARNING: no bounds checking is performed.
- Parameters:
array- the array to search.key- the key to search for.startIndex- the start index to start looking at (inclusive).endIndex- the end index to stop looking at (inclusive).- Returns:
- the index where key (or the greatest value less than key) is located.
-
binarySearch
public static int binarySearch(long[] array, long key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.WARNING: no bounds checking is performed.
- Parameters:
array- the array to search.key- the key to search for.startIndex- the start index to start looking at (inclusive).endIndex- the end index to stop looking at (inclusive).- Returns:
- the index where key (or the greatest value less than key) is located.
-
binarySearch
public static int binarySearch(float[] array, float key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.WARNING: no bounds checking is performed.
- Parameters:
array- the array to search.key- the key to search for.startIndex- the start index to start looking at (inclusive).endIndex- the end index to stop looking at (inclusive).- Returns:
- the index where key (or the greatest value less than key) is located.
-
binarySearch
public static int binarySearch(double[] array, double key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.WARNING: no bounds checking is performed.
- Parameters:
array- the array to search.key- the key to search for.startIndex- the start index to start looking at (inclusive).endIndex- the end index to stop looking at (inclusive).- Returns:
- the index where key (or the greatest value less than key) is located.
-
clone
public static java.util.Date clone(java.util.Date value)
Null safe clone of a date.
-
copyOf
public static <T> java.util.List<T> copyOf(java.util.List<T> value)
Null safe copy of a list.NOTE: this is a shallow clone.
-
binarySearch
public static <T extends java.lang.Comparable<T>> int binarySearch(T[] array, T key, int startIndex, int endIndex, int missingOffset)Fastest possible binary search.WARNING: no bounds checking is performed.
- Parameters:
array- the array to search.key- the key to search for.startIndex- the start index to start looking at (inclusive).endIndex- the end index to stop looking at (inclusive).- Returns:
- the index where key (or the greatest value less than key) is located.
-
size
public static <T> int size(T[][] matrix)
Returns the total number of cells in a matrix- Parameters:
matrix- the matrix- Returns:
- the total number of cells
-
size
public static <T> int size(java.util.List<java.util.List<T>> matrix)
Returns the total number of cells in a matrix- Parameters:
matrix- the matrix- Returns:
- the total number of cells
-
equals
public static boolean equals(java.lang.Object a, java.lang.Object b)Returns true if a is equivalent to b.- Parameters:
a- left hand sideb- right hand side- Returns:
- true if a is equivalent to b
-
equals
public static boolean equals(float a, float b)Returns true if a is equivalent to b.- Parameters:
a- left hand sideb- right hand side- Returns:
- true if a is equivalent to b
-
equals
public static boolean equals(double a, double b)Returns true if a is equivalent to b.- Parameters:
a- left hand sideb- right hand side- Returns:
- true if a is equivalent to b
-
equals
public static boolean equals(java.util.List<? extends java.lang.Throwable> a, java.util.List<? extends java.lang.Throwable> b)Compares if 2 lists ofThrowables are equivalent.Uses
equals(Throwable, Throwable)for comparing elements.
-
equals
public static boolean equals(java.lang.Throwable a, java.lang.Throwable b)Compares equality of 2Throwables.Equality check will validate a and b are of the same type and have same message.
-
compareTo
@Deprecated public static int compareTo(java.lang.Comparable a, java.lang.Comparable b)Deprecated.Use#compare(Comparable, Comparable)instead.Return the output of a.compareTo(b)Safe if a or b is null
- Parameters:
a- left hand sideb- right hand side- Returns:
- true if a is equivalent to b
-
compare
public static <T> int compare(java.lang.Comparable<T> a, T b)Null safe equivalent of a.compareTo(b)
-
toSortedList
public static <T extends java.lang.Comparable<? super T>> java.util.List<T> toSortedList(java.util.Collection<T> collec)
Converts an arbitrary collection into a sorted list.- Parameters:
collec- the collection to convert- Returns:
- the sorted list
-
toStringValue
public static java.lang.String toStringValue(java.lang.Object value, java.lang.String defaultValue)Converts an arbitrary object to a String.- Parameters:
value- the value to convertdefaultValue- if value is null- Returns:
- the value as a String, or defaultValue if conversion fails.
-
parseInt
public static int parseInt(java.lang.Object value)
Parse an arbitrary object as an integer.
-
parseLong
public static long parseLong(java.lang.Object value)
Parse an arbitrary object as a long.
-
parseFloat
public static float parseFloat(java.lang.Object value)
Parse an arbitrary object as a float.
-
parseDouble
public static double parseDouble(java.lang.Object value)
Parse an arbitrary object as a double.
-
parseDecimal
public static java.math.BigDecimal parseDecimal(java.lang.Object value)
Parse an arbitrary object as aBigDecimal.
-
toIntValue
public static int toIntValue(java.lang.Object value, int defaultValue)Converts an arbitrary object to a primitive integer.- Parameters:
value- the value to convertdefaultValue- if value cannot be converted, this will be returned- Returns:
- the value as a int, or defaultValue if conversion fails.
-
toUnsignedValue
public static int toUnsignedValue(java.lang.Object value, int defaultValue)Converts an arbitrary object to a primitive unsigned integer.- Parameters:
value- the value to convertdefaultValue- if value cannot be parsed, this will be returned- Returns:
- the value as a int, or defaultValue if conversion fails.
-
parseUnsigned
public static int parseUnsigned(java.lang.Object value) throws java.lang.NumberFormatException, java.lang.IllegalArgumentExceptionParses an arbitrary object into an unsigned integer.- Parameters:
value- the value to convert- Returns:
- the value as an int
- Throws:
java.lang.NumberFormatException- if object cannot be converted to intjava.lang.IllegalArgumentException- if integer value is negative
-
toLongValue
public static long toLongValue(java.lang.Object value, long defaultValue)Converts an arbitrary object to a primitive long.- Parameters:
value- the value to convertdefaultValue- if value cannot be converted, this will be returned- Returns:
- the value as a long, or defaultValue if conversion fails.
-
toFloatValue
public static float toFloatValue(java.lang.Object value, float defaultValue)Converts an arbitrary object to a primitive float.- Parameters:
value- the value to convertdefaultValue- if value cannot be converted, this will be returned- Returns:
- the value as a float, or defaultValue if conversion fails.
-
toDoubleValue
public static double toDoubleValue(java.lang.Object value, double defaultValue)Converts an arbitrary object to a primitive double.- Parameters:
value- the value to convertdefaultValue- if value cannot be converted, this will be returned- Returns:
- the value as a double, or defaultValue if conversion fails.
-
toBooleanValue
public static boolean toBooleanValue(java.lang.Object value, boolean defaultValue)Converts an arbitrary object to a primitive boolean.If value is a Boolean instance, return its value. If value is a Number instance, return true if it is non-zero. true will be returned if the value's lower cased string representation is true, yes, or on. false will be returned if values' lower cased string representation is false, no, or off. non-zero numbers will be return true, 0 returns false. Otherwise, the defaultValue will be returned.
- Parameters:
value- the value to convertdefaultValue- if value cannot be converted, this will be returned- Returns:
- the value as a boolean, or defaultValue if conversion fails.
-
isTrue
public static boolean isTrue(java.lang.String value)
Returns true if the stringvalueis a boolean value indicatingtrue.
-
isFalse
public static boolean isFalse(java.lang.String value)
Returns true if the stringvalueis a boolean value indicatingfalse.
-
parseBoolean
public static java.lang.Boolean parseBoolean(java.lang.String value)
Parse a boolean value (more lenient than Boolean.valueOf()).true will be returned if the value's lower cased string representation is true, yes, or on. false will be returned if values' lower cased string representation is false, no, or off. non-zero numbers will be return true, 0 returns false.
- Parameters:
value-- Returns:
- the value or null if not parseable as a boolean
-
parseBoolean
public static java.lang.Boolean parseBoolean(java.lang.Object value)
Convertvalueto a Boolean (or null if not parseable).
-
prettyPrint
public static void prettyPrint(int[][] matrix)
Pretty prints a 2-D int array
-
prettyPrint
public static <T> void prettyPrint(T[][] matrix)
Pretty prints a 2-D object array
-
getCause
public static <T extends java.lang.Throwable> T getCause(java.lang.Throwable ex, java.lang.Class<T> type)Returns the exception of 'type' if anywhere in the exception's cause tree a class of 'type' is found, null otherwise
-
newMap
public static <K,V> java.util.Map<K,V> newMap(K key, V val, java.lang.Object... args) throws java.lang.IllegalArgumentExceptionCreates a newLinkedHashMapimplementation based on key value pairs specified as paired arguments.The first key/value are used for compiler time type safety and are put into the map. All of the other arguments are NOT validated for type safety.
- Throws:
java.lang.IllegalArgumentException- if an odd number of arguments are specified
-
newList
@SafeVarargs public static <T> java.util.List<T> newList(T... vals)
Creates a newListimplementation based on ordered input values. Same as Arrays.asList() but provides type safety- Type Parameters:
T- the type of generic list to create- Parameters:
vals- the values to add to the list
-
hashCode
public static int hashCode(boolean value)
Compute the hash code of a primitiveboolean.
-
hashCode
public static int hashCode(float value)
Compute the hash code of a primitivefloat.
-
hashCode
public static int hashCode(double value)
Compute the hash code of a primitivedouble.
-
hashCode
public static int hashCode(long value)
Compute the hash code of a primitivelong.
-
hashCode
public static int hashCode(java.lang.Class<?> clazz)
Compute the hash code of a Class by using the hashcode ofClass.getName(). Null safe.
-
hashCode
public static int hashCode(java.util.List<?> list)
Compute the hash code of a list of objects, using our internal routines when possible.
-
hashCode
public static int hashCode(int[] data, int start, int count)Compute the hash code of a segment of an int[].
-
hashCode
public static int hashCode(double[] data, int start, int count)Compute the hash code of a segment of an int[].
-
hashCode
public static int hashCode(int hash, boolean value)Add the hashCode forvaluetohash.
-
hashCode
public static int hashCode(int hash, float value)Add the hashCode forvaluetohash.
-
hashCode
public static int hashCode(int hash, double value)Add the hashCode forvaluetohash.
-
hashCode
public static int hashCode(int hash, long value)Add the hashCode forvaluetohash.
-
hashCode
public static int hashCode(int hash, java.lang.Object value)Add the hashCode forvaluetohash.
-
hashCode
public static int hashCode(java.lang.Object value)
Returns the value's hash code or 0 if null.
-
hashCode
public static int hashCode(int hash, int value)Add the hashCode forvaluetohash.
-
-