/** * Turn given source String array into sorted array. * @param array the source array * @return the sorted array (never <code>null</code>) */ public static String[] sortStringArray(String[] array) { if (Objects.isEmpty(array)) { return new String[0]; } Arrays.sort(array); return array; }
/** * Check whether the given array contains the given element. * * @param array the array to check (may be <code>null</code>, * in which case the return value will always be <code>false</code>) * @param element the element to check for * @return whether the element has been found in the given array */ public static boolean containsElement(Object[] array, Object element) { if (array == null) { return false; } for (Object arrayEle : array) { if (nullSafeEquals(arrayEle, element)) { return true; } } return false; }
/** * Check whether the given array of enum constants contains a constant with the given name, * ignoring case when determining a match. * * @param enumValues the enum values to check, typically the product of a call to MyEnum.values() * @param constant the constant name to find (must not be null or empty string) * @return whether the constant has been found in the given array */ public static boolean containsConstant(Enum<?>[] enumValues, String constant) { return containsConstant(enumValues, constant, false); }
/** * Convenience method to return a String array as a delimited (e.g. CSV) * String. E.g. useful for <code>toString()</code> implementations. * @param arr the array to display * @param delim the delimiter to use (probably a ",") * @return the delimited String */ public static String arrayToDelimitedString(Object[] arr, String delim) { if (Objects.isEmpty(arr)) { return ""; } if (arr.length == 1) { return Objects.nullSafeToString(arr[0]); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { if (i > 0) { sb.append(delim); } sb.append(arr[i]); } return sb.toString(); }
/** * Return a hash code based on the contents of the specified array. * If <code>array</code> is <code>null</code>, this method returns 0. */ 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; }
protected byte[] doCompress(byte[] payload) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); GZIPOutputStream compressorOutputStream = new GZIPOutputStream(outputStream, true); try { compressorOutputStream.write(payload, 0, payload.length); compressorOutputStream.finish(); return outputStream.toByteArray(); } finally { Objects.nullSafeClose(compressorOutputStream, outputStream); } } }
/** * Return a hash code based on the contents of the specified array. * If <code>array</code> is <code>null</code>, this method returns 0. */ 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; }
/** * Return a content-based String representation if <code>obj</code> is * not <code>null</code>; otherwise returns an empty String. * <p>Differs from {@link #nullSafeToString(Object)} in that it returns * an empty String rather than "null" for a <code>null</code> value. * * @param obj the object to build a display String for * @return a display String representation of <code>obj</code> * @see #nullSafeToString(Object) */ public static String getDisplayString(Object obj) { if (obj == null) { return EMPTY_STRING; } return nullSafeToString(obj); }
return toJSONArray(coll); if (Objects.isArray(object)) { Collection c = Collections.arrayToList(object); return toJSONArray(c);
/** * 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 an empty String 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); }
/** * Check whether the given exception is compatible with the exceptions * declared in a throws clause. * * @param ex the exception to checked * @param declaredExceptions the exceptions declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { int i = 0; while (i < declaredExceptions.length) { if (declaredExceptions[i].isAssignableFrom(ex.getClass())) { return true; } i++; } } return false; }
/** * Convenience method to return a String array as a delimited (e.g. CSV) * String. E.g. useful for <code>toString()</code> implementations. * @param arr the array to display * @param delim the delimiter to use (probably a ",") * @return the delimited String */ public static String arrayToDelimitedString(Object[] arr, String delim) { if (Objects.isEmpty(arr)) { return ""; } if (arr.length == 1) { return Objects.nullSafeToString(arr[0]); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { if (i > 0) { sb.append(delim); } sb.append(arr[i]); } return sb.toString(); }
/** * Return a hash code based on the contents of the specified array. * If <code>array</code> is <code>null</code>, this method returns 0. */ 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; }
@Override public byte[] doCompress(byte[] payload) throws IOException { Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); ByteArrayOutputStream outputStream = null; DeflaterOutputStream deflaterOutputStream = null; try { outputStream = new ByteArrayOutputStream(); deflaterOutputStream = new DeflaterOutputStream(outputStream, deflater, true); deflaterOutputStream.write(payload, 0, payload.length); deflaterOutputStream.flush(); return outputStream.toByteArray(); } finally { Objects.nullSafeClose(outputStream, deflaterOutputStream); } }
return nullSafeHashCode((Object[]) obj); return nullSafeHashCode((boolean[]) obj); return nullSafeHashCode((byte[]) obj); return nullSafeHashCode((char[]) obj); return nullSafeHashCode((double[]) obj); return nullSafeHashCode((float[]) obj); return nullSafeHashCode((int[]) obj); return nullSafeHashCode((long[]) obj); return nullSafeHashCode((short[]) obj);
return nullSafeToString((Object[]) obj); return nullSafeToString((boolean[]) obj); return nullSafeToString((byte[]) obj); return nullSafeToString((char[]) obj); return nullSafeToString((double[]) obj); return nullSafeToString((float[]) obj); return nullSafeToString((int[]) obj); return nullSafeToString((long[]) obj); return nullSafeToString((short[]) obj);
return toJSONArray(coll); if (Objects.isArray(object)) { Collection c = Collections.arrayToList(object); return toJSONArray(c);
/** * 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 an empty String 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); }
/** * Check whether the given exception is compatible with the exceptions * declared in a throws clause. * * @param ex the exception to checked * @param declaredExceptions the exceptions declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { int i = 0; while (i < declaredExceptions.length) { if (declaredExceptions[i].isAssignableFrom(ex.getClass())) { return true; } i++; } } return false; }
public static void notEmpty(byte[] array, String msg) { if (Objects.isEmpty(array)) { throw new IllegalArgumentException(msg); } }