Verifies that a value satisfies a Condition . @author Alex Ruiz
| 28 | * @author Alex Ruiz |
| 29 | */ |
| 30 | public class Conditions { |
| 31 | |
| 32 | private static final Conditions INSTANCE = new Conditions(); |
| 33 | |
| 34 | /** |
| 35 | * Returns the singleton instance of this class. |
| 36 | * @return the singleton instance of this class. |
| 37 | */ |
| 38 | public static Conditions instance() { |
| 39 | return INSTANCE; |
| 40 | } |
| 41 | |
| 42 | @VisibleForTesting |
| 43 | Failures failures = Failures.instance(); |
| 44 | |
| 45 | @VisibleForTesting |
| 46 | Conditions() {} |
| 47 | |
| 48 | /** |
| 49 | * Asserts that the actual value satisfies the given <code>{@link Condition}</code>. |
| 50 | * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. |
| 51 | * @param info contains information about the assertion. |
| 52 | * @param actual the actual value. |
| 53 | * @param condition the given {@code Condition}. |
| 54 | * @throws NullPointerException if the given {@code Condition} is {@code null}. |
| 55 | * @throws AssertionError if the actual value does not satisfy the given {@code Condition}. |
| 56 | */ |
| 57 | public <T> void assertIs(AssertionInfo info, T actual, Condition<? super T> condition) { |
| 58 | assertIsNotNull(condition); |
| 59 | if (condition.matches(actual)) return; |
| 60 | throw failures.failure(info, shouldBe(actual, condition)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Asserts that the actual value does not satisfy the given <code>{@link Condition}</code>. |
| 65 | * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. |
| 66 | * @param info contains information about the assertion. |
| 67 | * @param actual the actual value. |
| 68 | * @param condition the given {@code Condition}. |
| 69 | * @throws NullPointerException if the given {@code Condition} is {@code null}. |
| 70 | * @throws AssertionError if the actual value satisfies the given {@code Condition}. |
| 71 | */ |
| 72 | public <T> void assertIsNot(AssertionInfo info, T actual, Condition<? super T> condition) { |
| 73 | assertIsNotNull(condition); |
| 74 | if (!condition.matches(actual)) return; |
| 75 | throw failures.failure(info, shouldNotBe(actual, condition)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Asserts that the actual value satisfies the given <code>{@link Condition}</code>. |
| 80 | * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. |
| 81 | * @param info contains information about the assertion. |
| 82 | * @param actual the actual value. |
| 83 | * @param condition the given {@code Condition}. |
| 84 | * @throws NullPointerException if the given {@code Condition} is {@code null}. |
| 85 | * @throws AssertionError if the actual value does not satisfy the given {@code Condition}. |
| 86 | */ |
| 87 | public <T> void assertHas(AssertionInfo info, T actual, Condition<? super T> condition) { |