From eb54882ffadf59c466c350ad0a546ca4b16e46d3 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 1 Jan 2020 08:19:07 -0500 Subject: [PATCH] [LANG-1507] Add ComparableUtils #398. Clean ups. --- .../lang3/compare/ComparableUtils.java | 280 ++++++------- .../lang3/compare/ComparableUtilsTest.java | 376 +++++++++--------- 2 files changed, 328 insertions(+), 328 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/compare/ComparableUtils.java b/src/main/java/org/apache/commons/lang3/compare/ComparableUtils.java index b7227cb29..4312af7ef 100644 --- a/src/main/java/org/apache/commons/lang3/compare/ComparableUtils.java +++ b/src/main/java/org/apache/commons/lang3/compare/ComparableUtils.java @@ -29,63 +29,96 @@ import java.util.function.Predicate; */ public class ComparableUtils { - private ComparableUtils() {} - /** * Provides access to the available methods - * - * @param a base object in the further comparison - * @param type of the base object - * @return a builder object with further methods */ - public static > ComparableCheckBuilder is(A a) { - return new ComparableCheckBuilder<>(a); - } + public static class ComparableCheckBuilder> { - /** - * Checks if the tested object is less than {@code b} - * - * @param b the object to compare to the tested object - * @param type of the test object - * @return a predicate for true if the value returned by {@link Comparable#compareTo} is less than {@code 0} - */ - public static > Predicate lt(A b) { - return a -> is(a).lessThan(b); - } + private final A a; - /** - * Checks if the tested object is less than or equal to {@code b} - * - * @param b the object to compare to the tested object - * @param type of the test object - * @return a predicate for true if the value returned by {@link Comparable#compareTo} - * is less than or equal to {@code 0} - */ - public static > Predicate le(A b) { - return a -> is(a).lessThanOrEqualTo(b); - } + private ComparableCheckBuilder(final A a) { + this.a = a; + } - /** - * Checks if the tested object is greater than {@code b} - * - * @param b the object to compare to the tested object - * @param type of the test object - * @return a predicate for true if the value returned by {@link Comparable#compareTo} is greater than {@code 0} - */ - public static > Predicate gt(A b) { - return a -> is(a).greaterThan(b); - } + /** + * Checks if {@code [b <= a <= c]} or {@code [b >= a >= c]} where the {@code a} is object passed to {@link #is}. + * + * @param b the object to compare to the base object + * @param c the object to compare to the base object + * @return true if the base object is between b and c + */ + public boolean between(final A b, final A c) { + return betweenOrdered(b, c) || betweenOrdered(c, b); + } - /** - * Checks if the tested object is greater than or equal to {@code b} - * - * @param b the object to compare to the tested object - * @param type of the test object - * @return a predicate for true if the value returned by {@link Comparable#compareTo} - * is greater than or equal to {@code 0} - */ - public static > Predicate ge(A b) { - return a -> is(a).greaterThanOrEqualTo(b); + /** + * Checks if {@code (b < a < c)} or {@code (b > a > c)} where the {@code a} is object passed to {@link #is}. + * + * @param b the object to compare to the base object + * @param c the object to compare to the base object + * @return true if the base object is between b and c and not equal to those + */ + public boolean betweenExclusive(final A b, final A c) { + return betweenOrderedExclusive(b, c) || betweenOrderedExclusive(c, b); + } + + private boolean betweenOrdered(final A b, final A c) { + return greaterThanOrEqualTo(b) && lessThanOrEqualTo(c); + } + + private boolean betweenOrderedExclusive(final A b, final A c) { + return greaterThan(b) && lessThan(c); + } + + /** + * Checks if the object passed to {@link #is} is equal to {@code b} + * + * @param b the object to compare to the base object + * @return true if the value returned by {@link Comparable#compareTo} is equal to {@code 0} + */ + public boolean equalTo(final A b) { + return a.compareTo(b) == 0; + } + + /** + * Checks if the object passed to {@link #is} is greater than {@code b} + * + * @param b the object to compare to the base object + * @return true if the value returned by {@link Comparable#compareTo} is greater than {@code 0} + */ + public boolean greaterThan(final A b) { + return a.compareTo(b) > 0; + } + + /** + * Checks if the object passed to {@link #is} is greater than or equal to {@code b} + * + * @param b the object to compare to the base object + * @return true if the value returned by {@link Comparable#compareTo} is greater than or equal to {@code 0} + */ + public boolean greaterThanOrEqualTo(final A b) { + return a.compareTo(b) >= 0; + } + + /** + * Checks if the object passed to {@link #is} is less than {@code b} + * + * @param b the object to compare to the base object + * @return true if the value returned by {@link Comparable#compareTo} is less than {@code 0} + */ + public boolean lessThan(final A b) { + return a.compareTo(b) < 0; + } + + /** + * Checks if the object passed to {@link #is} is less than or equal to {@code b} + * + * @param b the object to compare to the base object + * @return true if the value returned by {@link Comparable#compareTo} is less than or equal to {@code 0} + */ + public boolean lessThanOrEqualTo(final A b) { + return a.compareTo(b) <= 0; + } } /** @@ -96,7 +129,7 @@ public class ComparableUtils { * @param type of the test object * @return a predicate for true if the tested object is between b and c */ - public static > Predicate between(A b, A c) { + public static > Predicate between(final A b, final A c) { return a -> is(a).between(b, c); } @@ -108,99 +141,66 @@ public class ComparableUtils { * @param type of the test object * @return a predicate for true if the tested object is between b and c and not equal to those */ - public static > Predicate betweenExclusive(A b, A c) { + public static > Predicate betweenExclusive(final A b, final A c) { return a -> is(a).betweenExclusive(b, c); } /** - * Provides access to the available methods + * Checks if the tested object is greater than or equal to {@code b} + * + * @param b the object to compare to the tested object + * @param type of the test object + * @return a predicate for true if the value returned by {@link Comparable#compareTo} + * is greater than or equal to {@code 0} */ - public static class ComparableCheckBuilder> { - - private final A a; - - private ComparableCheckBuilder(A a) { - this.a = a; - } - - /** - * Checks if the object passed to {@link #is} is equal to {@code b} - * - * @param b the object to compare to the base object - * @return true if the value returned by {@link Comparable#compareTo} is equal to {@code 0} - */ - public boolean equalTo(A b) { - return a.compareTo(b) == 0; - } - - /** - * Checks if the object passed to {@link #is} is less than {@code b} - * - * @param b the object to compare to the base object - * @return true if the value returned by {@link Comparable#compareTo} is less than {@code 0} - */ - public boolean lessThan(A b) { - return a.compareTo(b) < 0; - } - - /** - * Checks if the object passed to {@link #is} is less than or equal to {@code b} - * - * @param b the object to compare to the base object - * @return true if the value returned by {@link Comparable#compareTo} is less than or equal to {@code 0} - */ - public boolean lessThanOrEqualTo(A b) { - return a.compareTo(b) <= 0; - } - - /** - * Checks if the object passed to {@link #is} is greater than {@code b} - * - * @param b the object to compare to the base object - * @return true if the value returned by {@link Comparable#compareTo} is greater than {@code 0} - */ - public boolean greaterThan(A b) { - return a.compareTo(b) > 0; - } - - /** - * Checks if the object passed to {@link #is} is greater than or equal to {@code b} - * - * @param b the object to compare to the base object - * @return true if the value returned by {@link Comparable#compareTo} is greater than or equal to {@code 0} - */ - public boolean greaterThanOrEqualTo(A b) { - return a.compareTo(b) >= 0; - } - - /** - * Checks if {@code [b <= a <= c]} or {@code [b >= a >= c]} where the {@code a} is object passed to {@link #is}. - * - * @param b the object to compare to the base object - * @param c the object to compare to the base object - * @return true if the base object is between b and c - */ - public boolean between(A b, A c) { - return betweenOrdered(b, c) || betweenOrdered(c, b); - } - - /** - * Checks if {@code (b < a < c)} or {@code (b > a > c)} where the {@code a} is object passed to {@link #is}. - * - * @param b the object to compare to the base object - * @param c the object to compare to the base object - * @return true if the base object is between b and c and not equal to those - */ - public boolean betweenExclusive(A b, A c) { - return betweenOrderedExclusive(b, c) || betweenOrderedExclusive(c, b); - } - - private boolean betweenOrdered(A b, A c) { - return greaterThanOrEqualTo(b) && lessThanOrEqualTo(c); - } - - private boolean betweenOrderedExclusive(A b, A c) { - return greaterThan(b) && lessThan(c); - } + public static > Predicate ge(final A b) { + return a -> is(a).greaterThanOrEqualTo(b); } + + /** + * Checks if the tested object is greater than {@code b} + * + * @param b the object to compare to the tested object + * @param type of the test object + * @return a predicate for true if the value returned by {@link Comparable#compareTo} is greater than {@code 0} + */ + public static > Predicate gt(final A b) { + return a -> is(a).greaterThan(b); + } + + /** + * Provides access to the available methods + * + * @param a base object in the further comparison + * @param type of the base object + * @return a builder object with further methods + */ + public static > ComparableCheckBuilder is(final A a) { + return new ComparableCheckBuilder<>(a); + } + + /** + * Checks if the tested object is less than or equal to {@code b} + * + * @param b the object to compare to the tested object + * @param type of the test object + * @return a predicate for true if the value returned by {@link Comparable#compareTo} + * is less than or equal to {@code 0} + */ + public static > Predicate le(final A b) { + return a -> is(a).lessThanOrEqualTo(b); + } + + /** + * Checks if the tested object is less than {@code b} + * + * @param b the object to compare to the tested object + * @param type of the test object + * @return a predicate for true if the value returned by {@link Comparable#compareTo} is less than {@code 0} + */ + public static > Predicate lt(final A b) { + return a -> is(a).lessThan(b); + } + + private ComparableUtils() {} } diff --git a/src/test/java/org/apache/commons/lang3/compare/ComparableUtilsTest.java b/src/test/java/org/apache/commons/lang3/compare/ComparableUtilsTest.java index 418b7bbce..2b391b66a 100644 --- a/src/test/java/org/apache/commons/lang3/compare/ComparableUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/compare/ComparableUtilsTest.java @@ -16,163 +16,34 @@ */ package org.apache.commons.lang3.compare; +import static org.apache.commons.lang3.compare.ComparableUtils.is; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.math.BigDecimal; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import java.math.BigDecimal; - -import static org.apache.commons.lang3.compare.ComparableUtils.is; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) public class ComparableUtilsTest { @Nested class A_is_1 { - BigDecimal a = BigDecimal.ONE; - - @DisplayName("B is 1 (B = A)") + @DisplayName("B is 0 (B < A)") @Nested - class B_is_1 { + class B_is_0 { - BigDecimal b = BigDecimal.ONE; - - @Test - void equalTo_returns_true() { - assertTrue(is(a).equalTo(b)); - } - - @Test - void lessThan_returns_false() { - assertFalse(is(a).lessThan(b)); - } - - @Test - void lessThanOrEqualTo_returns_true() { - assertTrue(is(a).lessThanOrEqualTo(b)); - } - - @Test - void greaterThan_returns_false() { - assertFalse(is(a).greaterThan(b)); - } - - @Test - void greaterThanOrEqualTo_returns_true() { - assertTrue(is(a).greaterThanOrEqualTo(b)); - } - - @DisplayName("C is 1 (B = A = C)") - @Nested - class C_is_1 { - - BigDecimal c = BigDecimal.ONE; - - @Test - void between_returns_true() { - assertTrue(is(a).between(b, c)); - } - - @Test - void betweenExclusive_returns_false() { - assertFalse(is(a).betweenExclusive(b, c)); - } - } - - @DisplayName("C is 10 (B = A < C)") - @Nested - class C_is_10 { - - BigDecimal c = BigDecimal.TEN; - - @Test - void between_returns_true() { - assertTrue(is(a).between(b, c)); - } - - @Test - void betweenExclusive_returns_false() { - assertFalse(is(a).betweenExclusive(b, c)); - } - } - - @DisplayName("C is 0 (B = A > C)") + @DisplayName("C is 0 ([B=C] < A)") @Nested class C_is_0 { BigDecimal c = BigDecimal.ZERO; - @Test - void between_returns_true() { - assertTrue(is(a).between(b, c)); - } - - @Test - void betweenExclusive_returns_false() { - assertFalse(is(a).betweenExclusive(b, c)); - } - } - } - - @DisplayName("B is 10 (B > A)") - @Nested - class B_is_10 { - - BigDecimal b = BigDecimal.TEN; - - @Test - void equalTo_returns_false() { - assertFalse(is(a).equalTo(b)); - } - - @Test - void lessThan_returns_true() { - assertTrue(is(a).lessThan(b)); - } - - @Test - void lessThanOrEqualTo_returns_true() { - assertTrue(is(a).lessThanOrEqualTo(b)); - } - - @Test - void greaterThan_returns_false() { - assertFalse(is(a).greaterThan(b)); - } - - @Test - void greaterThanOrEqualTo_returns_false() { - assertFalse(is(a).greaterThanOrEqualTo(b)); - } - - @DisplayName("C is 1 (B > A = C)") - @Nested - class C_is_1 { - - BigDecimal c = BigDecimal.ONE; - - @Test - void between_returns_true() { - assertTrue(is(a).between(b, c)); - } - - @Test - void betweenExclusive_returns_false() { - assertFalse(is(a).betweenExclusive(b, c)); - } - } - - @DisplayName("C is 10 ([B,C] > A)") - @Nested - class C_is_10 { - - BigDecimal c = BigDecimal.TEN; - @Test void between_returns_false() { assertFalse(is(a).between(b, c)); @@ -184,55 +55,6 @@ public class ComparableUtilsTest { } } - @DisplayName("C is 0 (B > A > C)") - @Nested - class C_is_0 { - - BigDecimal c = BigDecimal.ZERO; - - @Test - void between_returns_true() { - assertTrue(is(a).between(b, c)); - } - - @Test - void betweenExclusive_returns_true() { - assertTrue(is(a).betweenExclusive(b, c)); - } - } - } - - @DisplayName("B is 0 (B < A)") - @Nested - class B_is_0 { - - BigDecimal b = BigDecimal.ZERO; - - @Test - void equalTo_returns_false() { - assertFalse(is(a).equalTo(b)); - } - - @Test - void lessThan_returns_false() { - assertFalse(is(a).lessThan(b)); - } - - @Test - void lessThanOrEqualTo_returns_false() { - assertFalse(is(a).lessThanOrEqualTo(b)); - } - - @Test - void greaterThan_returns_true() { - assertTrue(is(a).greaterThan(b)); - } - - @Test - void greaterThanOrEqualTo_returns_true() { - assertTrue(is(a).greaterThanOrEqualTo(b)); - } - @DisplayName("C is 1 (B < A = C)") @Nested class C_is_1 { @@ -267,12 +89,161 @@ public class ComparableUtilsTest { } } - @DisplayName("C is 0 ([B=C] < A)") + BigDecimal b = BigDecimal.ZERO; + + @Test + void equalTo_returns_false() { + assertFalse(is(a).equalTo(b)); + } + + @Test + void greaterThan_returns_true() { + assertTrue(is(a).greaterThan(b)); + } + + @Test + void greaterThanOrEqualTo_returns_true() { + assertTrue(is(a).greaterThanOrEqualTo(b)); + } + + @Test + void lessThan_returns_false() { + assertFalse(is(a).lessThan(b)); + } + + @Test + void lessThanOrEqualTo_returns_false() { + assertFalse(is(a).lessThanOrEqualTo(b)); + } + } + + @DisplayName("B is 1 (B = A)") + @Nested + class B_is_1 { + + @DisplayName("C is 0 (B = A > C)") @Nested class C_is_0 { BigDecimal c = BigDecimal.ZERO; + @Test + void between_returns_true() { + assertTrue(is(a).between(b, c)); + } + + @Test + void betweenExclusive_returns_false() { + assertFalse(is(a).betweenExclusive(b, c)); + } + } + + @DisplayName("C is 1 (B = A = C)") + @Nested + class C_is_1 { + + BigDecimal c = BigDecimal.ONE; + + @Test + void between_returns_true() { + assertTrue(is(a).between(b, c)); + } + + @Test + void betweenExclusive_returns_false() { + assertFalse(is(a).betweenExclusive(b, c)); + } + } + + @DisplayName("C is 10 (B = A < C)") + @Nested + class C_is_10 { + + BigDecimal c = BigDecimal.TEN; + + @Test + void between_returns_true() { + assertTrue(is(a).between(b, c)); + } + + @Test + void betweenExclusive_returns_false() { + assertFalse(is(a).betweenExclusive(b, c)); + } + } + + BigDecimal b = BigDecimal.ONE; + + @Test + void equalTo_returns_true() { + assertTrue(is(a).equalTo(b)); + } + + @Test + void greaterThan_returns_false() { + assertFalse(is(a).greaterThan(b)); + } + + @Test + void greaterThanOrEqualTo_returns_true() { + assertTrue(is(a).greaterThanOrEqualTo(b)); + } + + @Test + void lessThan_returns_false() { + assertFalse(is(a).lessThan(b)); + } + + @Test + void lessThanOrEqualTo_returns_true() { + assertTrue(is(a).lessThanOrEqualTo(b)); + } + } + + @DisplayName("B is 10 (B > A)") + @Nested + class B_is_10 { + + @DisplayName("C is 0 (B > A > C)") + @Nested + class C_is_0 { + + BigDecimal c = BigDecimal.ZERO; + + @Test + void between_returns_true() { + assertTrue(is(a).between(b, c)); + } + + @Test + void betweenExclusive_returns_true() { + assertTrue(is(a).betweenExclusive(b, c)); + } + } + + @DisplayName("C is 1 (B > A = C)") + @Nested + class C_is_1 { + + BigDecimal c = BigDecimal.ONE; + + @Test + void between_returns_true() { + assertTrue(is(a).between(b, c)); + } + + @Test + void betweenExclusive_returns_false() { + assertFalse(is(a).betweenExclusive(b, c)); + } + } + + @DisplayName("C is 10 ([B,C] > A)") + @Nested + class C_is_10 { + + BigDecimal c = BigDecimal.TEN; + @Test void between_returns_false() { assertFalse(is(a).between(b, c)); @@ -283,6 +254,35 @@ public class ComparableUtilsTest { assertFalse(is(a).betweenExclusive(b, c)); } } + + BigDecimal b = BigDecimal.TEN; + + @Test + void equalTo_returns_false() { + assertFalse(is(a).equalTo(b)); + } + + @Test + void greaterThan_returns_false() { + assertFalse(is(a).greaterThan(b)); + } + + @Test + void greaterThanOrEqualTo_returns_false() { + assertFalse(is(a).greaterThanOrEqualTo(b)); + } + + @Test + void lessThan_returns_true() { + assertTrue(is(a).lessThan(b)); + } + + @Test + void lessThanOrEqualTo_returns_true() { + assertTrue(is(a).lessThanOrEqualTo(b)); + } } + + BigDecimal a = BigDecimal.ONE; } }