com.googlecode.ecache.annotations.utils.Objects Maven / Gradle / Ivy
Show all versions of ehcache-spring-annotations Show documentation
/**
* Copyright 2010 Nicholas Blair, Eric Dalquist
*
* Licensed 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 com.googlecode.ecache.annotations.utils;
import java.util.HashSet;
import java.util.Set;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
*
* Miscellaneous object utility methods.
*
*
* @author Alex Ruiz
*/
public abstract class Objects {
private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
private static final String ARRAY_END = "}";
private static final String ARRAY_START = "{";
private static final String EMPTY_ARRAY = "{}";
private static final int INITIAL_HASH = 7;
private static final int MULTIPLIER = 31;
private static final String NULL_ARRAY = "null";
private static final Set> primitivesAndWrappers;
static {
primitivesAndWrappers = new HashSet>();
primitivesAndWrappers.add(boolean.class);
primitivesAndWrappers.add(Boolean.class);
primitivesAndWrappers.add(byte.class);
primitivesAndWrappers.add(Byte.class);
primitivesAndWrappers.add(char.class);
primitivesAndWrappers.add(Character.class);
primitivesAndWrappers.add(double.class);
primitivesAndWrappers.add(Double.class);
primitivesAndWrappers.add(float.class);
primitivesAndWrappers.add(Float.class);
primitivesAndWrappers.add(int.class);
primitivesAndWrappers.add(Integer.class);
primitivesAndWrappers.add(long.class);
primitivesAndWrappers.add(Long.class);
primitivesAndWrappers.add(short.class);
primitivesAndWrappers.add(Short.class);
}
/**
* Returns the same value as {@link Boolean#hashCode()}
.
*
* @param bool
* the given boolean
.
* @return the hash code for the given boolean
.
* @see Boolean#hashCode()
*/
public static int hashCode(boolean bool) {
return bool ? 1231 : 1237;
}
/**
* Returns the same value as {@link Double#hashCode()}
.
*
* @param dbl
* the given double
.
* @return the hash code for the given double
.
* @see Double#hashCode()
*/
public static int hashCode(double dbl) {
long bits = Double.doubleToLongBits(dbl);
return hashCode(bits);
}
/**
* Returns the same value as {@link Float#hashCode()}
.
*
* @param flt
* the given float
.
* @return the hash code for the given float
.
* @see Float#hashCode()
*/
public static int hashCode(float flt) {
return Float.floatToIntBits(flt);
}
/**
* Returns the same value as {@link Long#hashCode()}
.
*
* @param lng
* the given long
.
* @return the hash code for the given long
.
* @see Long#hashCode()
*/
public static int hashCode(long lng) {
return (int) (lng ^ (lng >>> 32));
}
/**
*
* Returns a StringBuffer
containing:
*
* - the class name of the given object
* - the character '@'
* - the hex string for the object's identity hash code
*
*
*
* This method will return an empty StringBuffer
if the given
* object is null
.
*
*
* @param obj
* the given object.
* @return a StringBuffer
containing identity information of
* the given object.
*/
public static StringBuffer identityToString(Object obj) {
StringBuffer buffer = new StringBuffer();
if (obj != null) {
buffer.append(obj.getClass().getName());
buffer.append("@");
buffer.append(ObjectUtils.getIdentityHexString(obj));
}
return buffer;
}
/**
* Returns true
if the given object is an array of primitives.
*
* @param array
* the given object to check.
* @return true
if the given object is an array of primitives.
*/
public static boolean isArrayOfPrimitives(Object array) {
boolean primitiveArray = false;
if (array != null) {
Class extends Object> clazz = array.getClass();
primitiveArray = clazz.isArray()
&& clazz.getComponentType().isPrimitive();
}
return primitiveArray;
}
/**
* Returns true
if the given class is any of the following:
*
* boolean
* - Boolean
* byte
* - Byte
* char
* - Character
* double
* - Double
* float
* - Float
* int
* - Integer
* long
* - Long
* short
* - Short
*
*
* @param clazz
* the given class.
* @return true
if the given class represents a primitive or a
* wrapper, false
otherwise.
*/
public static boolean isPrimitiveOrWrapper(Class> clazz) {
return primitivesAndWrappers.contains(clazz);
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two boolean
arrays a
and b
* such that Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
*
* If array
is null
, this method returns 0.
*
*
* @param array
* the array whose hash value to compute.
* @return a content-based hash code for array
.
* @see #hashCode(boolean)
*/
public static int nullSafeHashCode(boolean[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + hashCode(array[i]);
}
return hash;
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two byte
arrays a
and b
such
* that Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
*
* If array
is null
, this method returns 0.
*
*
* @param array
* the array whose hash value to compute.
* @return a content-based hash code for array
.
*/
public static int nullSafeHashCode(byte[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + array[i];
}
return hash;
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two char
arrays a
and b
such
* that Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
*
* If array
is null
, this method returns 0.
*
*
* @param array
* the array whose hash value to compute.
* @return a content-based hash code for array
.
*/
public static int nullSafeHashCode(char[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + array[i];
}
return hash;
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two double
arrays a
and b
such
* that Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
*
* If array
is null
, this method returns 0.
*
*
* @param array
* the array whose hash value to compute.
* @return a content-based hash code for array
.
* @see #hashCode(double)
*/
public static int nullSafeHashCode(double[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + hashCode(array[i]);
}
return hash;
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two float
arrays a
and b
such
* that Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
*
* If array
is null
, this method returns 0.
*
*
* @param array
* the array whose hash value to compute.
* @return a content-based hash code for array
.
* @see #hashCode(float)
*/
public static int nullSafeHashCode(float[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + hashCode(array[i]);
}
return hash;
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two int
arrays a
and b
such
* that Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
*
* If array
is null
, this method returns 0.
*
*
* @param array
* the array whose hash value to compute.
* @return a content-based hash code for array
.
*/
public static int nullSafeHashCode(int[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + array[i];
}
return hash;
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two long
arrays a
and b
such
* that Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
*
* If array
is null
, this method returns 0.
*
*
* @param array
* the array whose hash value to compute.
* @return a content-based hash code for array
.
* @see #hashCode(long)
*/
public static int nullSafeHashCode(long[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + hashCode(array[i]);
}
return hash;
}
/**
*
* Returns the value of {@link Object#hashCode()}
. If the
* object is an array, this method will delegate to any of the
* nullSafeHashCode
methods for arrays in this class.
*
*
*
* If the object is null
, this method returns 0.
*
*
* @param obj
* the object whose hash value to compute.
* @return the hash code of the given object.
* @see #nullSafeHashCode(boolean[])
* @see #nullSafeHashCode(byte[])
* @see #nullSafeHashCode(char[])
* @see #nullSafeHashCode(double[])
* @see #nullSafeHashCode(float[])
* @see #nullSafeHashCode(int[])
* @see #nullSafeHashCode(long[])
* @see #nullSafeHashCode(Object[])
* @see #nullSafeHashCode(short[])
*/
public static int nullSafeHashCode(Object obj) {
if (obj == null)
return 0;
if (obj instanceof boolean[]) {
return nullSafeHashCode((boolean[]) obj);
}
if (obj instanceof byte[]) {
return nullSafeHashCode((byte[]) obj);
}
if (obj instanceof char[]) {
return nullSafeHashCode((char[]) obj);
}
if (obj instanceof double[]) {
return nullSafeHashCode((double[]) obj);
}
if (obj instanceof float[]) {
return nullSafeHashCode((float[]) obj);
}
if (obj instanceof int[]) {
return nullSafeHashCode((int[]) obj);
}
if (obj instanceof long[]) {
return nullSafeHashCode((long[]) obj);
}
if (obj instanceof short[]) {
return nullSafeHashCode((short[]) obj);
}
if (obj instanceof Object[]) {
return nullSafeHashCode((Object[]) obj);
}
return obj.hashCode();
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two arrays a
and b
such that
* Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
* The value returned by this method is equal to the value that would be
* returned by Arrays.asList(a).hashCode()
, unless
* array
is null
, in which case 0
* is returned.
*
*
* @param array
* the array whose content-based hash code to compute.
* @return a content-based hash code for array
.
*/
public static int nullSafeHashCode(Object[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);
}
return hash;
}
/**
*
* Returns a hash code based on the contents of the specified array. For any
* two short
arrays a
and b
such
* that Arrays.equals(a, b)
, it is also the case that
* Arrays.hashCode(a) == Arrays.hashCode(b)
.
*
*
*
* If array
is null
, this method returns 0.
*
* @param array
* the array whose hash value to compute
* @return a content-based hash code for array
*/
public static int nullSafeHashCode(short[] array) {
if (array == null)
return 0;
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + array[i];
}
return hash;
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(boolean[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(array[i]);
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(byte[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(array[i]);
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(char[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append("'" + array[i] + "'");
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(double[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(array[i]);
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(float[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(array[i]);
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(int[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(array[i]);
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(long[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(array[i]);
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(Object[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(StringUtils.quoteIfString(array[i]));
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(short[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(array[i]);
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Returns a string representation of the contents of the specified array. The
* string representation consists of a list of the array's elements, enclosed
* in curly braces ("{}"
). Adjacent elements are separated by
* the characters ", "
(a comma followed by a space). Returns
* "null"
if array
is null
.
*
* @param array
* the array whose string representation to return.
* @return a string representation of array
.
*/
public static String nullSafeToString(String[] array) {
if (array == null)
return NULL_ARRAY;
int length = array.length;
if (length == 0)
return EMPTY_ARRAY;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
if (i == 0)
buffer.append(ARRAY_START);
else
buffer.append(ARRAY_ELEMENT_SEPARATOR);
buffer.append(StringUtils.quote(array[i]));
}
buffer.append(ARRAY_END);
return buffer.toString();
}
}