1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.allanbank.mongodb.bson.element;
21
22 import static com.allanbank.mongodb.util.Assertions.assertNotNull;
23
24 import java.util.Iterator;
25
26 import com.allanbank.mongodb.bson.Document;
27 import com.allanbank.mongodb.bson.Element;
28 import com.allanbank.mongodb.bson.ElementType;
29 import com.allanbank.mongodb.bson.Visitor;
30 import com.allanbank.mongodb.bson.builder.BuilderFactory;
31 import com.allanbank.mongodb.bson.builder.DocumentBuilder;
32 import com.allanbank.mongodb.bson.io.StringEncoder;
33
34
35
36
37
38
39
40
41
42
43 public class JavaScriptWithScopeElement extends JavaScriptElement {
44
45
46 @SuppressWarnings("hiding")
47 public static final ElementType TYPE = ElementType.JAVA_SCRIPT_WITH_SCOPE;
48
49
50 private static final long serialVersionUID = -5697976862389984453L;
51
52
53
54
55
56
57
58
59
60
61
62
63
64 private static long computeSize(final String name, final String javaScript,
65 final Document scope) {
66 long result = 15;
67
68 result += StringEncoder.utf8Size(name);
69 result += StringEncoder.utf8Size(javaScript);
70 if (scope != null) {
71 result += scope.size();
72 }
73
74 return result;
75 }
76
77
78 private final Document myScope;
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public JavaScriptWithScopeElement(final String name,
94 final String javaScript, final Document scope) {
95 this(name, javaScript, scope, computeSize(name, javaScript, scope));
96
97 assertNotNull(scope, "JavaScript element's scope cannot be null.");
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public JavaScriptWithScopeElement(final String name,
119 final String javaScript, final Document scope, final long size) {
120 super(name, javaScript, size);
121
122 assertNotNull(scope, "JavaScript element's scope cannot be null.");
123
124 myScope = scope;
125 }
126
127
128
129
130
131
132
133 @Override
134 public void accept(final Visitor visitor) {
135 visitor.visitJavaScript(getName(), getJavaScript(), getScope());
136 }
137
138
139
140
141
142
143
144
145 @Override
146 public int compareTo(final Element otherElement) {
147 int result = super.compareTo(otherElement);
148
149 if (result == 0) {
150 final JavaScriptWithScopeElement other = (JavaScriptWithScopeElement) otherElement;
151
152 final Iterator<Element> thisIter = myScope.iterator();
153 final Iterator<Element> otherIter = other.myScope.iterator();
154 while (thisIter.hasNext() && otherIter.hasNext()) {
155 result = thisIter.next().compareTo(otherIter.next());
156 if (result != 0) {
157 return result;
158 }
159 }
160
161 if (thisIter.hasNext()) {
162 return 1;
163 }
164 else if (otherIter.hasNext()) {
165 return -1;
166 }
167 else {
168 return 0;
169 }
170 }
171
172 return result;
173 }
174
175
176
177
178
179
180
181
182
183
184 @Override
185 public boolean equals(final Object object) {
186 boolean result = false;
187 if (this == object) {
188 result = true;
189 }
190 else if ((object != null) && (getClass() == object.getClass())) {
191 final JavaScriptWithScopeElement other = (JavaScriptWithScopeElement) object;
192
193 result = super.equals(object)
194 && nullSafeEquals(myScope, other.myScope);
195 }
196 return result;
197 }
198
199
200
201
202
203
204 public Document getScope() {
205 return myScope;
206 }
207
208
209
210
211 @Override
212 public ElementType getType() {
213 return TYPE;
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227 @Override
228 public Document getValueAsObject() {
229 final DocumentBuilder b = BuilderFactory.start();
230 b.add("$code", getJavaScript());
231 b.add("$scope", myScope);
232
233 return b.build();
234 }
235
236
237
238
239
240
241 @Override
242 public int hashCode() {
243 int result = 1;
244 result = (31 * result) + super.hashCode();
245 result = (31 * result) + ((myScope != null) ? myScope.hashCode() : 3);
246 return result;
247 }
248
249
250
251
252
253
254
255 @Override
256 public JavaScriptWithScopeElement withName(final String name) {
257 if (getName().equals(name)) {
258 return this;
259 }
260 return new JavaScriptWithScopeElement(name, getJavaScript(), myScope);
261 }
262 }