forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicBSONObjectBuilder.java
More file actions
214 lines (198 loc) · 6.98 KB
/
BasicBSONObjectBuilder.java
File metadata and controls
214 lines (198 loc) · 6.98 KB
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
package com.mongodb;
import java.security.InvalidParameterException;
import java.util.List;
import java.util.Map;
import org.bson.BasicBSONObject;
import org.bson.types.BasicBSONList;
import org.bson.types.ObjectId;
/*
A builder for creating BasicBSONObject models from scratch.
Utility for building complex BasicBSONObject.
This builder and provides methods to add name/value pairs to the object model and to return the resulting object.
The methods in this class can be chained to add multiple name/value pairs to the object.
This class does not allow null to be used as a name(key/field) while building the BasicBSON object
Example Code:
# BasicBSONObjectBuilder.getInstance().add(key,value).add(key,mapValue).build()
# BasicBSONObject bObject = BasicBSONObjectBuilder.createObject(map)
*/
@SuppressWarnings("rawtypes")
public class BasicBSONObjectBuilder {
private final BasicBSONObject _basicBSONObject;
/**
* Creates a builder intialized with an empty document.
* @return The new empty builder
*/
public static BasicBSONObjectBuilder getInstance() {
return new BasicBSONObjectBuilder();
}
/**
* Creates a builder initialized with an empty document.
*/
private BasicBSONObjectBuilder(){
_basicBSONObject = new BasicBSONObject();
}
/**
* add default _id field with value document.
* @return BasicBSONObjectBuilder
*/
public BasicBSONObjectBuilder addId(){
_basicBSONObject.append("_id", new ObjectId());
return this;
}
/**
* Add a field/element to the Object
*
* @param key The field name
* @param value
* @return the new BasicBSONObjectBuilder
*/
public BasicBSONObjectBuilder add(final String key, final Object value){
validateKeyField(key);
_basicBSONObject.append(key, value);
return this;
}
/**
* add a field/element with null value
*
* @param key The field name
* @return the new BasicBSONObjectBuilder
*/
public BasicBSONObjectBuilder addNull(final String key){
validateKeyField(key);
_basicBSONObject.append(key, null);
return this;
}
/**
* Add a ObjectId type field/element
* @param key
* @param value
* @return BasicBSONObjectBuilder
*/
public BasicBSONObjectBuilder addObjectId(final String key, final String value){
validateKeyField(key);
_basicBSONObject.append(key, new ObjectId(value));
return this;
}
/**
* add a fields/elements from a Map
*
* @param documentAsMap a document in Map form.
* @return the active BasicBSONObject document
*/
public BasicBSONObjectBuilder addMap(final Map documentAsMap){
validateMapDocument(documentAsMap);
_basicBSONObject.putAll(documentAsMap);
return this;
}
/**
* add a basicBSONObject Type field/element
* @param field
* @param key
* @param val
* @return BasicBSONObjectBuilder
*/
public BasicBSONObjectBuilder addObject(final String field, final String key, final Object val){
validateKeyField(field);
validateKeyField(key);
_basicBSONObject.append(field, BasicBSONObjectBuilder.createObject(key, val));
return this;
}
/**
* create BasicBSONOBject type object from the key/value
* @param key
* @param val
* @return BasicBSONObject
*/
public static BasicBSONObject createObject(final String key, final Object val){
validateKeyField(key);
return new BasicBSONObject(key,val);
}
/**
* create BasicBSONOBject type object from a map
* @param documentAsMap a document in Map form.
* @return the new BasicBSONObject
*/
public static BasicBSONObject createObject(final Map documentAsMap){
validateMapDocument(documentAsMap);
return new BasicBSONObject(documentAsMap);
}
/**
* Create BasicBSONList from the key/value
* @param mapList : List<Map<String, Object>>
* @return BasicBSONList
*/
public static BasicBSONList createNestedList(List<Map<String, Object>> mapList){
BasicBSONList bsonList = new BasicBSONList();
for (Map<String, Object> map : mapList) {
bsonList.add(createObject(map));
}
return bsonList;
}
/**
* create a list from a list of strings array
* @param stringList
* @return BasicBSONList
*/
public static BasicBSONList createList(List<String> stringList){
validateStringList(stringList);
BasicBSONList bsonList = new BasicBSONList();
int index = 0;
for(String item :stringList)
{
if(item != null && !item.isEmpty()){
bsonList.put( index, item);
index++;
}
}
return bsonList;
}
/**
* Gets the top level document.
* @return BasicBSONObject : The base object
*/
public BasicBSONObject build(){
return _basicBSONObject;
}
/**
* create and build a single element BasicBSONObject
* @param key
* @param value
* @return BasicBSONObject
*/
public static BasicBSONObject single(final String key, Object value){
validateKeyField(key);
return getInstance().add(key, value).build();
}
private static void validateMapDocument(Map aMap){
if(aMap == null)
throw new NullPointerException("documentAsMap value is null.");
if(aMap.isEmpty())
throw new InvalidParameterException("documentAsMap value is empty.");
for(Object key: aMap.keySet()){
if(key ==null || key.toString().isEmpty())throw new NullPointerException("one of the map's key value is empty .");
}
}
private static void validateKeyField(final String key){
if(key == null )throw new NullPointerException("key parameter value is null.");
if( key.isEmpty())throw new InvalidParameterException("key parameter value is empty.");
}
private static void validateStringList(List<String> stringList){
if(stringList == null)throw new NullPointerException("the list in null!");
if(stringList.isEmpty())throw new InvalidParameterException("the list is empty!");
}
/**
*
* @return int size of the top level document.
*/
public int size(){
return _basicBSONObject.size();
}
/**
* Returns true if no key/value was inserted into the top level document.
*
* @return true if empty
*/
public boolean isNullOrEmpty(){
return ( _basicBSONObject == null || _basicBSONObject.isEmpty());
}
}