Reusable assertions for Map s. @author Alex Ruiz @author Nicolas François @author dorzey
| 49 | * @author dorzey |
| 50 | */ |
| 51 | public class Maps { |
| 52 | |
| 53 | private static final Maps INSTANCE = new Maps(); |
| 54 | |
| 55 | /** |
| 56 | * Returns the singleton instance of this class. |
| 57 | * |
| 58 | * @return the singleton instance of this class. |
| 59 | */ |
| 60 | public static Maps instance() { |
| 61 | return INSTANCE; |
| 62 | } |
| 63 | |
| 64 | @VisibleForTesting |
| 65 | Failures failures = Failures.instance(); |
| 66 | |
| 67 | @VisibleForTesting |
| 68 | Maps() { |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Asserts that the given {@code Map} is {@code null} or empty. |
| 73 | * |
| 74 | * @param info contains information about the assertion. |
| 75 | * @param actual the given map. |
| 76 | * @throws AssertionError if the given {@code Map} is not {@code null} *and* contains one or more entries. |
| 77 | */ |
| 78 | public void assertNullOrEmpty(AssertionInfo info, Map<?, ?> actual) { |
| 79 | if (actual == null || actual.isEmpty()) { |
| 80 | return; |
| 81 | } |
| 82 | throw failures.failure(info, shouldBeNullOrEmpty(actual)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Asserts that the given {@code Map} is empty. |
| 87 | * |
| 88 | * @param info contains information about the assertion. |
| 89 | * @param actual the given {@code Map}. |
| 90 | * @throws AssertionError if the given {@code Map} is {@code null}. |
| 91 | * @throws AssertionError if the given {@code Map} is not empty. |
| 92 | */ |
| 93 | public void assertEmpty(AssertionInfo info, Map<?, ?> actual) { |
| 94 | assertNotNull(info, actual); |
| 95 | if (actual.isEmpty()) { |
| 96 | return; |
| 97 | } |
| 98 | throw failures.failure(info, shouldBeEmpty(actual)); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Asserts that the given {@code Map} is not empty. |
| 103 | * |
| 104 | * @param info contains information about the assertion. |
| 105 | * @param actual the given {@code Map}. |
| 106 | * @throws AssertionError if the given {@code Map} is {@code null}. |
| 107 | * @throws AssertionError if the given {@code Map} is empty. |
| 108 | */ |