/** * 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; }
public static void notEmpty(byte[] array, String msg) { if (Objects.isEmpty(array)) { throw new IllegalArgumentException(msg); } }
/** * Assert that an array has elements; that is, it must not be * <code>null</code> and must have at least one element. * <pre class="code">Assert.notEmpty(array, "The array must have elements");</pre> * @param array the array to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements */ public static void notEmpty(Object[] array, String message) { if (Objects.isEmpty(array)) { throw new IllegalArgumentException(message); } }
/** * Append the given String to the given String array, returning a new array * consisting of the input array contents plus the given String. * @param array the array to append to (can be <code>null</code>) * @param str the String to append * @return the new array (never <code>null</code>) */ public static String[] addStringToArray(String[] array, String str) { if (Objects.isEmpty(array)) { return new String[] {str}; } String[] newArr = new String[array.length + 1]; System.arraycopy(array, 0, newArr, 0, array.length); newArr[array.length] = str; return newArr; }
/** * Trim the elements of the given String array, * calling <code>String.trim()</code> on each of them. * @param array the original String array * @return the resulting array (of the same size) with trimmed elements */ public static String[] trimArrayElements(String[] array) { if (Objects.isEmpty(array)) { return new String[0]; } String[] result = new String[array.length]; for (int i = 0; i < array.length; i++) { String element = array[i]; result[i] = (element != null ? element.trim() : null); } return result; }
/** * Concatenate the given String arrays into one, * with overlapping array elements included twice. * <p>The order of elements in the original arrays is preserved. * @param array1 the first array (can be <code>null</code>) * @param array2 the second array (can be <code>null</code>) * @return the new array (<code>null</code> if both given arrays were <code>null</code>) */ public static String[] concatenateStringArrays(String[] array1, String[] array2) { if (Objects.isEmpty(array1)) { return array2; } if (Objects.isEmpty(array2)) { return array1; } String[] newArr = new String[array1.length + array2.length]; System.arraycopy(array1, 0, newArr, 0, array1.length); System.arraycopy(array2, 0, newArr, array1.length, array2.length); return newArr; }
/** * Remove duplicate Strings from the given array. * Also sorts the array, as it uses a TreeSet. * @param array the String array * @return an array without duplicates, in natural sort order */ public static String[] removeDuplicateStrings(String[] array) { if (Objects.isEmpty(array)) { return array; } Set<String> set = new TreeSet<String>(); for (String element : array) { set.add(element); } return toStringArray(set); }
/** * Merge the given String arrays into one, with overlapping * array elements only included once. * <p>The order of elements in the original arrays is preserved * (with the exception of overlapping elements, which are only * included on their first occurrence). * @param array1 the first array (can be <code>null</code>) * @param array2 the second array (can be <code>null</code>) * @return the new array (<code>null</code> if both given arrays were <code>null</code>) */ public static String[] mergeStringArrays(String[] array1, String[] array2) { if (Objects.isEmpty(array1)) { return array2; } if (Objects.isEmpty(array2)) { return array1; } List<String> result = new ArrayList<String>(); result.addAll(Arrays.asList(array1)); for (String str : array2) { if (!result.contains(str)) { result.add(str); } } return toStringArray(result); }
/** * 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(); }
String[] array, String delimiter, String charsToDelete) { if (Objects.isEmpty(array)) { return null;
/** * Find a single value of one of the given types in the given Collection: * searching the Collection for a value of the first type, then * searching for a value of the second type, etc. * @param collection the collection to search * @param types the types to look for, in prioritized order * @return a value of one of the given types found if there is a clear match, * or <code>null</code> if none or more than one such value found */ public static Object findValueOfType(Collection<?> collection, Class<?>[] types) { if (isEmpty(collection) || Objects.isEmpty(types)) { return null; } for (Class<?> type : types) { Object value = findValueOfType(collection, type); if (value != null) { return value; } } return null; }
public static void notEmpty(byte[] array, String msg) { if (Objects.isEmpty(array)) { throw new IllegalArgumentException(msg); } }
public static void notEmpty(byte[] array, String msg) { if (Objects.isEmpty(array)) { throw new IllegalArgumentException(msg); } }
/** * 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; }
/** * 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; }
/** * Assert that an array has elements; that is, it must not be * <code>null</code> and must have at least one element. * <pre class="code">Assert.notEmpty(array, "The array must have elements");</pre> * @param array the array to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements */ public static void notEmpty(Object[] array, String message) { if (Objects.isEmpty(array)) { throw new IllegalArgumentException(message); } }
/** * Assert that an array has elements; that is, it must not be * <code>null</code> and must have at least one element. * <pre class="code">Assert.notEmpty(array, "The array must have elements");</pre> * @param array the array to check * @param message the exception message to use if the assertion fails * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements */ public static void notEmpty(Object[] array, String message) { if (Objects.isEmpty(array)) { throw new IllegalArgumentException(message); } }
/** * Append the given String to the given String array, returning a new array * consisting of the input array contents plus the given String. * @param array the array to append to (can be <code>null</code>) * @param str the String to append * @return the new array (never <code>null</code>) */ public static String[] addStringToArray(String[] array, String str) { if (Objects.isEmpty(array)) { return new String[] {str}; } String[] newArr = new String[array.length + 1]; System.arraycopy(array, 0, newArr, 0, array.length); newArr[array.length] = str; return newArr; }
/** * Remove duplicate Strings from the given array. * Also sorts the array, as it uses a TreeSet. * @param array the String array * @return an array without duplicates, in natural sort order */ public static String[] removeDuplicateStrings(String[] array) { if (Objects.isEmpty(array)) { return array; } Set<String> set = new TreeSet<String>(); for (String element : array) { set.add(element); } return toStringArray(set); }