Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Unified Diff: base/android/java/src/org/chromium/base/Check.java

Issue 1918403004: Add Check and DCheck java classes to base (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Converted crashing mechanism to throw an AssertionError instead Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/BUILD.gn ('k') | base/android/java/src/org/chromium/base/ThreadUtils.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/Check.java
diff --git a/base/android/java/src/org/chromium/base/Check.java b/base/android/java/src/org/chromium/base/Check.java
new file mode 100644
index 0000000000000000000000000000000000000000..ac83e61ae6a091a6d566972b6ad9e9ac8d94d6fc
--- /dev/null
+++ b/base/android/java/src/org/chromium/base/Check.java
@@ -0,0 +1,1400 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.base;
+
+import android.os.Debug;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Locale;
+
+/**
+ * Makes assertions in java code and causes crashes in both Debug and Release builds. Use
+ * {@code DCheck} to only crash in Debug builds, but not in Release. The behavior of this class is
+ * intended to be similar to {@code org.junit.Assert} and {@code //base/logging.h#CHECK}
+ */
+public final class Check {
+ /**
+ * This flag will force the framework to wait for a debugger before crashing. This flag should
+ * always be checked in false, but can be useful for developers that want to turn it on. This
+ * cannot be done via a command line flag however as there is plenty of code that runs before
+ * the command line flag file is read and parsed as well as introducing a circular dependency.
+ */
+ private static final boolean WAIT_FOR_DEBUGGER = false;
+
+ private static final String TAG = "ASSERTION";
+ private static final CharSequence DEFAULT_MESSAGE = "";
+ private static final Locale LOCALE = Locale.US;
+ private static final String FORMAT_NOT_EQUAL_EXPECTED_ACTUAL =
+ "Not equal. Expected [%s], but recieved [%s]";
+ private static final String FORMAT_EQUAL_EXPECTED_ACTUAL =
+ "Unexpected equality. Expected [%s], but recieved [%s]";
+ private static final String UNEXPECTED_NULL = "Unexpected null value";
+ private static final String FORMAT_UNEXPECTED_ARRAY_LENGTH = "Unexpected array length: %d";
+ private static final String UNEXPECTED_EMPTY_ARRAY = "Unexpected empty array";
+
+ private Check() {}
+
+ // Boolean Checks
+
+ /**
+ * Asserts that the value is true or induces a crash if false or null.
+ * @param value The value to check
+ */
+ public static void isTrue(final Boolean value) {
+ isTrue(value, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the value is true or induces a crash if false or null.
+ * @param value The value to check
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isTrue(final Boolean value, final CharSequence messageFormat,
+ final Object... messageArgs) {
+ if (value == null) {
+ fail("Expected true, but was null", messageFormat, messageArgs);
+ } else if (!value) {
+ fail("Expected true, but was false", messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the value is false or induces a crash if true or null.
+ * @param value The value to check
+ */
+ public static void isFalse(final Boolean value) {
+ isFalse(value, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the value is false or induces a crash if true or null.
+ * @param value The value to check
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isFalse(final Boolean value, final CharSequence messageFormat,
+ final Object... messageArgs) {
+ if (value == null) {
+ fail("Expected false, but was null", messageFormat, messageArgs);
+ } else if (value) {
+ fail("Expected false, but was true", messageFormat, messageArgs);
+ }
+ }
+
+ // Equality Checks
+
+ /**
+ * Asserts that the Comparable's compareTo method returns 0 or that both are null. A crash is
+ * induced if one value is null, but not the other or if the compareTo call returns a non-zero
+ * value.
+ * @param comparable The comparable to object
+ * @param comparedTo The object passed into comparable's compareTo method
+ */
+ public static <T> void isEqual(final Comparable<T> comparable, final T comparedTo) {
+ isEqual(comparable, comparedTo, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the comparable's compareTo method returns 0 or that both are null. A crash is
+ * induced if one value is null, but not the other or if
+ * {@code comparable.compareTo(comparedTo)} returns a non-zero value.
+ * @param comparable The comparable to object
+ * @param comparedTo The object passed into comparable's compareTo method
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static <T> void isEqual(final Comparable<T> comparable, final T comparedTo,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (nullXor(comparable, comparedTo)) {
+ fail(stringFormat("Expected comparables but one of the arguments was null while the "
+ + "other was not. [%s] [%s]", comparable, comparedTo),
+ messageFormat, messageArgs);
+ return;
+ } else if (comparable == null && comparedTo == null) {
+ return;
+ }
+
+ final int value = comparable.compareTo(comparedTo);
+ if (value != 0) {
+ fail(stringFormat("Expected compareTo to return 0 but returned %d. [%s] [%s]",
+ value, comparable, comparedTo),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two objects are equal or both null. Induces a crash if expected is null but
+ * actual is not or if {@code expected.equals(actual)} returns false.
+ * @param expected The expected object to test against
+ * @param actual The object to be tested
+ */
+ public static void isEqual(final Object expected, final Object actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two objects are equal or both null. Induces a crash if expected is null but
+ * actual is not or if {@code expected.equals(actual)} returns false.
+ * @param expected The expected object to test against
+ * @param actual The object to be tested
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final Object expected, final Object actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!equalOrBothNull(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are deeply equal as per {@code Arrays.deepEquals}. Induces a
+ * crash if one of the arrays is null but the other is not or if the objects contained in the
+ * array are not equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final Object[] expected, final Object[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are deeply equal as per {@code Arrays.deepEquals}. Induces a
+ * crash if one of the arrays is null but the other is not or if the objects contained in the
+ * array are not equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final Object[] expected, final Object[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.deepEquals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the booleans contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final boolean[] expected, final boolean[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the booleans contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final boolean[] expected, final boolean[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the bytes contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final byte[] expected, final byte[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the bytes contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final byte[] expected, final byte[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the chars contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final char[] expected, final char[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the chars contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final char[] expected, final char[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the shorts contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final short[] expected, final short[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the shorts contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final short[] expected, final short[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the ints contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final int[] expected, final int[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the ints contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final int[] expected, final int[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the longs contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final long[] expected, final long[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the longs contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final long[] expected, final long[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the floats contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final float[] expected, final float[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the floats contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final float[] expected, final float[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the doubles contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isEqual(final double[] expected, final double[] actual) {
+ isEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are equal as per {@code Arrays.equals}. Induces a crash if one
+ * of the arrays is null but the other is not or if the doubles contained in the array are not
+ * equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEqual(final double[] expected, final double[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (!Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_NOT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the Comparable's compareTo method returns a non-zero value or that one value is
+ * null while the other is not. A crash is induced if both values are null or if
+ * {@code comparable.compareTo(comparedTo)} returns 0.
+ * @param comparable The comparable to object
+ * @param comparedTo The object passed into comparable's compareTo method
+ */
+ public static <T> void isNotEqual(final Comparable<T> comparable, final T comparedTo) {
+ isNotEqual(comparable, comparedTo, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the Comparable's compareTo method returns a non-zero value or that one value is
+ * null while the other is not. A crash is induced if both values are null or if
+ * {@code comparable.compareTo(comparedTo)} returns 0.
+ * @param comparable The comparable to object
+ * @param comparedTo The object passed into comparable's compareTo method
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static <T> void isNotEqual(final Comparable<T> comparable, final T comparedTo,
+ final CharSequence messageFormat,
+ final Object... messageArgs) {
+ if (nullXor(comparable, comparedTo)) {
+ return;
+ } else if (comparable == null && comparedTo == null) {
+ fail("Expected comparables to be not equal, but both were null",
+ messageFormat, messageArgs);
+ return;
+ }
+
+ final int value = comparable.compareTo(comparedTo);
+ if (value == 0) {
+ fail(stringFormat("Expected comparables to be not equal but were. [%s] [%s]",
+ comparable, comparedTo), messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two objects are not equal or one is null and the other is not. Induces a
+ * crash if both are null or if {@code expected.equals(actual)} returns true
+ * @param expected The expected object to test against
+ * @param actual The object to be tested
+ */
+ public static void isNotEqual(final Object expected, final Object actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two objects are not equal or one is null and the other is not. Induces a
+ * crash if both are null or if {@code expected.equals(actual)} returns true
+ * @param expected The expected object to test against
+ * @param actual The object to be tested
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final Object expected, final Object actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (equalOrBothNull(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are deeply equal as per {@code Arrays.deepEquals}. Induces a
+ * crash if one of the arrays is null but the other is not or if the objects contained in the
+ * array are not equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final Object[] expected, final Object[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are deeply equal as per {@code Arrays.deepEquals}. Induces a
+ * crash if one of the arrays is null but the other is not or if the objects contained in the
+ * array are not equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final Object[] expected, final Object[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.deepEquals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the booleans contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final boolean[] expected, final boolean[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the booleans contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final boolean[] expected, final boolean[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the bytes contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final byte[] expected, final byte[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the bytes contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final byte[] expected, final byte[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the chars contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final char[] expected, final char[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a
+ * crash if both of the arrays are null or if the chars contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final char[] expected, final char[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the shorts contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final short[] expected, final short[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the shorts contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final short[] expected, final short[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the ints contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final int[] expected, final int[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the ints contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final int[] expected, final int[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the longs contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final long[] expected, final long[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the longs contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final long[] expected, final long[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the floats contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final float[] expected, final float[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the floats contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final float[] expected, final float[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the doubles contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ */
+ public static void isNotEqual(final double[] expected, final double[] actual) {
+ isNotEqual(expected, actual, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the two arrays are not equal as per {@code Arrays.equals}. Induces a crash if
+ * both of the arrays are null or if the doubles contained in the array are equal.
+ * @param expected The expected array
+ * @param actual The array to test
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEqual(final double[] expected, final double[] actual,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (Arrays.equals(expected, actual)) {
+ fail(stringFormat(FORMAT_EQUAL_EXPECTED_ACTUAL, expected, actual),
+ messageFormat, messageArgs);
+ }
+ }
+
+ // Null Checks
+
+ /**
+ * Asserts that the value is null. Induces a crash if the value is non-null.
+ * @param value The value to check for nullness
+ */
+ public static void isNull(final Object value) {
+ isNull(value, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the value is null. Induces a crash if the value is non-null.
+ * @param value The value to check for nullness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNull(final Object value,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (value != null) {
+ fail(stringFormat("Unexpected non-null value. [%s]", value),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the value is nonnull. Induces a crash if the value is null.
+ * @param value The value to check for nullness
+ */
+ public static void isNotNull(final Object value) {
+ isNotNull(value, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the value is nonnull. Induces a crash if the value is null.
+ * @param value The value to check for nullness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotNull(final Object value,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (value == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ }
+ }
+
+ // Empty Checks
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final Object[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final Object[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final boolean[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final boolean[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final byte[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final byte[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final char[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final char[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final short[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final short[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final int[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final int[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final long[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final long[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final float[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final float[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isEmpty(final double[] arr) {
+ isEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is empty. Induces a crash if the array is not empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final double[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length != 0) {
+ fail(stringFormat(FORMAT_UNEXPECTED_ARRAY_LENGTH, arr.length),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final Object[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final Object[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final boolean[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final boolean[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final byte[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final byte[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final char[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final char[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final short[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final short[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final int[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final int[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final long[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final long[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final float[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final float[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ */
+ public static void isNotEmpty(final double[] arr) {
+ isNotEmpty(arr, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the array is not empty. Induces a crash if the array is empty.
+ * @param arr The array to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final double[] arr,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (arr == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (arr.length == 0) {
+ fail(UNEXPECTED_EMPTY_ARRAY, messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the character sequence is empty. Induces a crash if the character sequence is
+ * not empty.
+ * @param charSequence The character sequence to check for emptiness
+ */
+ public static void isEmpty(final CharSequence charSequence) {
+ isEmpty(charSequence, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the character sequence is empty. Induces a crash if the character sequence is
+ * not empty.
+ * @param charSequence The character sequence to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final CharSequence charSequence,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (charSequence == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (charSequence.length() != 0) {
+ fail(stringFormat("Unexpected non-empty charSequence. [%s]", charSequence),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the character sequence is not empty. Induces a crash if the character sequence
+ * is empty.
+ * @param charSequence The character sequence to check for emptiness
+ */
+ public static void isNotEmpty(final CharSequence charSequence) {
+ isNotEmpty(charSequence, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the character sequence is not empty. Induces a crash if the character sequence
+ * is empty.
+ * @param charSequence The character sequence to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final CharSequence charSequence,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (charSequence == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (charSequence.length() == 0) {
+ fail("Unexpected empty charSequence", messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the collection is empty. Induces a crash if the collection is not empty.
+ * @param collection The collection to check for emptiness
+ */
+ public static void isEmpty(final Collection collection) {
+ isEmpty(collection, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the collection is empty. Induces a crash if the collection is not empty.
+ * @param collection The collection to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isEmpty(final Collection collection,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (collection == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (!collection.isEmpty()) {
+ fail(stringFormat("Unexpected non-empty collection. [%s]", collection),
+ messageFormat, messageArgs);
+ }
+ }
+
+ /**
+ * Asserts that the collection is not empty. Induces a crash if the collection is empty.
+ * @param collection The collection to check for emptiness
+ */
+ public static void isNotEmpty(final Collection collection) {
+ isNotEmpty(collection, DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that the collection is not empty. Induces a crash if the collection is empty.
+ * @param collection The collection to check for emptiness
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void isNotEmpty(final Collection collection,
+ final CharSequence messageFormat, final Object... messageArgs) {
+ if (collection == null) {
+ fail(UNEXPECTED_NULL, messageFormat, messageArgs);
+ } else if (collection.isEmpty()) {
+ fail("Unexpected empty collection", messageFormat, messageArgs);
+ }
+ }
+
+ /** Special Cases **/
+
+ /**
+ * Asserts that this code was not executed. Always induces a crash. Use when a code path should
+ * never be expected to run.
+ */
+ public static void notReached() {
+ notReached(DEFAULT_MESSAGE);
+ }
+
+ /**
+ * Asserts that this code was not executed. Always induces a crash. Use when a code path should
+ * never be expected to run
+ * @param messageFormat A case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs A case-specific message to log before crashing
+ */
+ public static void notReached(final CharSequence messageFormat, final Object... messageArgs) {
+ fail("Unexpected path reached", messageFormat, messageArgs);
+ }
+
+ // Helper functions
+
+ /**
+ * The crashing method used by all Check statements. Does not call shutdown hooks and is
+ * uncatchable. Desgined not to crash during tests, but instead perform a callback. This does
+ * not use {@code //base/logging.cc} DCHECKs to allow DCHECK's before the JNI bridge is set up.
+ * @param error The framework error message
+ * @param messageFormat The case-specific message formatting string, suitable for
+ * {@code String.format()}
+ * @param messageArgs The case-specific message to log before crashing
+ */
+ private static void fail(final CharSequence error, final CharSequence messageFormat,
+ final Object... messageArgs) {
+ final String formattedMessage =
+ stringFormat("%s - %s", error, stringFormat(messageFormat, messageArgs));
+ final AssertionError assertionError = new AssertionError(formattedMessage);
+ Log.e(TAG, formattedMessage, assertionError);
+
+ // If enabled, waits a few seconds for a debugger to attach before crashing.
+ // This is useful if the crash happens during startup, before a debugger can attach.
+ if (WAIT_FOR_DEBUGGER) {
+ Debug.waitForDebugger();
+ }
+
+ throw assertionError;
+ }
+
+ /**
+ * Wrapper method for formatting strings with the default locale.
+ * @param format The format string passed into {@code String.format()}
+ * @param values The values passed into {@code String.format()}
+ * @return The built string
+ */
+ private static String stringFormat(final CharSequence format, final Object... values) {
+ return String.format(LOCALE, format.toString(), values);
+ }
+
+ /**
+ * Helper function to check if two objects are the same, both null, or {@code Object.equals()}.
+ * @param o1 The first object to check and the one that will perform {@code equals}
+ * @param o2 The second object to check and the one passed into o1's {@code equals} call
+ * @return True if {@code o1} is the same as {@code o2}, both are null, or they are equal
+ */
+ private static boolean equalOrBothNull(final Object o1, final Object o2) {
+ return o1 == o2 || (o1 != null && o1.equals(o2));
+ }
+
+ /**
+ * Helper function to perform the exclusive or logical operation on two objects nullness.
+ * @param o1 The first object
+ * @param o2 The second object
+ * @return True if either o1 or o2 is null but not both.
+ */
+ private static boolean nullXor(final Object o1, final Object o2) {
+ return (o1 == null) != (o2 == null);
+ }
+}
« no previous file with comments | « base/BUILD.gn ('k') | base/android/java/src/org/chromium/base/ThreadUtils.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698