-
Notifications
You must be signed in to change notification settings - Fork 121
/
KeyRange.java
206 lines (187 loc) · 5.55 KB
/
KeyRange.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*-
* #%L
* LmdbJava
* %%
* Copyright (C) 2016 - 2017 The LmdbJava Open Source Project
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.lmdbjava;
import static java.util.Objects.requireNonNull;
import static org.lmdbjava.KeyRangeType.BACKWARD_ALL;
import static org.lmdbjava.KeyRangeType.FORWARD_ALL;
/**
* Limits the range and direction of keys to iterate.
*
* <p>
* Immutable once created (although the buffers themselves may not be).
*
* @param <T> buffer type
*/
public final class KeyRange<T> {
private static final KeyRange<?> BK = new KeyRange<>(BACKWARD_ALL, null, null);
private static final KeyRange<?> FW = new KeyRange<>(FORWARD_ALL, null, null);
private final T start;
private final T stop;
private final KeyRangeType type;
/**
* Construct a key range.
*
* <p>
* End user code may find it more expressive to use one of the static methods
* provided on this class.
*
* @param type key type
* @param start start key (required if applicable for the passed range type)
* @param stop stop key (required if applicable for the passed range type)
*/
public KeyRange(final KeyRangeType type, final T start, final T stop) {
requireNonNull(type, "Key range type is required");
if (type.isStartKeyRequired()) {
requireNonNull(start, "Start key is required for this key range type");
}
if (type.isStopKeyRequired()) {
requireNonNull(stop, "Stop key is required for this key range type");
}
this.start = start;
this.stop = stop;
this.type = type;
}
/**
* Create a {@link KeyRangeType#FORWARD_ALL} range.
*
* @param <T> buffer type
* @return a key range (never null)
*/
@SuppressWarnings({"checkstyle:SuppressWarnings", "unchecked"})
public static <T> KeyRange<T> all() {
return (KeyRange<T>) FW;
}
/**
* Create a {@link KeyRangeType#BACKWARD_ALL} range.
*
* @param <T> buffer type
* @return a key range (never null)
*/
@SuppressWarnings({"checkstyle:SuppressWarnings", "unchecked"})
public static <T> KeyRange<T> allBackward() {
return (KeyRange<T>) BK;
}
/**
* Create a {@link KeyRangeType#FORWARD_AT_LEAST} range.
*
* @param <T> buffer type
* @param start start key (required)
* @return a key range (never null)
*/
public static <T> KeyRange<T> atLeast(final T start) {
return new KeyRange<>(KeyRangeType.FORWARD_AT_LEAST, start, null);
}
/**
* Create a {@link KeyRangeType#BACKWARD_AT_LEAST} range.
*
* @param <T> buffer type
* @param start start key (required)
* @return a key range (never null)
*/
public static <T> KeyRange<T> atLeastBackward(final T start) {
return new KeyRange<>(KeyRangeType.BACKWARD_AT_LEAST, start, null);
}
/**
* Create a {@link KeyRangeType#FORWARD_AT_MOST} range.
*
* @param <T> buffer type
* @param stop stop key (required)
* @return a key range (never null)
*/
public static <T> KeyRange<T> atMost(final T stop) {
return new KeyRange<>(KeyRangeType.FORWARD_AT_MOST, null, stop);
}
/**
* Create a {@link KeyRangeType#BACKWARD_AT_MOST} range.
*
* @param <T> buffer type
* @param stop stop key (required)
* @return a key range (never null)
*/
public static <T> KeyRange<T> atMostBackward(final T stop) {
return new KeyRange<>(KeyRangeType.BACKWARD_AT_MOST, null, stop);
}
/**
* Create a {@link KeyRangeType#FORWARD_CLOSED} range.
*
* @param <T> buffer type
* @param start start key (required)
* @param stop stop key (required)
* @return a key range (never null)
*/
public static <T> KeyRange<T> closed(final T start, final T stop) {
return new KeyRange<>(KeyRangeType.FORWARD_CLOSED, start, stop);
}
/**
* Create a {@link KeyRangeType#BACKWARD_CLOSED} range.
*
* @param <T> buffer type
* @param start start key (required)
* @param stop stop key (required)
* @return a key range (never null)
*/
public static <T> KeyRange<T> closedBackward(final T start, final T stop) {
return new KeyRange<>(KeyRangeType.BACKWARD_CLOSED, start, stop);
}
/**
* Create a {@link KeyRangeType#FORWARD_GREATER_THAN} range.
*
* @param <T> buffer type
* @param start start key (required)
* @return a key range (never null)
*/
public static <T> KeyRange<T> greaterThan(final T start) {
return new KeyRange<>(KeyRangeType.FORWARD_GREATER_THAN, start, null);
}
/**
* Create a {@link KeyRangeType#BACKWARD_GREATER_THAN} range.
*
* @param <T> buffer type
* @param start start key (required)
* @return a key range (never null)
*/
public static <T> KeyRange<T> greaterThanBackward(final T start) {
return new KeyRange<>(KeyRangeType.BACKWARD_GREATER_THAN, start, null);
}
/**
* Start key.
*
* @return start key (may be null)
*/
public T getStart() {
return start;
}
/**
* Stop key.
*
* @return stop key (may be null)
*/
public T getStop() {
return stop;
}
/**
* Key range type.
*
* @return type (never null)
*/
public KeyRangeType getType() {
return type;
}
}