forked from kohanyirobert/ebson
/
BsonObject.java
295 lines (259 loc) · 8.02 KB
/
BsonObject.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package com.github.kohanyirobert.ebson;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import javax.annotation.Nullable;
/**
* Representation of a <a href="http://bsonspec.org/">BSON</a> object type.
*
* @see BsonToken
* @see BsonBinary
*/
public enum BsonObject {
/**
* 64-bit IEEE 754 floating point.
*/
DOUBLE(BsonBytes.DOUBLE, DefaultPredicate.DOUBLE, DefaultReader.DOUBLE,
DefaultWriter.DOUBLE),
/**
* UTF-8 string.
*/
STRING(BsonBytes.STRING, DefaultPredicate.STRING, DefaultReader.STRING,
DefaultWriter.STRING),
/**
* Embedded {@linkplain BsonToken#DOCUMENT document}.
*/
EMBEDDED(BsonBytes.EMBEDDED, DefaultPredicate.EMBEDDED,
BsonToken.DOCUMENT.reader(), BsonToken.DOCUMENT.writer()),
/**
* Special embedded {@linkplain #EMBEDDED document}.
* <p>
* <b>Note:</b> an array is a document whose keys are integer values starting
* with 0 and continuing sequentially.
* </p>
*/
ARRAY(BsonBytes.ARRAY, DefaultPredicate.ARRAY, DefaultReader.ARRAY,
DefaultWriter.ARRAY),
/**
* Binary data.
*/
BINARY(BsonBytes.BINARY, DefaultPredicate.BINARY, DefaultReader.BINARY,
DefaultWriter.BINARY),
/**
* Undefined.
*
* @deprecated See the <a href="http://bsonspec.org/#/">BSON</a> specification
* for details.
*/
@Deprecated
UNDEFINED(BsonBytes.UNDEFINED),
/**
* <a href="http://www.mongodb.org/display/DOCS/Object+IDs">Object ID</a>.
* <p>
* <b>Note:</b> special <a href="http://mongodb.org">MongoDB</a> related type.
* </p>
*/
OBJECT_ID(BsonBytes.OBJECT_ID, DefaultPredicate.OBJECT_ID,
DefaultReader.OBJECT_ID, DefaultWriter.OBJECT_ID),
/**
* Boolean.
* <p>
* <b>Note:</b> 0 is <em>false</em>; 1 is <em>true</em>.
* </p>
*/
BOOLEAN(BsonBytes.BOOLEAN, DefaultPredicate.BOOLEAN, DefaultReader.BOOLEAN,
DefaultWriter.BOOLEAN),
/**
* UTC date-time.
* <p>
* <b>Note</b>: milliseconds since the Unix epoch.
* </p>
*/
UTC_DATE_TIME(BsonBytes.UTC_DATE_TIME, DefaultPredicate.UTC_DATE_TIME,
DefaultReader.UTC_DATE_TIME, DefaultWriter.UTC_DATE_TIME),
/**
* <em>null</em>.
*/
NULL(BsonBytes.NULL, DefaultPredicate.NULL, DefaultReader.NULL,
DefaultWriter.NULL),
/**
* Regular expression.
*/
REGULAR_EXPRESSION(BsonBytes.REGULAR_EXPRESSION, DefaultPredicate.REGULAR_EXPRESSION,
DefaultReader.REGULAR_EXPRESSION, DefaultWriter.REGULAR_EXPRESSION),
/**
* DB pointer.
*
* @deprecated See the <a
* href="http://api.mongodb.org/java/1.3/com/mongodb/DBPointer.html">following
* link</a> for more details.
*/
@Deprecated
DB_POINTER(BsonBytes.DB_POINTER),
/**
* JavaScript code.
*/
JAVASCRIPT_CODE(BsonBytes.JAVASCRIPT_CODE),
/**
* Symbol.
* <p>
* <b>Note:</b> similar to a string but for languages with a distinct symbol
* type.
* </p>
*/
SYMBOL(BsonBytes.SYMBOL),
/**
* JavaScript code with scope.
*/
JAVASCRIPT_CODE_WITH_SCOPE(BsonBytes.JAVASCRIPT_CODE_WITH_SCOPE),
/**
* 32-bit signed integer.
*/
INT32(BsonBytes.INT32, DefaultPredicate.INT32, DefaultReader.INT32,
DefaultWriter.INT32),
/**
* <a href="http://www.mongodb.org/display/DOCS/Timestamp+data+type"
* >Timestamp</a>.
* <p>
* <b>Note:</b> special internal type used by MongoDB replication and
* sharding.
* </p>
*/
TIMESTAMP(BsonBytes.TIMESTAMP, DefaultPredicate.TIMESTAMP,
DefaultReader.TIMESTAMP, DefaultWriter.TIMESTAMP),
/**
* 64-bit signed integer.
*/
INT64(BsonBytes.INT64, DefaultPredicate.INT64, DefaultReader.INT64,
DefaultWriter.INT64),
/**
* <a href="http://www.mongodb.org/display/DOCS/min+and+max+Query+Specifiers">
* Max key</a>.
* <p>
* <b>Note:</b> special type which compares higher than all other possible
* {@code BSON} element values.
* </p>
*/
MAX_KEY(BsonBytes.MAX_KEY),
/**
* <a href="http://www.mongodb.org/display/DOCS/min+and+max+Query+Specifiers">
* Min key</a>.
* <p>
* <b>Note:</b> special type which compares lower than all other possible
* {@code BSON} element values.
* </p>
*/
MIN_KEY(BsonBytes.MIN_KEY);
private final byte terminal;
private Predicate<Class<?>> predicate;
private BsonReader reader;
private BsonWriter writer;
private BsonObject(byte terminal) {
this(terminal, Predicates.<Class<?>>alwaysFalse(), null, null);
}
private BsonObject(byte terminal, Predicate<Class<?>> predicate,
BsonReader reader, BsonWriter writer) {
this.terminal = terminal;
this.predicate = predicate;
this.reader = reader;
this.writer = writer;
}
/**
* Returns this object's associated terminal.
*
* @return this object's associated terminal
*/
public byte terminal() {
return terminal;
}
/**
* Returns this object's associated {@linkplain Predicate predicate}.
*
* @return this object's associated predicate
* @throws IllegalStateException if this object does not have an associated
* predicate
*/
public Predicate<Class<?>> predicate() {
Preconditions.checkState(predicate != null, "'%s' does not have an associated predicate", this);
return predicate;
}
/**
* Associates {@link Predicate predicate} with this object.
*
* @param predicate the predicate to be associated with this object
*/
public void predicate(Predicate<Class<?>> predicate) {
Preconditions.checkNotNull(predicate, "cannot associate a null predicate with '%s'", this);
this.predicate = predicate;
}
/**
* Returns this object's associated {@linkplain BsonReader reader}.
*
* @return this object's associated reader
* @throws IllegalStateException if this object does not have an associated
* reader
*/
public BsonReader reader() {
Preconditions.checkState(reader != null, "'%s' does not have an associated reader", this);
return reader;
}
/**
* Associates {@link BsonReader reader} with this object.
*
* @param reader the reader to be associated with this object
*/
public void reader(BsonReader reader) {
Preconditions.checkNotNull(predicate, "cannot associate a null reader with '%s'", this);
this.reader = reader;
}
/**
* Returns this object's associated {@linkplain BsonWriter writer}.
*
* @return this object's associated writer
* @throws IllegalStateException if this object does not have an associated
* writer
*/
public BsonWriter writer() {
Preconditions.checkState(writer != null, "'%s' does not have an associated writer", this);
return writer;
}
/**
* Associates {@link BsonWriter writer} with this object.
*
* @param writer the writer to be associated with this object
*/
public void writer(BsonWriter writer) {
Preconditions.checkNotNull(writer, "cannot associate a null writer with '%s'", this);
this.writer = writer;
}
/**
* Returns the object representing {@code clazz}.
*
* @param clazz the class to return a object representation for
* @return the object representing {@code clazz}
* @throws IllegalArgumentException if no object representing {@code clazz}
* was found
*/
public static BsonObject find(@Nullable Class<?> clazz) {
for (BsonObject object : values())
if (object.predicate().apply(clazz))
return object;
throw new IllegalArgumentException(String.format("no object "
+ "representing the '%s' type value was found", clazz));
}
/**
* Returns the object representing {@code terminal}.
*
* @param terminal the terminal to return a object representation for
* @return the object representing {@code terminal}
* @throws IllegalArgumentException if no object representing {@code terminal}
* was found
*/
public static BsonObject find(byte terminal) {
for (BsonObject object : values())
if (object.terminal() - terminal == 0)
return object;
throw new IllegalArgumentException(String.format("no object representing "
+ "the '%s' terminal value was found", Byte.valueOf(terminal)));
}
}