Permalink
Browse files

Update DOCUMENT reader. Rename ObjectId ...

 - Update DefaultReader.DOCUMENT so that it returns an immutable BsonDocument
   instead of a simple Map.
 - Update BsonDocuments.readFrom(ByteBuffer) so that it doesn't create a new
   BsonDocument by copying the Map (which is a BsonDocument really) that was
   read from the buffer.
 - Rename ObjectId --> BsonObjectId.
 - Reanem Timestamp --> BsonTimestamp.
  • Loading branch information...
1 parent 9c27089 commit 8c6a9353c78bd3cd875d2ecc7e2c80ba835ae76d @kohanyirobert committed Jan 28, 2012
@@ -7,7 +7,7 @@
import javax.xml.bind.DatatypeConverter;
-final class BasicObjectId implements ObjectId {
+final class BasicObjectId implements BsonObjectId {
private static final int TIME_LENGTH = 4;
private static final int MACHINE_ID_LENGTH = 3;
@@ -69,8 +69,8 @@ public int hashCode() {
@Override
public boolean equals(Object object) {
- if (object instanceof ObjectId) {
- ObjectId other = (ObjectId) object;
+ if (object instanceof BsonObjectId) {
+ BsonObjectId other = (BsonObjectId) object;
return objectId.equals(other.objectId());
}
return false;
@@ -7,7 +7,7 @@
import javax.xml.bind.DatatypeConverter;
-final class BasicTimestamp implements Timestamp {
+final class BasicTimestamp implements BsonTimestamp {
private static final int TIME_LENGTH = 4;
private static final int INCREMENT_LENGTH = 4;
@@ -52,8 +52,8 @@ public int hashCode() {
@Override
public boolean equals(Object object) {
- if (object instanceof Timestamp) {
- Timestamp other = (Timestamp) object;
+ if (object instanceof BsonTimestamp) {
+ BsonTimestamp other = (BsonTimestamp) object;
return timestamp.equals(other.timestamp());
}
return false;
@@ -21,9 +21,8 @@ private BsonDocuments() {}
* @throws IllegalArgumentException if {@code buffer} is not using
* little-endian byte ordering
*/
- @SuppressWarnings("unchecked")
public static BsonDocument readFrom(ByteBuffer buffer) {
- return copyOf((Map<String, Object>) BsonToken.DOCUMENT.reader().readFrom(buffer));
+ return (BsonDocument) BsonToken.DOCUMENT.reader().readFrom(buffer);
}
/**
@@ -3,7 +3,7 @@
import java.nio.ByteBuffer;
// @checkstyle:off .
-public interface ObjectId {
+public interface BsonObjectId {
ByteBuffer objectId();
@@ -3,7 +3,7 @@
import java.nio.ByteBuffer;
// @checkstyle:off .
-public interface Timestamp {
+public interface BsonTimestamp {
ByteBuffer timestamp();
@@ -75,7 +75,7 @@ public boolean apply(Class<?> input) {
public boolean apply(Class<?> input) {
return input == null
? false
- : ObjectId.class.isAssignableFrom(input);
+ : BsonObjectId.class.isAssignableFrom(input);
}
},
@@ -133,7 +133,7 @@ public boolean apply(Class<?> input) {
public boolean apply(Class<?> input) {
return input == null
? false
- : Timestamp.class.isAssignableFrom(input);
+ : BsonTimestamp.class.isAssignableFrom(input);
}
},
@@ -9,7 +9,6 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
-import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
@@ -21,14 +20,15 @@
public Object checkedReadFrom(ByteBuffer buffer) {
int documentLength = buffer.getInt();
BsonReader fieldReader = BsonToken.FIELD.reader();
- Map<Object, Object> document = Maps.newLinkedHashMap();
+ BsonDocument.Builder document = BsonDocuments.builder();
if (documentLength > Ints.BYTES + 1)
do {
- Entry<?, ?> entry = (Entry<?, ?>) fieldReader.readFrom(buffer);
+ @SuppressWarnings("unchecked")
+ Entry<String, Object> entry = (Entry<String, Object>) fieldReader.readFrom(buffer);
document.put(entry.getKey(), entry.getValue());
} while (buffer.get(buffer.position()) != BsonBytes.EOO);
buffer.get();
- return document;
+ return document.build();
}
},
@@ -108,7 +108,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
@Override
public void writeTo(ByteBuffer buffer, Object reference) {
- buffer.put(((ObjectId) reference).objectId());
+ buffer.put(((BsonObjectId) reference).objectId());
}
},
@@ -181,7 +181,7 @@ public void writeTo(ByteBuffer buffer, Object reference) {
@Override
public void writeTo(ByteBuffer buffer, Object reference) {
- buffer.put(((Timestamp) reference).timestamp());
+ buffer.put(((BsonTimestamp) reference).timestamp());
}
},

0 comments on commit 8c6a935

Please sign in to comment.