private static String badPositionIndexes(int start, int end, int size) { if (start < 0 || start > size) { return badPositionIndex(start, size, "start index"); } if (end < 0 || end > size) { return badPositionIndex(end, size, "end index"); } // end < start return format("end index (%s) must not be less than start index (%s)", end, start); }
/** * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive. * * @param index a user-supplied index identifying an element of an array, list or string * @param size the size of that array, list or string * @return the value of {@code index} * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size} * @throws IllegalArgumentException if {@code size} is negative */ public static int checkElementIndex(int index, int size) { return checkElementIndex(index, size, "index"); }
/** * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of * size {@code size}. A position index may range from zero to {@code size}, inclusive. * * @param index a user-supplied index identifying a position in an array, list or string * @param size the size of that array, list or string * @return the value of {@code index} * @throws IndexOutOfBoundsException if {@code index} is negative or is greater than {@code size} * @throws IllegalArgumentException if {@code size} is negative */ public static int checkPositionIndex(int index, int size) { return checkPositionIndex(index, size, "index"); }
private static String badPositionIndex(int index, int size, String desc) { if (index < 0) { return format("%s (%s) must not be negative", desc, index); } else if (size < 0) { throw new IllegalArgumentException("negative size: " + size); } else { // index > size return format("%s (%s) must not be greater than size (%s)", desc, index, size); } }
/** * The {@code fragment} is added to the container view with id {@code frameId}. The operation is * performed by the {@code fragmentManager}. */ public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager, @NonNull Fragment fragment, int frameId) { Preconditions.checkNotNull(fragmentManager); Preconditions.checkNotNull(fragment); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.add(frameId, fragment); transaction.commit(); } }
/** * Ensures that {@code start} and {@code end} specify a valid <i>positions</i> in an array, list * or string of size {@code size}, and are in order. A position index may range from zero to * {@code size}, inclusive. * * @param start a user-supplied index identifying a starting position in an array, list or string * @param end a user-supplied index identifying a ending position in an array, list or string * @param size the size of that array, list or string * @throws IndexOutOfBoundsException if either index is negative or is greater than {@code size}, * or if {@code end} is less than {@code start} * @throws IllegalArgumentException if {@code size} is negative */ public static void checkPositionIndexes(int start, int end, int size) { // Carefully optimized for execution by hotspot (explanatory comment above) if (start < 0 || end < start || end > size) { throw new IndexOutOfBoundsException(badPositionIndexes(start, end, size)); } }
/** * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of * size {@code size}. A position index may range from zero to {@code size}, inclusive. * * @param index a user-supplied index identifying a position in an array, list or string * @param size the size of that array, list or string * @param desc the text to use to describe this index in an error message * @return the value of {@code index} * @throws IndexOutOfBoundsException if {@code index} is negative or is greater than {@code size} * @throws IllegalArgumentException if {@code size} is negative */ public static int checkPositionIndex(int index, int size, @Nullable String desc) { // Carefully optimized for execution by hotspot (explanatory comment above) if (index < 0 || index > size) { throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc)); } return index; }
/** * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive. * * @param index a user-supplied index identifying an element of an array, list or string * @param size the size of that array, list or string * @param desc the text to use to describe this index in an error message * @return the value of {@code index} * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size} * @throws IllegalArgumentException if {@code size} is negative */ public static int checkElementIndex(int index, int size, @Nullable String desc) { // Carefully optimized for execution by hotspot (explanatory comment above) if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(badElementIndex(index, size, desc)); } return index; }
private static String badElementIndex(int index, int size, String desc) { if (index < 0) { return format("%s (%s) must not be negative", desc, index); } else if (size < 0) { throw new IllegalArgumentException("negative size: " + size); } else { // index >= size return format("%s (%s) must be less than size (%s)", desc, index, size); } }
/** * Ensures the truth of an expression involving the state of the calling instance, but not * involving any parameters to the calling method. * <p/> * <p>See {@link #checkState(boolean, String, Object...)} for details. */ public static void checkState(boolean b, @Nullable String errorMessageTemplate, long p1) { if (!b) { throw new IllegalStateException(format(errorMessageTemplate, p1)); } }
/** * Ensures that an object reference passed as a parameter to the calling method is not null. * <p/> * <p>See {@link #checkNotNull(Object, String, Object...)} for details. */ public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, long p1) { if (obj == null) { throw new NullPointerException(format(errorMessageTemplate, p1)); } return obj; }
/** * Ensures the truth of an expression involving one or more parameters to the calling method. * <p/> * <p>See {@link #checkArgument(boolean, String, Object...)} for details. */ public static void checkArgument(boolean b, @Nullable String errorMessageTemplate, char p1) { if (!b) { throw new IllegalArgumentException(format(errorMessageTemplate, p1)); } }
/** * Ensures the truth of an expression involving one or more parameters to the calling method. * <p/> * <p>See {@link #checkArgument(boolean, String, Object...)} for details. */ public static void checkArgument( boolean b, @Nullable String errorMessageTemplate, int p1, char p2) { if (!b) { throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); } }
/** * Ensures the truth of an expression involving the state of the calling instance, but not * involving any parameters to the calling method. * <p/> * <p>See {@link #checkState(boolean, String, Object...)} for details. */ public static void checkState(boolean b, @Nullable String errorMessageTemplate, int p1) { if (!b) { throw new IllegalStateException(format(errorMessageTemplate, p1)); } }
/** * Ensures the truth of an expression involving the state of the calling instance, but not * involving any parameters to the calling method. * <p/> * <p>See {@link #checkState(boolean, String, Object...)} for details. */ public static void checkState(boolean b, @Nullable String errorMessageTemplate, long p1, int p2) { if (!b) { throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); } }
/** * Ensures that an object reference passed as a parameter to the calling method is not null. * <p/> * <p>See {@link #checkNotNull(Object, String, Object...)} for details. */ public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, int p1, long p2) { if (obj == null) { throw new NullPointerException(format(errorMessageTemplate, p1, p2)); } return obj; }
/** * Ensures that an object reference passed as a parameter to the calling method is not null. * <p/> * <p>See {@link #checkNotNull(Object, String, Object...)} for details. */ public static <T> T checkNotNull(T obj, @Nullable String errorMessageTemplate, long p1, char p2) { if (obj == null) { throw new NullPointerException(format(errorMessageTemplate, p1, p2)); } return obj; }
/** * Ensures the truth of an expression involving one or more parameters to the calling method. * <p/> * <p>See {@link #checkArgument(boolean, String, Object...)} for details. */ public static void checkArgument(boolean b, @Nullable String errorMessageTemplate, long p1) { if (!b) { throw new IllegalArgumentException(format(errorMessageTemplate, p1)); } }
/** * Ensures the truth of an expression involving one or more parameters to the calling method. * <p/> * <p>See {@link #checkArgument(boolean, String, Object...)} for details. */ public static void checkArgument( boolean b, @Nullable String errorMessageTemplate, char p1, long p2) { if (!b) { throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); } }
/** * Ensures the truth of an expression involving one or more parameters to the calling method. * <p/> * <p>See {@link #checkArgument(boolean, String, Object...)} for details. */ public static void checkArgument( boolean b, @Nullable String errorMessageTemplate, int p1, int p2) { if (!b) { throw new IllegalArgumentException(format(errorMessageTemplate, p1, p2)); } }