Skip to content

Allow using Range with Comparator #2571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.0.0-SNAPSHOT</version>
<version>3.0.0-GH-2571-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
61 changes: 40 additions & 21 deletions src/main/java/org/springframework/data/domain/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.domain;

import java.util.Comparator;
import java.util.Optional;

import org.springframework.util.Assert;
Expand All @@ -27,7 +28,7 @@
* @author Mark Paluch
* @since 1.10
*/
public final class Range<T extends Comparable<T>> {
public final class Range<T> {

private final static Range<?> UNBOUNDED = Range.of(Bound.unbounded(), Bound.UNBOUNDED);

Expand Down Expand Up @@ -57,7 +58,7 @@ private Range(Bound<T> lowerBound, Bound<T> upperBound) {
* @since 2.0
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<T>> Range<T> unbounded() {
public static <T> Range<T> unbounded() {
return (Range<T>) UNBOUNDED;
}

Expand All @@ -70,7 +71,7 @@ public static <T extends Comparable<T>> Range<T> unbounded() {
* @return
* @since 2.2
*/
public static <T extends Comparable<T>> Range<T> closed(T from, T to) {
public static <T> Range<T> closed(T from, T to) {
return new Range<>(Bound.inclusive(from), Bound.inclusive(to));
}

Expand All @@ -83,7 +84,7 @@ public static <T extends Comparable<T>> Range<T> closed(T from, T to) {
* @return
* @since 2.2
*/
public static <T extends Comparable<T>> Range<T> open(T from, T to) {
public static <T> Range<T> open(T from, T to) {
return new Range<>(Bound.exclusive(from), Bound.exclusive(to));
}

Expand All @@ -96,7 +97,7 @@ public static <T extends Comparable<T>> Range<T> open(T from, T to) {
* @return
* @since 2.2
*/
public static <T extends Comparable<T>> Range<T> leftOpen(T from, T to) {
public static <T> Range<T> leftOpen(T from, T to) {
return new Range<>(Bound.exclusive(from), Bound.inclusive(to));
}

Expand All @@ -109,7 +110,7 @@ public static <T extends Comparable<T>> Range<T> leftOpen(T from, T to) {
* @return
* @since 2.2
*/
public static <T extends Comparable<T>> Range<T> rightOpen(T from, T to) {
public static <T> Range<T> rightOpen(T from, T to) {
return new Range<>(Bound.inclusive(from), Bound.exclusive(to));
}

Expand All @@ -122,7 +123,7 @@ public static <T extends Comparable<T>> Range<T> rightOpen(T from, T to) {
* @return
* @since 2.2
*/
public static <T extends Comparable<T>> Range<T> leftUnbounded(Bound<T> to) {
public static <T> Range<T> leftUnbounded(Bound<T> to) {
return new Range<>(Bound.unbounded(), to);
}

Expand All @@ -135,7 +136,7 @@ public static <T extends Comparable<T>> Range<T> leftUnbounded(Bound<T> to) {
* @return
* @since 2.2
*/
public static <T extends Comparable<T>> Range<T> rightUnbounded(Bound<T> from) {
public static <T> Range<T> rightUnbounded(Bound<T> from) {
return new Range<>(from, Bound.unbounded());
}

Expand All @@ -146,7 +147,7 @@ public static <T extends Comparable<T>> Range<T> rightUnbounded(Bound<T> from) {
* @return
* @since 2.0
*/
public static <T extends Comparable<T>> RangeBuilder<T> from(Bound<T> lower) {
public static <T> RangeBuilder<T> from(Bound<T> lower) {

Assert.notNull(lower, "Lower bound must not be null!");
return new RangeBuilder<>(lower);
Expand All @@ -161,7 +162,7 @@ public static <T extends Comparable<T>> RangeBuilder<T> from(Bound<T> lower) {
* @since 2.0
* @see #from(Bound)
*/
public static <T extends Comparable<T>> Range<T> of(Bound<T> lowerBound, Bound<T> upperBound) {
public static <T> Range<T> of(Bound<T> lowerBound, Bound<T> upperBound) {
return new Range<>(lowerBound, upperBound);
}

Expand All @@ -171,9 +172,9 @@ public static <T extends Comparable<T>> Range<T> of(Bound<T> lowerBound, Bound<T
* @param <T>
* @param value must not be {@literal null}.
* @return
* @see Range#closed(Comparable, Comparable)
* @see Range#closed(Object, Object)
*/
public static <T extends Comparable<T>> Range<T> just(T value) {
public static <T> Range<T> just(T value) {
return Range.closed(value, value);
}

Expand All @@ -183,16 +184,34 @@ public static <T extends Comparable<T>> Range<T> just(T value) {
* @param value must not be {@literal null}.
* @return
*/
public boolean contains(T value) {
@SuppressWarnings({ "unchecked" })
public boolean contains(Comparable<T> value) {

return contains((T) value, (o1, o2) -> {

Assert.isInstanceOf(Comparable.class, o1,
"Range value must be an instance of Comparable to use contains(Comparable<T>)");
return ((Comparable<T>) o1).compareTo(o2);
});
}

/**
* Returns whether the {@link Range} contains the given value.
*
* @param value must not be {@literal null}.
* @return
* @since 3.0
*/
public boolean contains(T value, Comparator<T> comparator) {

Assert.notNull(value, "Reference value must not be null!");

boolean greaterThanLowerBound = lowerBound.getValue() //
.map(it -> lowerBound.isInclusive() ? it.compareTo(value) <= 0 : it.compareTo(value) < 0) //
.map(it -> lowerBound.isInclusive() ? comparator.compare(it, value) <= 0 : comparator.compare(it, value) < 0) //
.orElse(true);

boolean lessThanUpperBound = upperBound.getValue() //
.map(it -> upperBound.isInclusive() ? it.compareTo(value) >= 0 : it.compareTo(value) > 0) //
.map(it -> upperBound.isInclusive() ? comparator.compare(it, value) >= 0 : comparator.compare(it, value) > 0) //
.orElse(true);

return greaterThanLowerBound && lessThanUpperBound;
Expand Down Expand Up @@ -238,13 +257,13 @@ public int hashCode() {

/**
* Value object representing a boundary. A boundary can either be {@link #unbounded() unbounded},
* {@link #inclusive(Comparable) including its value} or {@link #exclusive(Comparable) its value}.
* {@link #inclusive(Object) including its value} or {@link #exclusive(Object) its value}.
*
* @author Mark Paluch
* @since 2.0
* @soundtrack Mohamed Ragab - Excelsior Sessions (March 2017)
*/
public static final class Bound<T extends Comparable<T>> {
public static final class Bound<T> {

@SuppressWarnings({ "rawtypes", "unchecked" }) //
private static final Bound<?> UNBOUNDED = new Bound(Optional.empty(), true);
Expand All @@ -261,7 +280,7 @@ private Bound(Optional<T> value, boolean inclusive) {
* Creates an unbounded {@link Bound}.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<T>> Bound<T> unbounded() {
public static <T> Bound<T> unbounded() {
return (Bound<T>) UNBOUNDED;
}

Expand All @@ -280,7 +299,7 @@ public boolean isBounded() {
* @param value must not be {@literal null}.
* @return
*/
public static <T extends Comparable<T>> Bound<T> inclusive(T value) {
public static <T> Bound<T> inclusive(T value) {

Assert.notNull(value, "Value must not be null!");
return new Bound<>(Optional.of(value), true);
Expand Down Expand Up @@ -332,7 +351,7 @@ public static Bound<Double> inclusive(double value) {
* @param value must not be {@literal null}.
* @return
*/
public static <T extends Comparable<T>> Bound<T> exclusive(T value) {
public static <T> Bound<T> exclusive(T value) {

Assert.notNull(value, "Value must not be null!");
return new Bound<>(Optional.of(value), false);
Expand Down Expand Up @@ -439,7 +458,7 @@ public int hashCode() {
* @since 2.0
* @soundtrack Aly and Fila - Future Sound Of Egypt 493
*/
public static class RangeBuilder<T extends Comparable<T>> {
public static class RangeBuilder<T> {

private final Bound<T> lower;

Expand Down
Loading