1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.allanbank.mongodb.bson;
22
23 import static com.allanbank.mongodb.util.Assertions.assertNotNull;
24
25 import java.io.Serializable;
26
27 import com.allanbank.mongodb.bson.builder.BuilderFactory;
28 import com.allanbank.mongodb.bson.builder.DocumentBuilder;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class DocumentReference implements DocumentAssignable, Serializable {
53
54
55 public static final String COLLECTION_FIELD_NAME = "$ref";
56
57
58 public static final String DATABASE_FIELD_NAME = "$db";
59
60
61 public static final String ID_FIELD_NAME = "$id";
62
63
64 private static final long serialVersionUID = 7597767390422754639L;
65
66
67 private final String myCollectionName;
68
69
70 private final String myDatabaseName;
71
72
73 private final Element myId;
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public DocumentReference(final String collectionName, final Element id)
88 throws IllegalArgumentException {
89 this(null, collectionName, id);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public DocumentReference(final String databaseName,
107 final String collectionName, final Element id)
108 throws IllegalArgumentException {
109
110 assertNotNull(collectionName,
111 "The collection name of a Document Reference (DBRef) cannot be null.");
112 assertNotNull(id,
113 "The id of a Document Reference (DBRef) cannot be null.");
114
115 myDatabaseName = databaseName;
116 myCollectionName = collectionName;
117 myId = id.withName(ID_FIELD_NAME);
118 }
119
120
121
122
123
124
125
126
127 @Override
128 public Document asDocument() {
129 final DocumentBuilder builder = BuilderFactory.start();
130
131 builder.add(COLLECTION_FIELD_NAME, myCollectionName);
132 builder.add(myId.withName(ID_FIELD_NAME));
133 if (myDatabaseName != null) {
134 builder.add(DATABASE_FIELD_NAME, myDatabaseName);
135 }
136
137 return builder.asDocument();
138 }
139
140
141
142
143
144
145
146
147 @Override
148 public boolean equals(final Object object) {
149 boolean result = false;
150 if (this == object) {
151 result = true;
152 }
153 else if ((object != null) && (getClass() == object.getClass())) {
154 final DocumentReference other = (DocumentReference) object;
155
156 result = myCollectionName.equals(other.myCollectionName)
157 && myId.withName(ID_FIELD_NAME).equals(
158 other.myId.withName(ID_FIELD_NAME))
159 && nullSafeEquals(myDatabaseName, other.myDatabaseName);
160 }
161 return result;
162 }
163
164
165
166
167
168
169 public String getCollectionName() {
170 return myCollectionName;
171 }
172
173
174
175
176
177
178
179 public String getDatabaseName() {
180 return myDatabaseName;
181 }
182
183
184
185
186
187
188 public Element getId() {
189 return myId;
190 }
191
192
193
194
195
196
197
198
199 @Override
200 public int hashCode() {
201 int result = 1;
202 result = (31 * result) + myCollectionName.hashCode();
203 result = (31 * result) + myId.withName(ID_FIELD_NAME).hashCode();
204 result = (31 * result)
205 + ((myDatabaseName != null) ? myDatabaseName.hashCode() : 3);
206 return result;
207 }
208
209
210
211
212
213
214
215
216 @Override
217 public String toString() {
218 final StringBuilder builder = new StringBuilder("{ '");
219
220 builder.append(COLLECTION_FIELD_NAME);
221 builder.append("' : '");
222 builder.append(myCollectionName);
223 builder.append("', ");
224
225 builder.append(myId.withName(ID_FIELD_NAME));
226
227 if (myDatabaseName != null) {
228 builder.append(", '");
229 builder.append(DATABASE_FIELD_NAME);
230 builder.append("' : '");
231 builder.append(myDatabaseName);
232 builder.append("'");
233 }
234
235 builder.append(" }");
236
237 return builder.toString();
238 }
239
240
241
242
243
244
245
246
247
248
249
250 protected boolean nullSafeEquals(final Object rhs, final Object lhs) {
251 return (rhs == lhs) || ((rhs != null) && rhs.equals(lhs));
252 }
253 }