15
15
*/
16
16
package org .springframework .data .domain ;
17
17
18
+ import java .util .Comparator ;
18
19
import java .util .Optional ;
19
20
20
21
import org .springframework .util .Assert ;
27
28
* @author Mark Paluch
28
29
* @since 1.10
29
30
*/
30
- public final class Range <T extends Comparable < T > > {
31
+ public final class Range <T > {
31
32
32
33
private final static Range <?> UNBOUNDED = Range .of (Bound .unbounded (), Bound .UNBOUNDED );
33
34
@@ -57,7 +58,7 @@ private Range(Bound<T> lowerBound, Bound<T> upperBound) {
57
58
* @since 2.0
58
59
*/
59
60
@ SuppressWarnings ("unchecked" )
60
- public static <T extends Comparable < T > > Range <T > unbounded () {
61
+ public static <T > Range <T > unbounded () {
61
62
return (Range <T >) UNBOUNDED ;
62
63
}
63
64
@@ -70,7 +71,7 @@ public static <T extends Comparable<T>> Range<T> unbounded() {
70
71
* @return
71
72
* @since 2.2
72
73
*/
73
- public static <T extends Comparable < T > > Range <T > closed (T from , T to ) {
74
+ public static <T > Range <T > closed (T from , T to ) {
74
75
return new Range <>(Bound .inclusive (from ), Bound .inclusive (to ));
75
76
}
76
77
@@ -83,7 +84,7 @@ public static <T extends Comparable<T>> Range<T> closed(T from, T to) {
83
84
* @return
84
85
* @since 2.2
85
86
*/
86
- public static <T extends Comparable < T > > Range <T > open (T from , T to ) {
87
+ public static <T > Range <T > open (T from , T to ) {
87
88
return new Range <>(Bound .exclusive (from ), Bound .exclusive (to ));
88
89
}
89
90
@@ -96,7 +97,7 @@ public static <T extends Comparable<T>> Range<T> open(T from, T to) {
96
97
* @return
97
98
* @since 2.2
98
99
*/
99
- public static <T extends Comparable < T > > Range <T > leftOpen (T from , T to ) {
100
+ public static <T > Range <T > leftOpen (T from , T to ) {
100
101
return new Range <>(Bound .exclusive (from ), Bound .inclusive (to ));
101
102
}
102
103
@@ -109,7 +110,7 @@ public static <T extends Comparable<T>> Range<T> leftOpen(T from, T to) {
109
110
* @return
110
111
* @since 2.2
111
112
*/
112
- public static <T extends Comparable < T > > Range <T > rightOpen (T from , T to ) {
113
+ public static <T > Range <T > rightOpen (T from , T to ) {
113
114
return new Range <>(Bound .inclusive (from ), Bound .exclusive (to ));
114
115
}
115
116
@@ -122,7 +123,7 @@ public static <T extends Comparable<T>> Range<T> rightOpen(T from, T to) {
122
123
* @return
123
124
* @since 2.2
124
125
*/
125
- public static <T extends Comparable < T > > Range <T > leftUnbounded (Bound <T > to ) {
126
+ public static <T > Range <T > leftUnbounded (Bound <T > to ) {
126
127
return new Range <>(Bound .unbounded (), to );
127
128
}
128
129
@@ -135,7 +136,7 @@ public static <T extends Comparable<T>> Range<T> leftUnbounded(Bound<T> to) {
135
136
* @return
136
137
* @since 2.2
137
138
*/
138
- public static <T extends Comparable < T > > Range <T > rightUnbounded (Bound <T > from ) {
139
+ public static <T > Range <T > rightUnbounded (Bound <T > from ) {
139
140
return new Range <>(from , Bound .unbounded ());
140
141
}
141
142
@@ -146,7 +147,7 @@ public static <T extends Comparable<T>> Range<T> rightUnbounded(Bound<T> from) {
146
147
* @return
147
148
* @since 2.0
148
149
*/
149
- public static <T extends Comparable < T > > RangeBuilder <T > from (Bound <T > lower ) {
150
+ public static <T > RangeBuilder <T > from (Bound <T > lower ) {
150
151
151
152
Assert .notNull (lower , "Lower bound must not be null!" );
152
153
return new RangeBuilder <>(lower );
@@ -161,7 +162,7 @@ public static <T extends Comparable<T>> RangeBuilder<T> from(Bound<T> lower) {
161
162
* @since 2.0
162
163
* @see #from(Bound)
163
164
*/
164
- public static <T extends Comparable < T > > Range <T > of (Bound <T > lowerBound , Bound <T > upperBound ) {
165
+ public static <T > Range <T > of (Bound <T > lowerBound , Bound <T > upperBound ) {
165
166
return new Range <>(lowerBound , upperBound );
166
167
}
167
168
@@ -171,9 +172,9 @@ public static <T extends Comparable<T>> Range<T> of(Bound<T> lowerBound, Bound<T
171
172
* @param <T>
172
173
* @param value must not be {@literal null}.
173
174
* @return
174
- * @see Range#closed(Comparable, Comparable )
175
+ * @see Range#closed(Object, Object )
175
176
*/
176
- public static <T extends Comparable < T > > Range <T > just (T value ) {
177
+ public static <T > Range <T > just (T value ) {
177
178
return Range .closed (value , value );
178
179
}
179
180
@@ -183,16 +184,34 @@ public static <T extends Comparable<T>> Range<T> just(T value) {
183
184
* @param value must not be {@literal null}.
184
185
* @return
185
186
*/
186
- public boolean contains (T value ) {
187
+ @ SuppressWarnings ({ "unchecked" })
188
+ public boolean contains (Comparable <T > value ) {
189
+
190
+ return contains ((T ) value , (o1 , o2 ) -> {
191
+
192
+ Assert .isInstanceOf (Comparable .class , o1 ,
193
+ "Range value must be an instance of Comparable to use contains(Comparable<T>)" );
194
+ return ((Comparable <T >) o1 ).compareTo (o2 );
195
+ });
196
+ }
197
+
198
+ /**
199
+ * Returns whether the {@link Range} contains the given value.
200
+ *
201
+ * @param value must not be {@literal null}.
202
+ * @return
203
+ * @since 3.0
204
+ */
205
+ public boolean contains (T value , Comparator <T > comparator ) {
187
206
188
207
Assert .notNull (value , "Reference value must not be null!" );
189
208
190
209
boolean greaterThanLowerBound = lowerBound .getValue () //
191
- .map (it -> lowerBound .isInclusive () ? it . compareTo ( value ) <= 0 : it . compareTo ( value ) < 0 ) //
210
+ .map (it -> lowerBound .isInclusive () ? comparator . compare ( it , value ) <= 0 : comparator . compare ( it , value ) < 0 ) //
192
211
.orElse (true );
193
212
194
213
boolean lessThanUpperBound = upperBound .getValue () //
195
- .map (it -> upperBound .isInclusive () ? it . compareTo ( value ) >= 0 : it . compareTo ( value ) > 0 ) //
214
+ .map (it -> upperBound .isInclusive () ? comparator . compare ( it , value ) >= 0 : comparator . compare ( it , value ) > 0 ) //
196
215
.orElse (true );
197
216
198
217
return greaterThanLowerBound && lessThanUpperBound ;
@@ -238,13 +257,13 @@ public int hashCode() {
238
257
239
258
/**
240
259
* Value object representing a boundary. A boundary can either be {@link #unbounded() unbounded},
241
- * {@link #inclusive(Comparable ) including its value} or {@link #exclusive(Comparable ) its value}.
260
+ * {@link #inclusive(Object ) including its value} or {@link #exclusive(Object ) its value}.
242
261
*
243
262
* @author Mark Paluch
244
263
* @since 2.0
245
264
* @soundtrack Mohamed Ragab - Excelsior Sessions (March 2017)
246
265
*/
247
- public static final class Bound <T extends Comparable < T > > {
266
+ public static final class Bound <T > {
248
267
249
268
@ SuppressWarnings ({ "rawtypes" , "unchecked" }) //
250
269
private static final Bound <?> UNBOUNDED = new Bound (Optional .empty (), true );
@@ -261,7 +280,7 @@ private Bound(Optional<T> value, boolean inclusive) {
261
280
* Creates an unbounded {@link Bound}.
262
281
*/
263
282
@ SuppressWarnings ("unchecked" )
264
- public static <T extends Comparable < T > > Bound <T > unbounded () {
283
+ public static <T > Bound <T > unbounded () {
265
284
return (Bound <T >) UNBOUNDED ;
266
285
}
267
286
@@ -280,7 +299,7 @@ public boolean isBounded() {
280
299
* @param value must not be {@literal null}.
281
300
* @return
282
301
*/
283
- public static <T extends Comparable < T > > Bound <T > inclusive (T value ) {
302
+ public static <T > Bound <T > inclusive (T value ) {
284
303
285
304
Assert .notNull (value , "Value must not be null!" );
286
305
return new Bound <>(Optional .of (value ), true );
@@ -332,7 +351,7 @@ public static Bound<Double> inclusive(double value) {
332
351
* @param value must not be {@literal null}.
333
352
* @return
334
353
*/
335
- public static <T extends Comparable < T > > Bound <T > exclusive (T value ) {
354
+ public static <T > Bound <T > exclusive (T value ) {
336
355
337
356
Assert .notNull (value , "Value must not be null!" );
338
357
return new Bound <>(Optional .of (value ), false );
@@ -439,7 +458,7 @@ public int hashCode() {
439
458
* @since 2.0
440
459
* @soundtrack Aly and Fila - Future Sound Of Egypt 493
441
460
*/
442
- public static class RangeBuilder <T extends Comparable < T > > {
461
+ public static class RangeBuilder <T > {
443
462
444
463
private final Bound <T > lower ;
445
464
0 commit comments