@Override public int compare(boolean[] left, boolean[] right) { int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { int result = Booleans.compare(left[i], right[i]); if (result != 0) { return result; } } return left.length - right.length; }
@Override public int hashCode() { int result = 1; for (int i = start; i < end; i++) { result = 31 * result + Booleans.hashCode(array[i]); } return result; }
public void testAsListHashcode() { assertEquals(1, Booleans.asList(EMPTY).hashCode()); assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), Booleans.asList(ARRAY_FALSE).hashCode()); List<Boolean> reference = Booleans.asList(ARRAY_FALSE); assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), reference.hashCode()); }
public void testToArray() { // need explicit type parameter to avoid javac warning!? List<Boolean> none = Arrays.<Boolean>asList(); assertTrue(Arrays.equals(EMPTY, Booleans.toArray(none))); List<Boolean> one = Arrays.asList(false); assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.toArray(one))); boolean[] array = {false, false, true}; List<Boolean> three = Arrays.asList(false, false, true); assertTrue(Arrays.equals(array, Booleans.toArray(three))); assertTrue(Arrays.equals(array, Booleans.toArray(Booleans.asList(array)))); }
public void testToArray_threadSafe() { // Only for booleans, we lengthen VALUES boolean[] VALUES = BooleansTest.VALUES; VALUES = Booleans.concat(VALUES, VALUES); for (int delta : new int[] {+1, 0, -1}) { for (int i = 0; i < VALUES.length; i++) { List<Boolean> list = Booleans.asList(VALUES).subList(0, i); Collection<Boolean> misleadingSize = Helpers.misleadingSizeCollection(delta); misleadingSize.addAll(list); boolean[] arr = Booleans.toArray(misleadingSize); assertEquals(i, arr.length); for (int j = 0; j < i; j++) { assertEquals(VALUES[j], arr[j]); } } } }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * <p><b>Note:</b> consider representing the array as a {@link java.util.BitSet} instead, and * using {@link java.util.BitSet#nextSetBit(int)} or {@link java.util.BitSet#nextClearBit(int)}. * * @param array an array of {@code boolean} values, possibly empty * @param target a primitive {@code boolean} 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(boolean[] array, boolean 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 boolean} values, possibly empty * @param target a primitive {@code boolean} 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(boolean[] array, boolean target) { return lastIndexOf(array, target, 0, array.length); }
@Override public PersistedData serializeCollection(Collection<Boolean> value, SerializationContext context) { return context.create(Booleans.toArray(value)); }
/** * 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 boolean[] ensureCapacity(boolean[] 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; }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * <p><b>Note:</b> consider representing the array as a {@link java.util.BitSet} instead, and * using {@link java.util.BitSet#nextSetBit(int)} or {@link java.util.BitSet#nextClearBit(int)}. * * @param array an array of {@code boolean} values, possibly empty * @param target a primitive {@code boolean} 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(boolean[] array, boolean 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 boolean} values, possibly empty * @param target a primitive {@code boolean} 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(boolean[] array, boolean target) { return lastIndexOf(array, target, 0, array.length); }
@Override protected boolean[] getBooleanArrayInternal(int columnIndex) { return Booleans.toArray(getBooleanListInternal(columnIndex)); }
public void testToArray() { // need explicit type parameter to avoid javac warning!? List<Boolean> none = Arrays.<Boolean>asList(); assertTrue(Arrays.equals(EMPTY, Booleans.toArray(none))); List<Boolean> one = Arrays.asList(false); assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.toArray(one))); boolean[] array = {false, false, true}; List<Boolean> three = Arrays.asList(false, false, true); assertTrue(Arrays.equals(array, Booleans.toArray(three))); assertTrue(Arrays.equals(array, Booleans.toArray(Booleans.asList(array)))); }
/** * 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 boolean[] ensureCapacity( boolean[] 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 int compare(boolean[] left, boolean[] right) { int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { int result = Booleans.compare(left[i], right[i]); if (result != 0) { return result; } } return left.length - right.length; }
@Override public int hashCode() { int result = 1; for (int i = start; i < end; i++) { result = 31 * result + Booleans.hashCode(array[i]); } return result; }
public void testAsListEquals() { assertEquals(Booleans.asList(EMPTY), Collections.emptyList()); assertEquals(Booleans.asList(ARRAY_FALSE), Booleans.asList(ARRAY_FALSE)); assertFalse(Booleans.asList(ARRAY_FALSE).equals(ARRAY_FALSE)); assertFalse(Booleans.asList(ARRAY_FALSE).equals(null)); assertFalse(Booleans.asList(ARRAY_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE))); assertFalse(Booleans.asList(ARRAY_FALSE_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE))); assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true)); List<Boolean> reference = Booleans.asList(ARRAY_FALSE); assertEquals(Booleans.asList(ARRAY_FALSE), reference); assertEquals(reference, reference); }
/** * Returns the index of the first appearance of the value {@code target} in {@code array}. * * <p><b>Note:</b> consider representing the array as a {@link java.util.BitSet} instead, and * using {@link java.util.BitSet#nextSetBit(int)} or {@link java.util.BitSet#nextClearBit(int)}. * * @param array an array of {@code boolean} values, possibly empty * @param target a primitive {@code boolean} 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(boolean[] array, boolean 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 boolean} values, possibly empty * @param target a primitive {@code boolean} 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(boolean[] array, boolean target) { return lastIndexOf(array, target, 0, array.length); }
public void testToArray_withNull() { List<Boolean> list = Arrays.asList(false, true, null); try { Booleans.toArray(list); fail(); } catch (NullPointerException expected) { } }