@Override public int compare(char[] left, char[] right) { int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { int result = Chars.compare(left[i], right[i]); if (result != 0) { return result; } } return left.length - right.length; }
/** * Returns the {@code char} value whose big-endian representation is stored in the first 2 bytes * of {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getChar()}. For example, the * input byte array {@code {0x54, 0x32}} would yield the {@code char} value {@code '\\u5432'}. * * <p>Arguably, it's preferable to use {@link java.nio.ByteBuffer}; that library exposes much more * flexibility at little cost in readability. * * @throws IllegalArgumentException if {@code bytes} has fewer than 2 elements */ @GwtIncompatible // doesn't work public static char fromByteArray(byte[] bytes) { checkArgument(bytes.length >= BYTES, "array too small: %s < %s", bytes.length, BYTES); return fromBytes(bytes[0], bytes[1]); }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * @param array an array of {@code char} values, possibly empty * @param target a primitive {@code char} value * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int indexOf(char[] array, char target) { return indexOf(array, target, 0, array.length); }
public void testAsList_subList_toArray_roundTrip() { char[] array = {(char) 0, (char) 1, (char) 2, (char) 3}; List<Character> list = Chars.asList(array); assertTrue(Arrays.equals(new char[] {(char) 1, (char) 2}, Chars.toArray(list.subList(1, 3)))); assertTrue(Arrays.equals(new char[] {}, Chars.toArray(list.subList(2, 2)))); }
/** * Returns the index of the last appearance of the value {@code target} in {@code array}. * * @param array an array of {@code char} values, possibly empty * @param target a primitive {@code char} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(char[] array, char target) { return lastIndexOf(array, target, 0, array.length); }
@Override public int hashCode() { int result = 1; for (int i = start; i < end; i++) { result = 31 * result + Chars.hashCode(array[i]); } return result; }
public static Iterable<Character> $in(char[] elements) { return Chars.asList(elements); }
/** * Returns an array containing the same values as {@code array}, but * guaranteed to be of a specified minimum length. If {@code array} already * has a length of at least {@code minLength}, it is returned directly. * Otherwise, a new array of size {@code minLength + padding} is returned, * containing the values of {@code array}, and zeroes in the remaining places. * * @param array the source array * @param minLength the minimum length the returned array must guarantee * @param padding an extra amount to "grow" the array by if growth is * necessary * @throws IllegalArgumentException if {@code minLength} or {@code padding} is * negative * @return an array containing the values of {@code array}, with guaranteed * minimum length {@code minLength} */ public static char[] ensureCapacity( char[] array, int minLength, int padding) { checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); checkArgument(padding >= 0, "Invalid padding: %s", padding); return (array.length < minLength) ? copyOf(array, minLength + padding) : array; }
@Override public List<Character> create(Character[] elements) { char[] chars = Chars.toArray(Arrays.asList(elements)); return Lists.charactersOf(String.copyValueOf(chars)); } }
/** * Returns the index of the last appearance of the value {@code target} in {@code array}. * * @param array an array of {@code char} values, possibly empty * @param target a primitive {@code char} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(char[] array, char target) { return lastIndexOf(array, target, 0, array.length); }
@Override public int hashCode() { int result = 1; for (int i = start; i < end; i++) { result = 31 * result + Chars.hashCode(array[i]); } return result; }
public void testToArray() { // need explicit type parameter to avoid javac warning!? List<Character> none = Arrays.<Character>asList(); assertTrue(Arrays.equals(EMPTY, Chars.toArray(none))); List<Character> one = Arrays.asList((char) 1); assertTrue(Arrays.equals(ARRAY1, Chars.toArray(one))); char[] array = {(char) 0, (char) 1, 'A'}; List<Character> three = Arrays.asList((char) 0, (char) 1, 'A'); assertTrue(Arrays.equals(array, Chars.toArray(three))); assertTrue(Arrays.equals(array, Chars.toArray(Chars.asList(array)))); }
private static List<Character> asList(Character[] values) { char[] temp = new char[values.length]; for (int i = 0; i < values.length; i++) { temp[i] = checkNotNull(values[i]); // checkNotNull for GWT (do not optimize). } return Chars.asList(temp); }
/** * Returns an array containing the same values as {@code array}, but * guaranteed to be of a specified minimum length. If {@code array} already * has a length of at least {@code minLength}, it is returned directly. * Otherwise, a new array of size {@code minLength + padding} is returned, * containing the values of {@code array}, and zeroes in the remaining places. * * @param array the source array * @param minLength the minimum length the returned array must guarantee * @param padding an extra amount to "grow" the array by if growth is * necessary * @throws IllegalArgumentException if {@code minLength} or {@code padding} is * negative * @return an array containing the values of {@code array}, with guaranteed * minimum length {@code minLength} */ public static char[] ensureCapacity( char[] array, int minLength, int padding) { checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); checkArgument(padding >= 0, "Invalid padding: %s", padding); return (array.length < minLength) ? copyOf(array, minLength + padding) : array; }
@Override public List<Character> create(Character[] elements) { char[] chars = Chars.toArray(Arrays.asList(elements)); StringBuilder str = new StringBuilder(); str.append(chars); return Lists.charactersOf(str); } }
@Override public int compare(char[] left, char[] right) { int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { int result = Chars.compare(left[i], right[i]); if (result != 0) { return result; } } return left.length - right.length; }
/** * Returns the {@code char} value whose big-endian representation is stored in the first 2 bytes * of {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getChar()}. For example, the * input byte array {@code {0x54, 0x32}} would yield the {@code char} value {@code '\\u5432'}. * * <p>Arguably, it's preferable to use {@link java.nio.ByteBuffer}; that library exposes much more * flexibility at little cost in readability. * * @throws IllegalArgumentException if {@code bytes} has fewer than 2 elements */ @GwtIncompatible // doesn't work public static char fromByteArray(byte[] bytes) { checkArgument(bytes.length >= BYTES, "array too small: %s < %s", bytes.length, BYTES); return fromBytes(bytes[0], bytes[1]); }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * @param array an array of {@code char} values, possibly empty * @param target a primitive {@code char} value * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int indexOf(char[] array, char target) { return indexOf(array, target, 0, array.length); }
/** * Returns the index of the last appearance of the value {@code target} in {@code array}. * * @param array an array of {@code char} values, possibly empty * @param target a primitive {@code char} value * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no * such index exists. */ public static int lastIndexOf(char[] array, char target) { return lastIndexOf(array, target, 0, array.length); }
@Override public int hashCode() { int result = 1; for (int i = start; i < end; i++) { result = 31 * result + Chars.hashCode(array[i]); } return result; }