22
22
* @param <U> The type of the value being restricted
23
23
*
24
24
* @see org.hibernate.query.restriction.Restriction
25
+ * @see org.hibernate.query.restriction.Restriction#restrict(jakarta.persistence.metamodel.SingularAttribute, Range)
25
26
*
26
27
* @author Gavin King
27
28
*
30
31
@ Incubating
31
32
public interface Range <U > {
32
33
34
+ /**
35
+ * The Java class of the values belonging to this range.
36
+ */
33
37
Class <? extends U > getType ();
34
38
35
39
/**
@@ -40,92 +44,189 @@ public interface Range<U> {
40
44
@ Internal
41
45
Predicate toPredicate (Path <? extends U > path , CriteriaBuilder builder );
42
46
47
+ /**
48
+ * A range containing a single value.
49
+ */
43
50
static <U > Range <U > singleValue (U value ) {
44
51
return new Value <>( value );
45
52
}
46
53
54
+ /**
55
+ * A range containing all strings which are equal to the given string,
56
+ * ignoring case.
57
+ */
47
58
static Range <String > singleCaseInsensitiveValue (String value ) {
48
59
return new CaseInsensitiveValue ( value );
49
60
}
50
61
62
+ /**
63
+ * A range containing all values belonging to the given list.
64
+ */
51
65
static <U > Range <U > valueList (List <U > values ) {
52
66
return new ValueList <>( values );
53
67
}
54
68
69
+ /**
70
+ * A range containing all values strictly greater than the given
71
+ * lower bound.
72
+ */
55
73
static <U extends Comparable <U >> Range <U > greaterThan (U bound ) {
56
74
return new LowerBound <>( bound , true );
57
75
}
58
76
77
+ /**
78
+ * A range containing all values greater than or equal to the given
79
+ * lower bound.
80
+ */
59
81
static <U extends Comparable <U >> Range <U > greaterThanOrEqualTo (U bound ) {
60
82
return new LowerBound <>( bound , false );
61
83
}
62
84
85
+ /**
86
+ * A range containing all values strictly less than the given
87
+ * upper bound.
88
+ */
63
89
static <U extends Comparable <U >> Range <U > lessThan (U bound ) {
64
90
return new UpperBound <>( bound , true );
65
91
}
66
92
93
+ /**
94
+ * A range containing all values less than or equal to the given
95
+ * upper bound.
96
+ */
67
97
static <U extends Comparable <U >> Range <U > lessThanOrEqualTo (U bound ) {
68
98
return new UpperBound <>( bound , false );
69
99
}
70
100
101
+ /**
102
+ * An open range containing all values strictly greater than the
103
+ * given lower bound, and strictly less than the given upper bound.
104
+ */
71
105
static <U extends Comparable <U >> Range <U > open (U lowerBound , U upperBound ) {
72
106
return new Interval <>( new LowerBound <>( lowerBound , true ),
73
107
new UpperBound <>( upperBound , true ) );
74
108
}
75
109
110
+ /**
111
+ * A closed range containing all values greater than or equal to the
112
+ * given lower bound, and less than or equal to the given upper bound.
113
+ */
76
114
static <U extends Comparable <U >> Range <U > closed (U lowerBound , U upperBound ) {
77
115
return new Interval <>( new LowerBound <>( lowerBound , false ),
78
116
new UpperBound <>( upperBound , false ) );
79
117
}
80
118
119
+ /**
120
+ * A range containing all strings which match the given pattern,
121
+ * with case-sensitivity specified explicitly. The pattern must
122
+ * be expressed in terms of the default wildcard characters
123
+ * {@code _} and {@code %}.
124
+ *
125
+ * @param pattern A pattern involving the default wildcard characters
126
+ * @param caseSensitive {@code true} if matching is case-sensitive
127
+ */
81
128
static Range <String > pattern (String pattern , boolean caseSensitive ) {
82
129
return new Pattern ( pattern , caseSensitive );
83
130
}
84
131
132
+ /**
133
+ * A range containing all strings which match the given pattern,
134
+ * with case-sensitivity specified explicitly. The pattern must
135
+ * be expressed in terms of the given single-character and
136
+ * multi-character wildcards.
137
+ *
138
+ * @param pattern A pattern involving the given wildcard characters
139
+ * @param caseSensitive {@code true} if matching is case-sensitive
140
+ * @param charWildcard A wildcard character which matches any single character
141
+ * @param stringWildcard A wildcard character which matches any string of characters
142
+ */
85
143
static Range <String > pattern (String pattern , boolean caseSensitive , char charWildcard , char stringWildcard ) {
86
144
return new Pattern ( pattern , caseSensitive , charWildcard , stringWildcard );
87
145
}
88
146
147
+ /**
148
+ * A range containing all strings which match the given pattern,
149
+ * with case-sensitivity. The pattern must be expressed in terms
150
+ * of the default wildcard characters {@code _} and {@code %}.
151
+ */
89
152
static Range <String > pattern (String pattern ) {
90
153
return pattern ( pattern , true );
91
154
}
92
155
156
+ /**
157
+ * A range containing all strings which begin with the given prefix,
158
+ * with case-sensitivity specified explicitly.
159
+ */
93
160
static Range <String > prefix (String prefix , boolean caseSensitive ) {
94
161
return pattern ( escape ( prefix ) + '%' , caseSensitive );
95
162
}
96
163
164
+ /**
165
+ * A range containing all strings which end with the given suffix,
166
+ * with case-sensitivity specified explicitly.
167
+ */
97
168
static Range <String > suffix (String suffix , boolean caseSensitive ) {
98
169
return pattern ( '%' + escape ( suffix ), caseSensitive );
99
170
}
100
171
172
+ /**
173
+ * A range containing all strings which contain the given substring,
174
+ * with case-sensitivity specified explicitly.
175
+ */
101
176
static Range <String > containing (String substring , boolean caseSensitive ) {
102
177
return pattern ( '%' + escape ( substring ) + '%' , caseSensitive );
103
178
}
104
179
180
+ /**
181
+ * A range containing all strings which begin with the given prefix,
182
+ * with case-sensitivity.
183
+ */
105
184
static Range <String > prefix (String prefix ) {
106
185
return prefix ( prefix , true );
107
186
}
108
187
188
+ /**
189
+ * A range containing all strings which end with the given suffix,
190
+ * with case-sensitivity.
191
+ */
109
192
static Range <String > suffix (String suffix ) {
110
193
return pattern ( suffix , true );
111
194
}
112
195
196
+ /**
197
+ * A range containing all strings which contain the given substring,
198
+ * with case-sensitivity.
199
+ */
113
200
static Range <String > containing (String substring ) {
114
201
return pattern ( substring , true );
115
202
}
116
203
204
+ /**
205
+ * An empty range containing no values.
206
+ */
117
207
static <U > Range <U > empty (Class <U > type ) {
118
208
return new EmptyRange <>( type );
119
209
}
120
210
211
+ /**
212
+ * A complete range containing all values of the given type.
213
+ */
121
214
static <U > Range <U > full (Class <U > type ) {
122
215
return new FullRange <>( type );
123
216
}
124
217
218
+ /**
219
+ * A range containing all allowed values of the given type
220
+ * except {@code null}.
221
+ */
125
222
static <U > Range <U > notNull (Class <U > type ) {
126
223
return new NotNull <>( type );
127
224
}
128
225
226
+ /**
227
+ * Escape occurrences of the default wildcard characters in the
228
+ * given literal string.
229
+ */
129
230
private static String escape (String literal ) {
130
231
final var result = new StringBuilder ();
131
232
for ( int i = 0 ; i < literal .length (); i ++ ) {
0 commit comments