Permalink
Browse files

Add mapping for BSON symbol objects.

Changes:
 - BsonSymbol: interface for a BSON symbol.
 - BasicSymbol: simple implementation of the previous interface.
 - Default{Reader,Writer}: add Bson{Reader,Writer} implementations for the
   symbol object type.
 - BsonObject: registered the Bson{Reader,Writer} implementations as the
   default ones for this type of BSON objects.
 - DefaultSymbolReaderWriterTest: not too exhaustive test for the
   implementations.

(Interactions with MongoDB are yet to be tested.)
1 parent a55cb54 commit 04ab8ec3763e03b66b06687b1f01e4f9285a1ec3 @kohanyirobert committed May 12, 2012
@@ -0,0 +1,53 @@
+package com.github.kohanyirobert.ebson;
+
+import com.google.common.base.Objects;
+import com.google.common.primitives.Ints;
+
+import java.nio.ByteBuffer;
+
+import javax.xml.bind.DatatypeConverter;
+
+final class BasicSymbol implements BsonSymbol {
+
+ private final ByteBuffer symbol;
+
+ BasicSymbol(ByteBuffer buffer) {
+ int oldPosition = buffer.position();
+
+ int symbolLength = buffer.getInt();
+ symbol = ByteBuffer.allocate(symbolLength - 1);
+
+ int oldLimit = buffer.limit();
+ buffer.limit(buffer.position() + symbolLength - 1);
+ symbol.put(buffer).flip();
+ buffer.limit(buffer.limit() + 1);
+ buffer.get();
+ buffer.limit(oldLimit);
+
+ assert buffer.position() == Ints.BYTES + oldPosition + symbolLength;
+ }
+
+ @Override
+ public ByteBuffer symbol() {
+ return symbol.asReadOnlyBuffer();
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(symbol);
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ if (object instanceof BasicSymbol) {
+ BasicSymbol other = (BasicSymbol) object;
+ return symbol.equals(other.symbol());
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return DatatypeConverter.printHexBinary(symbol.array()).toLowerCase();
+ }
+}
@@ -0,0 +1,9 @@
+package com.github.kohanyirobert.ebson;
+
+import java.nio.ByteBuffer;
+
+// @checkstyle:off .
+public interface BsonSymbol {
+
+ ByteBuffer symbol();
+}

0 comments on commit 04ab8ec

Please sign in to comment.