-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDoc.java
227 lines (202 loc) · 6.5 KB
/
Doc.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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.db.mongo;
import org.bson.Document;
import sirius.db.mixing.Mapping;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Value;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
/**
* A simple wrapper of a document in Mongo DB.
*/
public class Doc {
private Document obj;
/**
* Wraps a result from Mongo DB
*
* @param obj the document or object to wrap
*/
@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType")
public Doc(Document obj) {
this.obj = obj;
}
/**
* Returns the ID of the document
*
* @return the id as assigned by mongo db
*/
public String id() {
return String.valueOf(obj.get(Mango.ID_FIELD));
}
/**
* Returns the value of the requested field wrapped as {@link Value}.
*
* @param field the field to fetch
* @return the value for the given field
*/
public Value get(String field) {
return Value.of(obj.get(field));
}
/**
* Returns the value of the requested field wrapped as {@link Value}.
*
* @param field the field to fetch
* @return the value for the given field
*/
public Value get(Mapping field) {
return Value.of(obj.get(field.toString()));
}
/**
* Returns the string contents of the given field
*
* @param field the field to fetch
* @return the string contents or "" if the field is empty
*/
@Nonnull
public String getString(String field) {
return get(field).asString();
}
/**
* Returns the string contents of the given field
*
* @param field the field to fetch
* @return the string contents or "" if the field is empty
*/
@Nonnull
public String getString(Mapping field) {
return get(field).asString();
}
/**
* Returns the list contained in the given field
*
* @param field the field to fetch
* @return the list contained in the given field or an empty list, if the field was empty
*/
@SuppressWarnings("unchecked")
public List<Object> getList(String field) {
Object result = obj.get(field);
return result == null ? Collections.emptyList() : (List<Object>) result;
}
/**
* Returns the list contained in the given field
*
* @param field the field to fetch
* @return the list contained in the given field or an empty list, if the field was empty
*/
public List<Object> getList(Mapping field) {
return getList(field.toString());
}
/**
* Returns the list of strings contained in the given field
*
* @param field the field to fetch
* @return the list contained in the given field or an empty list, if the field was empty
*/
@SuppressWarnings("unchecked")
public List<String> getStringList(String field) {
return (List<String>) (Object) getList(field);
}
/**
* Returns the list of strings contained in the given field
*
* @param field the field to fetch
* @return the list contained in the given field or an empty list, if the field was empty
*/
public List<String> getStringList(Mapping field) {
return getStringList(field.toString());
}
/**
* Returns the object stored for the given field.
* <p>
* If there is no object present or the given field doesn't contain an object, an empty readonly instance is returned.
*
* @param field the field to read
* @return the object stored for the field
*/
@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType")
public Document getObject(String field) {
Object result = get(field).get();
if (result instanceof Document document) {
return document;
}
return ReadonlyObject.EMPTY_OBJECT;
}
/**
* Returns the object stored for the given field.
* <p>
* If there is no object present or the given field doesn't contain an object, an empty readonly instance is returned.
*
* @param field the field to read
* @return the object stored for the field
*/
public Document getObject(Mapping field) {
return getObject(field.toString());
}
/**
* Returns the inner value stored in the object in the given field.
*
* @param field the field to read the object from
* @param key the key to read from the object
* @return the value within the inner object wrapped as {@link Value}
*/
public Value getValueInObject(String field, String key) {
if (Strings.isEmpty(key)) {
return Value.EMPTY;
}
return Value.of(getObject(field).get(key));
}
/**
* Returns the inner value stored in the object in the given field.
*
* @param field the field to read the object from
* @param key the key to read from the object
* @return the value within the inner object wrapped as {@link Value}
*/
public Value getValueInObject(Mapping field, String key) {
return getValueInObject(field.toString(), key);
}
/**
* Updates the underlying object, without updating the database.
* <p>
* This can be used to enhance an existing object after an update for further consumers. This is not intended to
* update or modify the database in any way. Use {@link Mongo#update()} instead.
*
* @param key the field to update
* @param value the new value for the field
*/
public void put(String key, Object value) {
obj.put(key, value);
}
/**
* Updates the underlying object, without updating the database.
* <p>
* This can be used to enhance an existing object after an update for further consumers. This is not intended to
* update or modify the database in any way. Use {@link Mongo#update()} instead.
*
* @param key the field to update
* @param value the new value for the field
*/
public void put(Mapping key, Object value) {
obj.put(key.toString(), value);
}
@Override
public String toString() {
return obj == null ? "null" : obj.toString();
}
/**
* Retruns the underlying Mongo DB Obect.
*
* @return the underlying object
*/
@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType")
public Document getUnderlyingObject() {
return obj;
}
}