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 com.allanbank.mongodb.bson.DocumentReference;
25 import com.allanbank.mongodb.bson.Element;
26 import com.allanbank.mongodb.bson.ElementType;
27 import com.allanbank.mongodb.bson.Visitor;
28 import com.allanbank.mongodb.bson.io.StringEncoder;
29
30
31
32
33
34
35
36
37
38
39
40 @Deprecated
41 public class DBPointerElement extends AbstractElement {
42
43
44 public static final ElementType TYPE = ElementType.DB_POINTER;
45
46
47 private static final long serialVersionUID = 2736569317385382748L;
48
49
50
51
52
53
54
55
56
57
58
59
60
61 private static long computeSize(final String name, final String dbName,
62 final String collectionName) {
63 long result = 16;
64
65
66 result += StringEncoder.utf8Size(name);
67 result += StringEncoder.utf8Size(dbName);
68 result += StringEncoder.utf8Size(collectionName);
69
70 return result;
71 }
72
73
74 private final String myCollectionName;
75
76
77 private final String myDatabaseName;
78
79
80 private final ObjectId myId;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public DBPointerElement(final String name, final String dbName,
98 final String collectionName, final ObjectId id) {
99 this(name, dbName, collectionName, id, computeSize(name, dbName,
100 collectionName));
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public DBPointerElement(final String name, final String dbName,
124 final String collectionName, final ObjectId id, final long size) {
125 super(name, size);
126
127 assertNotNull(dbName,
128 "DBPointer element's database name cannot be null.");
129 assertNotNull(collectionName,
130 "DBPointer element's collection name cannot be null.");
131 assertNotNull(id, "DBPointer element's object id cannot be null.");
132
133 myDatabaseName = dbName;
134 myCollectionName = collectionName;
135 myId = id;
136 }
137
138
139
140
141
142
143 @Override
144 public void accept(final Visitor visitor) {
145 visitor.visitDBPointer(getName(), myDatabaseName, myCollectionName,
146 myId);
147 }
148
149
150
151
152
153
154
155
156 @Override
157 public int compareTo(final Element otherElement) {
158 int result = super.compareTo(otherElement);
159
160 if (result == 0) {
161 final DBPointerElement other = (DBPointerElement) otherElement;
162
163 result = myDatabaseName.compareTo(other.myDatabaseName);
164 if (result == 0) {
165 result = myCollectionName.compareTo(other.myCollectionName);
166 if (result == 0) {
167 result = myId.compareTo(other.myId);
168 }
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 DBPointerElement other = (DBPointerElement) object;
192
193 result = super.equals(object)
194 && myDatabaseName.equals(other.myDatabaseName)
195 && myCollectionName.equals(other.myCollectionName)
196 && myId.equals(other.myId);
197 }
198 return result;
199 }
200
201
202
203
204
205
206 public String getCollectionName() {
207 return myCollectionName;
208 }
209
210
211
212
213
214
215 public String getDatabaseName() {
216 return myDatabaseName;
217 }
218
219
220
221
222
223
224 public ObjectId getId() {
225 return myId;
226 }
227
228
229
230
231 @Override
232 public ElementType getType() {
233 return TYPE;
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247 @Override
248 public DocumentReference getValueAsObject() {
249 return new DocumentReference(myDatabaseName, myCollectionName,
250 new ObjectIdElement("_id", myId));
251 }
252
253
254
255
256
257
258 @Override
259 public int hashCode() {
260 int result = 1;
261 result = (31 * result) + super.hashCode();
262 result = (31 * result) + myDatabaseName.hashCode();
263 result = (31 * result) + myCollectionName.hashCode();
264 result = (31 * result) + myId.hashCode();
265 return result;
266 }
267
268
269
270
271
272
273
274 @Override
275 public DBPointerElement withName(final String name) {
276 if (getName().equals(name)) {
277 return this;
278 }
279 return new DBPointerElement(name, myDatabaseName, myCollectionName,
280 myId);
281 }
282 }