📄 objectutils.java
字号:
/**
* Return the same value as <code>{@link Double#hashCode()}</code>.
* @see Double#hashCode()
*/
public static int hashCode(double dbl) {
long bits = Double.doubleToLongBits(dbl);
return hashCode(bits);
}
/**
* Return the same value as <code>{@link Float#hashCode()}</code>.
* @see Float#hashCode()
*/
public static int hashCode(float flt) {
return Float.floatToIntBits(flt);
}
/**
* Return the same value as <code>{@link Long#hashCode()}</code>.
* @see Long#hashCode()
*/
public static int hashCode(long lng) {
return (int) (lng ^ (lng >>> 32));
}
//---------------------------------------------------------------------
// Convenience methods for content-based toString output
//---------------------------------------------------------------------
/**
* Return a String representation of the specified Object.
* <p>Returns {@link #NULL_STRING the String 'null'} if <code>obj</code> is
* <code>null</code>.
* @param obj the object whose String representation to return
* @return a String representation of <code>obj</code>
*/
public static String nullSafeToString(Object obj) {
if (obj == null) {
return NULL_STRING;
}
if (obj instanceof String) {
return (String) obj;
}
if (obj instanceof Object[]) {
return nullSafeToString((Object[]) obj);
}
if (obj instanceof boolean[]) {
return nullSafeToString((boolean[]) obj);
}
if (obj instanceof byte[]) {
return nullSafeToString((byte[]) obj);
}
if (obj instanceof char[]) {
return nullSafeToString((char[]) obj);
}
if (obj instanceof double[]) {
return nullSafeToString((double[]) obj);
}
if (obj instanceof float[]) {
return nullSafeToString((float[]) obj);
}
if (obj instanceof int[]) {
return nullSafeToString((int[]) obj);
}
if (obj instanceof long[]) {
return nullSafeToString((long[]) obj);
}
if (obj instanceof short[]) {
return nullSafeToString((short[]) obj);
}
return obj.toString();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(Object[] array) {
if (array == null) {
return NULL_STRING;
}
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(String.valueOf(array[i]));
}
buffer.append(ARRAY_END);
return buffer.toString();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(boolean[] array) {
if (array == null) {
return NULL_STRING;
}
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();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(byte[] array) {
if (array == null) {
return NULL_STRING;
}
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();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(char[] array) {
if (array == null) {
return NULL_STRING;
}
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();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(double[] array) {
if (array == null) {
return NULL_STRING;
}
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();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(float[] array) {
if (array == null) {
return NULL_STRING;
}
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();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(int[] array) {
if (array == null) {
return NULL_STRING;
}
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();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(long[] array) {
if (array == null) {
return NULL_STRING;
}
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();
}
/**
* Return 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 (<code>"{}"</code>). Adjacent elements are separated by
* the characters <code>", "</code> (a comma followed by a space). Returns
* <code>"null"</code> if <code>array</code> is <code>null</code>.
* @param array the array whose String representation to return
* @return a String representation of <code>array</code>
*/
public static String nullSafeToString(short[] array) {
if (array == null) {
return NULL_STRING;
}
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();
}
/**
* Return a String representation of an object's overall identity.
* @param obj the object (may be <code>null</code>)
* @return the object's identity as String representation,
* or <code>null</code> if the object was <code>null</code>
*/
public static String identityToString(Object obj) {
if (obj == null) {
return EMPTY_STRING;
}
return obj.getClass().getName() + "@" + getIdentityHexString(obj);
}
/**
* Return a hex String form of an object's identity hash code.
* @param obj the object
* @return the object's identity code in hex notation
*/
public static String getIdentityHexString(Object obj) {
return Integer.toHexString(System.identityHashCode(obj));
}
/**
* Return a content-based String representation if <code>obj</code> is
* not <code>null</code>; otherwise returns an empty <code>String</code>.
* <p>Differs from <code>nullSafeToString</code> in that it returns an
* empty String rather than "null" for a <code>null</code> value.
* @see #nullSafeToString(Object)
*/
public static String getDisplayString(Object obj) {
if (obj == null) {
return EMPTY_STRING;
}
return nullSafeToString(obj);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -