Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
VisitorAdapter |
|
| 1.1363636363636365;1.136 |
1 | /* | |
2 | * #%L | |
3 | * VisitorAdapter.java - mongodb-async-driver - Allanbank Consulting, Inc. | |
4 | * %% | |
5 | * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc. | |
6 | * %% | |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
8 | * you may not use this file except in compliance with the License. | |
9 | * You may obtain a copy of the License at | |
10 | * | |
11 | * http://www.apache.org/licenses/LICENSE-2.0 | |
12 | * | |
13 | * Unless required by applicable law or agreed to in writing, software | |
14 | * distributed under the License is distributed on an "AS IS" BASIS, | |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
16 | * See the License for the specific language governing permissions and | |
17 | * limitations under the License. | |
18 | * #L% | |
19 | */ | |
20 | ||
21 | package com.allanbank.mongodb.bson; | |
22 | ||
23 | import java.util.List; | |
24 | ||
25 | import com.allanbank.mongodb.bson.element.ObjectId; | |
26 | ||
27 | /** | |
28 | * VisitorAdapter provides a helper for {@link Visitor} implementations that are | |
29 | * only interested in a subset of the elements within a document. | |
30 | * <p> | |
31 | * This implementation will walk the entire tree of elements. Derived classes | |
32 | * need only override the methods of interest. Derived classes should be careful | |
33 | * to always call the {@code super} implementation of the following methods to | |
34 | * ensure they do not break the document walking. Calling {@code super} for all | |
35 | * methods is encouraged. | |
36 | * <ul> | |
37 | * <li> {@link #visit(List)}</li> | |
38 | * <li> {@link #visitArray(String, List)}</li> | |
39 | * <li> {@link #visitDocument(String, List)}</li> | |
40 | * <li> {@link #visitJavaScript(String, String, Document)}</li> | |
41 | * </ul> | |
42 | * </p> | |
43 | * <p> | |
44 | * As a further aid to {@link Visitor} implementations only interested in the | |
45 | * names of elements, this class will call the {@link #visitName(String)} method | |
46 | * for each element visited. | |
47 | * </p> | |
48 | * | |
49 | * @api.yes This class is part of the driver's API. Public and protected members | |
50 | * will be deprecated for at least 1 non-bugfix release (version | |
51 | * numbers are <major>.<minor>.<bugfix>) before being | |
52 | * removed or modified. | |
53 | * @copyright 2013, Allanbank Consulting, Inc., All Rights Reserved | |
54 | */ | |
55 | public class VisitorAdapter implements Visitor { | |
56 | ||
57 | /** | |
58 | * Creates a new VisitorAdapter. | |
59 | */ | |
60 | public VisitorAdapter() { | |
61 | 2340 | super(); |
62 | 2340 | } |
63 | ||
64 | /** | |
65 | * {@inheritDoc} | |
66 | * <p> | |
67 | * Overridden to iterate over the elements of the document. | |
68 | * </p> | |
69 | */ | |
70 | @Override | |
71 | public void visit(final List<Element> elements) { | |
72 | 2338 | for (final Element element : elements) { |
73 | 2468 | element.accept(this); |
74 | 2468 | } |
75 | 2338 | } |
76 | ||
77 | /** | |
78 | * {@inheritDoc} | |
79 | * <p> | |
80 | * Overridden to visit the name of the element and then iterate over the | |
81 | * elements of the array. | |
82 | * </p> | |
83 | */ | |
84 | @Override | |
85 | public void visitArray(final String name, final List<Element> elements) { | |
86 | 10 | visitName(name); |
87 | 10 | for (final Element element : elements) { |
88 | 38 | element.accept(this); |
89 | 38 | } |
90 | 10 | } |
91 | ||
92 | /** | |
93 | * {@inheritDoc} | |
94 | * <p> | |
95 | * Overridden to visit the name of the element. | |
96 | * </p> | |
97 | */ | |
98 | @Override | |
99 | public void visitBinary(final String name, final byte subType, | |
100 | final byte[] data) { | |
101 | 4 | visitName(name); |
102 | 4 | } |
103 | ||
104 | /** | |
105 | * {@inheritDoc} | |
106 | * <p> | |
107 | * Overridden to visit the name of the element. | |
108 | * </p> | |
109 | */ | |
110 | @Override | |
111 | public void visitBoolean(final String name, final boolean value) { | |
112 | 36 | visitName(name); |
113 | 36 | } |
114 | ||
115 | /** | |
116 | * {@inheritDoc} | |
117 | * <p> | |
118 | * Overridden to visit the name of the element. | |
119 | * </p> | |
120 | */ | |
121 | @Override | |
122 | public void visitDBPointer(final String name, final String databaseName, | |
123 | final String collectionName, final ObjectId id) { | |
124 | 2 | visitName(name); |
125 | 2 | } |
126 | ||
127 | /** | |
128 | * {@inheritDoc} | |
129 | * <p> | |
130 | * Overridden to visit the name of the element and then iterate over the | |
131 | * elements of the document element. | |
132 | * </p> | |
133 | */ | |
134 | @Override | |
135 | public void visitDocument(final String name, final List<Element> elements) { | |
136 | 95 | visitName(name); |
137 | 95 | for (final Element element : elements) { |
138 | 99 | element.accept(this); |
139 | 99 | } |
140 | 95 | } |
141 | ||
142 | /** | |
143 | * {@inheritDoc} | |
144 | * <p> | |
145 | * Overridden to visit the name of the element. | |
146 | * </p> | |
147 | */ | |
148 | @Override | |
149 | public void visitDouble(final String name, final double value) { | |
150 | 2 | visitName(name); |
151 | 2 | } |
152 | ||
153 | /** | |
154 | * {@inheritDoc} | |
155 | * <p> | |
156 | * Overridden to visit the name of the element. | |
157 | * </p> | |
158 | */ | |
159 | @Override | |
160 | public void visitInteger(final String name, final int value) { | |
161 | 2349 | visitName(name); |
162 | 2349 | } |
163 | ||
164 | /** | |
165 | * {@inheritDoc} | |
166 | * <p> | |
167 | * Overridden to visit the name of the element. | |
168 | * </p> | |
169 | */ | |
170 | @Override | |
171 | public void visitJavaScript(final String name, final String code) { | |
172 | 2 | visitName(name); |
173 | 2 | } |
174 | ||
175 | /** | |
176 | * {@inheritDoc} | |
177 | * <p> | |
178 | * Overridden to visit the name of the element. | |
179 | * </p> | |
180 | */ | |
181 | @Override | |
182 | public void visitJavaScript(final String name, final String code, | |
183 | final Document scope) { | |
184 | 2 | visitName(name); |
185 | 2 | scope.accept(this); |
186 | 2 | } |
187 | ||
188 | /** | |
189 | * {@inheritDoc} | |
190 | * <p> | |
191 | * Overridden to visit the name of the element. | |
192 | * </p> | |
193 | */ | |
194 | @Override | |
195 | public void visitLong(final String name, final long value) { | |
196 | 2 | visitName(name); |
197 | 2 | } |
198 | ||
199 | /** | |
200 | * {@inheritDoc} | |
201 | * <p> | |
202 | * Overridden to visit the name of the element. | |
203 | * </p> | |
204 | */ | |
205 | @Override | |
206 | public void visitMaxKey(final String name) { | |
207 | 2 | visitName(name); |
208 | 2 | } |
209 | ||
210 | /** | |
211 | * {@inheritDoc} | |
212 | * <p> | |
213 | * Overridden to visit the name of the element. | |
214 | * </p> | |
215 | */ | |
216 | @Override | |
217 | public void visitMinKey(final String name) { | |
218 | 2 | visitName(name); |
219 | 2 | } |
220 | ||
221 | /** | |
222 | * {@inheritDoc} | |
223 | * <p> | |
224 | * Overridden to visit the name of the element. | |
225 | * </p> | |
226 | */ | |
227 | @Override | |
228 | public void visitMongoTimestamp(final String name, final long value) { | |
229 | 2 | visitName(name); |
230 | 2 | } |
231 | ||
232 | /** | |
233 | * {@inheritDoc} | |
234 | * <p> | |
235 | * Overridden to visit the name of the element. | |
236 | * </p> | |
237 | */ | |
238 | @Override | |
239 | public void visitNull(final String name) { | |
240 | 2 | visitName(name); |
241 | 2 | } |
242 | ||
243 | /** | |
244 | * {@inheritDoc} | |
245 | * <p> | |
246 | * Overridden to visit the name of the element. | |
247 | * </p> | |
248 | */ | |
249 | @Override | |
250 | public void visitObjectId(final String name, final ObjectId id) { | |
251 | 2 | visitName(name); |
252 | 2 | } |
253 | ||
254 | /** | |
255 | * {@inheritDoc} | |
256 | * <p> | |
257 | * Overridden to visit the name of the element. | |
258 | * </p> | |
259 | */ | |
260 | @Override | |
261 | public void visitRegularExpression(final String name, final String pattern, | |
262 | final String options) { | |
263 | 2 | visitName(name); |
264 | 2 | } |
265 | ||
266 | /** | |
267 | * {@inheritDoc} | |
268 | * <p> | |
269 | * Overridden to visit the name of the element. | |
270 | * </p> | |
271 | */ | |
272 | @Override | |
273 | public void visitString(final String name, final String value) { | |
274 | 85 | visitName(name); |
275 | 85 | } |
276 | ||
277 | /** | |
278 | * {@inheritDoc} | |
279 | * <p> | |
280 | * Overridden to visit the name of the element. | |
281 | * </p> | |
282 | */ | |
283 | @Override | |
284 | public void visitSymbol(final String name, final String symbol) { | |
285 | 2 | visitName(name); |
286 | 2 | } |
287 | ||
288 | /** | |
289 | * {@inheritDoc} | |
290 | * <p> | |
291 | * Overridden to visit the name of the element. | |
292 | * </p> | |
293 | */ | |
294 | @Override | |
295 | public void visitTimestamp(final String name, final long timestamp) { | |
296 | 2 | visitName(name); |
297 | 2 | } |
298 | ||
299 | /** | |
300 | * Extension point for {@link Visitor} implementation only interested in the | |
301 | * name of the elements. | |
302 | * | |
303 | * @param name | |
304 | * The name of the element. | |
305 | */ | |
306 | protected void visitName(final String name) { | |
307 | // Nothing. Extension point. | |
308 | 48 | } |
309 | } |