From e9f77d604637be0976d870272f59acecc8f1cc9d Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Thu, 14 Mar 2019 13:07:51 +0100 Subject: [PATCH] DATACMNS-1499 - Added new convenience factory methods on Range. --- .../springframework/data/domain/Range.java | 93 ++++++++++++++++++- .../data/domain/RangeUnitTests.java | 51 ++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/domain/Range.java b/src/main/java/org/springframework/data/domain/Range.java index bf0acc3a7..092a58d59 100644 --- a/src/main/java/org/springframework/data/domain/Range.java +++ b/src/main/java/org/springframework/data/domain/Range.java @@ -58,6 +58,84 @@ public class Range> { return (Range) UNBOUNDED; } + /** + * Creates a new {@link Range} with inclusive bounds for both values. + * + * @param + * @param from must not be {@literal null}. + * @param to must not be {@literal null}. + * @return + * @since 2.2 + */ + public static > Range closed(T from, T to) { + return new Range<>(Bound.inclusive(from), Bound.inclusive(to)); + } + + /** + * Creates a new {@link Range} with inclusive bounds for both values. + * + * @param + * @param from must not be {@literal null}. + * @param to must not be {@literal null}. + * @return + * @since 2.2 + */ + public static > Range open(T from, T to) { + return new Range<>(Bound.exclusive(from), Bound.exclusive(to)); + } + + /** + * Creates a new left-open {@link Range}, i.e. left exclusive, right inclusive. + * + * @param + * @param from must not be {@literal null}. + * @param to must not be {@literal null}. + * @return + * @since 2.2 + */ + public static > Range leftOpen(T from, T to) { + return new Range<>(Bound.exclusive(from), Bound.inclusive(to)); + } + + /** + * Creates a new right-open {@link Range}, i.e. left inclusive, right exclusive. + * + * @param + * @param from must not be {@literal null}. + * @param to must not be {@literal null}. + * @return + * @since 2.2 + */ + public static > Range rightOpen(T from, T to) { + return new Range<>(Bound.inclusive(from), Bound.exclusive(to)); + } + + /** + * Creates a left-unbounded {@link Range} (the left bound set to {@link Bound#unbounded()}) with the given right + * bound. + * + * @param + * @param to the right {@link Bound}, must not be {@literal null}. + * @return + * @since 2.2 + */ + public static > Range leftUnbounded(Bound to) { + return new Range<>(Bound.unbounded(), to); + } + + /** + * Creates a right-unbounded {@link Range} (the right bound set to {@link Bound#unbounded()}) with the given left + * bound. + * + * @param + * @param to the left {@link Bound}, must not be {@literal null}. + * @return + * @since 2.2 + */ + public static > Range rightUnbounded(Bound from) { + return new Range<>(from, Bound.unbounded()); + } + /** * Create a {@link RangeBuilder} given the lower {@link Bound}. * @@ -72,16 +150,29 @@ public class Range> { } /** - * Creates a new {@link Range} with the given lower and upper bound. + * Creates a new {@link Range} with the given lower and upper bound. Prefer {@link #from(Bound)} for a more builder + * style API. * * @param lowerBound must not be {@literal null}. * @param upperBound must not be {@literal null}. * @since 2.0 + * @see #from(Bound) */ public static > Range of(Bound lowerBound, Bound upperBound) { return new Range<>(lowerBound, upperBound); } + /** + * Creates a new Range with the given value as sole member. + * + * @param + * @param value must not be {@literal null}. + * @return + */ + public static > Range just(T value) { + return Range.closed(value, value); + } + /** * Returns whether the {@link Range} contains the given value. * diff --git a/src/test/java/org/springframework/data/domain/RangeUnitTests.java b/src/test/java/org/springframework/data/domain/RangeUnitTests.java index 33608208a..ecbdce452 100755 --- a/src/test/java/org/springframework/data/domain/RangeUnitTests.java +++ b/src/test/java/org/springframework/data/domain/RangeUnitTests.java @@ -156,4 +156,55 @@ public class RangeUnitTests { assertThat(range.getLowerBound().getValue()).isEmpty(); assertThat(range.getUpperBound().getValue()).isEmpty(); } + + @Test // DATACMNS-1499 + public void createsOpenRange() { + + Range range = Range.open(5L, 10L); + + assertThat(range.contains(5L)).isFalse(); + assertThat(range.contains(10L)).isFalse(); + } + + @Test // DATACMNS-1499 + public void createsClosedRange() { + + Range range = Range.closed(5L, 10L); + + assertThat(range.contains(5L)).isTrue(); + assertThat(range.contains(10L)).isTrue(); + } + + @Test // DATACMNS-1499 + public void createsLeftOpenRange() { + + Range range = Range.leftOpen(5L, 10L); + + assertThat(range.contains(5L)).isFalse(); + assertThat(range.contains(10L)).isTrue(); + } + + @Test // DATACMNS-1499 + public void createsRightOpenRange() { + + Range range = Range.rightOpen(5L, 10L); + + assertThat(range.contains(5L)).isTrue(); + assertThat(range.contains(10L)).isFalse(); + } + + @Test // DATACMNS-1499 + public void createsLeftUnboundedRange() { + assertThat(Range.leftUnbounded(Bound.inclusive(10L)).contains(-10000L)).isTrue(); + } + + @Test // DATACMNS-1499 + public void createsRightUnboundedRange() { + assertThat(Range.rightUnbounded(Bound.inclusive(10L)).contains(10000L)).isTrue(); + } + + @Test // DATACMNS-1499 + public void createsSingleValueRange() { + assertThat(Range.just(10L).contains(10L)).isTrue(); + } }