Reusable assertions for Date s. @author Joel Costigliola @author William Delanoue
| 65 | * @author William Delanoue |
| 66 | */ |
| 67 | public class Dates { |
| 68 | |
| 69 | private static final Dates INSTANCE = new Dates(); |
| 70 | |
| 71 | /** |
| 72 | * Returns the singleton instance of this class. |
| 73 | * @return the singleton instance of this class. |
| 74 | */ |
| 75 | public static Dates instance() { |
| 76 | return INSTANCE; |
| 77 | } |
| 78 | |
| 79 | @VisibleForTesting |
| 80 | Failures failures = Failures.instance(); |
| 81 | |
| 82 | @VisibleForTesting |
| 83 | Dates() { |
| 84 | this(StandardComparisonStrategy.instance()); |
| 85 | } |
| 86 | |
| 87 | private final ComparisonStrategy comparisonStrategy; |
| 88 | |
| 89 | public Dates(ComparisonStrategy comparisonStrategy) { |
| 90 | this.comparisonStrategy = comparisonStrategy; |
| 91 | } |
| 92 | |
| 93 | @VisibleForTesting |
| 94 | public Comparator<?> getComparator() { |
| 95 | if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) { return ((ComparatorBasedComparisonStrategy) comparisonStrategy) |
| 96 | .getComparator(); } |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Verifies that the actual {@code Date} is strictly before the given one. |
| 102 | * @param info contains information about the assertion. |
| 103 | * @param actual the "actual" {@code Date}. |
| 104 | * @param other the other date to compare actual with. |
| 105 | * @throws AssertionError if {@code actual} is {@code null}. |
| 106 | * @throws NullPointerException if other {@code Date} is {@code null}. |
| 107 | * @throws AssertionError if the actual {@code Date} is not strictly before the given one. |
| 108 | */ |
| 109 | public void assertIsBefore(AssertionInfo info, Date actual, Date other) { |
| 110 | assertNotNull(info, actual); |
| 111 | dateParameterIsNotNull(other); |
| 112 | if (isBefore(actual, other)) return; |
| 113 | throw failures.failure(info, shouldBeBefore(actual, other, comparisonStrategy)); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Verifies that the actual {@code Date} is before or equal to the given one. |
| 118 | * @param info contains information about the assertion. |
| 119 | * @param actual the "actual" {@code Date}. |
| 120 | * @param other the other date to compare actual with. |
| 121 | * @throws AssertionError if {@code actual} is {@code null}. |
| 122 | * @throws NullPointerException if other {@code Date} is {@code null}. |
| 123 | * @throws AssertionError if the actual {@code Date} is not before or equal to the given one. |
| 124 | */ |