├── .gitignore
├── LICENSE.txt
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── github
│ └── kohanyirobert
│ └── ebson
│ ├── BasicObjectId.java
│ ├── BasicSymbol.java
│ ├── BasicTimestamp.java
│ ├── BsonBinary.java
│ ├── BsonBytes.java
│ ├── BsonDocument.java
│ ├── BsonDocuments.java
│ ├── BsonObject.java
│ ├── BsonObjectId.java
│ ├── BsonReader.java
│ ├── BsonSymbol.java
│ ├── BsonTimestamp.java
│ ├── BsonToken.java
│ ├── BsonWriter.java
│ ├── DefaultDocument.java
│ ├── DefaultDocumentBuilder.java
│ ├── DefaultPredicate.java
│ ├── DefaultReader.java
│ ├── DefaultWriter.java
│ └── package-info.java
└── test
└── java
└── com
└── github
└── kohanyirobert
└── ebson
├── AbstractBsonTest.java
├── AbstractReaderWriterTest.java
├── BsonDocumentTest.java
├── BsonDocumentsTest.java
├── BsonEncodingTest.java
├── BsonRandom.java
├── DefaultArrayReaderWriterTest.java
├── DefaultBinaryReaderWriterTest.java
├── DefaultBooleanReaderWriterTest.java
├── DefaultDoubleReaderWriterTest.java
├── DefaultFieldReaderWriterTest.java
├── DefaultInt32ReaderWriterTest.java
├── DefaultInt64ReaderWriterTest.java
├── DefaultKeyReaderWriterTest.java
├── DefaultNullReaderWriterTest.java
├── DefaultObjectIdReaderWriterTest.java
├── DefaultRegularExpressionReaderWriterTest.java
├── DefaultSymbolReaderWriterTest.java
├── DefaultTimestampReaderWriterTest.java
└── DefaultUtcDateTimeReaderWriterTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *
2 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Copyright (C) 2011-Ad Infinitum by Kohányi Róbert
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ebson
2 | *ebson* is an extensible [BSON][] encoder/decoder library written in Java. The
3 | library is extensible in the sense that the mappings between Java and BSON types
4 | are configurable and the logic to serialize custom Java types is pluggable. Its
5 | single dependency is the [Guava libraries][] by Google.
6 |
7 | ## License
8 | Released under the permissive [MIT License][].
9 |
10 | ## Author
11 | [Kohányi Róbert][].
12 |
13 | ## Download
14 | Add the library as a dependency in your project's *pom.xml* like this.
15 |
16 | ```xml
17 |
18 | com.github.kohanyirobert
19 | ebson
20 | ...
21 |
22 | ```
23 |
24 | Releases and snapshots are deployed to [Sonatype's][] [OSS repository][] (and
25 | synced to the [Central Maven Repository][] from there). To download JARs from
26 | Sonatype's repository include the following repository tag inside your Maven
27 | installation's *settings.xml* or your project's *pom.xml*.
28 |
29 | ```xml
30 |
31 | sonatype-oss
32 | https://oss.sonatype.org/content/groups/public
33 |
34 | ```
35 |
36 | ## Build
37 | As the project is managed with [Maven][] you simply clone it and issue *mvn
38 | install* or *mvn package* inside the clone's directory.
39 |
40 | ```
41 | git clone git://github.com/kohanyirobert/ebson.git
42 | cd ebson/
43 | mvn package
44 | # and/or
45 | mvn install
46 | ```
47 |
48 | ## Usage
49 | ### Serialization
50 | ```java
51 | // create documents to serialize
52 | BsonDocument document = BsonDocuments.of("key", new Date());
53 |
54 | // grab a little-endian byte buffer
55 | ByteBuffer buffer = ByteBuffer.allocate(32).order(ByteOrder.LITTLE_ENDIAN);
56 |
57 | // use the documents utility class to write the document into the buffer
58 | BsonDocuments.writeTo(buffer, document);
59 |
60 | // use the serialized data
61 | buffer.flip();
62 | ```
63 |
64 | ### Deserialization
65 | ```java
66 | // given the previous buffer
67 | BsonDocument newDocument = BsonDocuments.readFrom(buffer);
68 |
69 | // prints true
70 | System.out.println(document.equals(newDocument));
71 | ```
72 |
73 | ### Extensibility
74 | ```java
75 | // to use joda-time's date-time instead of java's date supply
76 | // a predicate (to test whether an input class is compatible with
77 | // date-time or not) for the appropriate bson type
78 | BsonObject.UTC_DATE_TIME.predicate(new Predicate>() {
79 | @Override public boolean apply(Class> input) {
80 | return input == null ? false : DateTime.class.isAssignableFrom(input);
81 | }
82 | });
83 |
84 | // register a writer with the same bson type which is
85 | // able to serialize date-times into byte buffers
86 | BsonObject.UTC_DATE_TIME.writer(new BsonWriter() {
87 | @Override public void writeTo(ByteBuffer buffer, Object reference) {
88 | buffer.putLong(((DateTime) reference).getMillis());
89 | }
90 | });
91 |
92 | // finally register a reader to do all this ass backwards
93 | BsonObject.UTC_DATE_TIME.reader(new BsonReader() {
94 | @Override public Object readFrom(ByteBuffer buffer) {
95 | return new DateTime(buffer.getLong());
96 | }
97 | });
98 | ```
99 |
100 | [BSON]: http://bsonspec.org
101 | [Guava libraries]: http://code.google.com/p/guava-libraries
102 | [Kohányi Róbert]: http://kohanyirobert.github.com
103 | [MIT License]: https://raw.github.com/kohanyirobert/ebson/master/LICENSE.txt
104 | [Sonatype's]: http://sonatype.com
105 | [OSS repository]: https://oss.sonatype.org
106 | [Central Maven Repository]: http://search.maven.org
107 | [Maven]: http://maven.apache.org
108 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.github.kohanyirobert
7 | ebson
8 | 0.4-SNAPSHOT
9 |
10 | ebson
11 |
12 | Extensible BSON encoder/decoder library written
13 | in Java with pluggable Java-to-BSON type mappings.
14 |
15 | https://github.com/kohanyirobert/ebson
16 |
17 |
18 |
19 | MIT License
20 | LICENSE.txt
21 | repo
22 |
23 |
24 |
25 |
26 | scm:git:git@github.com:kohanyirobert/ebson.git
27 | scm:git:git@github.com:kohanyirobert/ebson.git
28 | git@github.com:kohanyirobert/ebson.git
29 |
30 |
31 |
32 |
33 | sonatype-nexus-staging
34 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
35 |
36 |
37 | sonatype-nexus-snapshots
38 | https://oss.sonatype.org/content/repositories/snapshots/
39 |
40 |
41 |
42 |
43 |
44 | kohanyi.robert
45 | Kohányi Róbert
46 | kohanyi.robert@gmail.com
47 |
48 | owner
49 | developer
50 |
51 | +1
52 |
53 |
54 |
55 |
56 | UTF-8
57 | ${project.build.sourceEncoding}
58 |
59 |
60 |
61 |
62 | com.google.guava
63 | guava
64 | 13.0.1
65 |
66 |
67 | com.google.code.findbugs
68 | jsr305
69 | 2.0.1
70 |
71 |
72 | junit
73 | junit
74 | 4.10
75 | test
76 |
77 |
78 | org.mongodb
79 | mongo-java-driver
80 | 2.9.1
81 | test
82 |
83 |
84 |
85 |
86 |
87 |
88 | ${project.basedir}
89 | ${project.build.outputDirectory}/META-INF
90 | false
91 |
92 | LICENSE.txt
93 | README.md
94 |
95 |
96 |
97 |
98 |
99 |
100 | org.apache.maven.plugins
101 | maven-compiler-plugin
102 | 2.5.1
103 |
104 | 1.7
105 | 1.7
106 |
107 |
108 |
109 | org.apache.maven.plugins
110 | maven-source-plugin
111 | 2.2
112 |
113 |
114 | attach-sources
115 |
116 | jar
117 |
118 |
119 |
120 |
121 |
122 | org.apache.maven.plugins
123 | maven-javadoc-plugin
124 | 2.9
125 |
126 |
127 | attach-javadocs
128 |
129 | jar
130 |
131 |
132 |
133 |
134 |
135 | http://docs.guava-libraries.googlecode.com/git/javadoc
136 |
137 |
138 |
139 |
140 | org.apache.maven.plugins
141 | maven-checkstyle-plugin
142 | 2.9.1
143 |
144 |
145 | checkstyle-main
146 | verify
147 |
148 | check
149 |
150 |
151 | https://raw.github.com/kohanyirobert/checkstyle/master/main.xml
152 |
153 |
154 |
155 | checkstyle-test
156 | verify
157 |
158 | check
159 |
160 |
161 | https://raw.github.com/kohanyirobert/checkstyle/master/test.xml
162 | true
163 |
164 |
165 |
166 |
167 | true
168 | true
169 | false
170 |
171 |
172 |
173 | org.apache.maven.plugins
174 | maven-gpg-plugin
175 | 1.4
176 |
177 |
178 | sign-artifacts
179 | verify
180 |
181 | sign
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
--------------------------------------------------------------------------------
/src/main/java/com/github/kohanyirobert/ebson/BasicObjectId.java:
--------------------------------------------------------------------------------
1 | package com.github.kohanyirobert.ebson;
2 |
3 | import com.google.common.base.Objects;
4 |
5 | import java.nio.ByteBuffer;
6 | import java.nio.ByteOrder;
7 |
8 | import javax.xml.bind.DatatypeConverter;
9 |
10 | final class BasicObjectId implements BsonObjectId {
11 |
12 | private static final int TIME_LENGTH = 4;
13 | private static final int MACHINE_ID_LENGTH = 3;
14 | private static final int PROCESS_ID_LENGTH = 2;
15 | private static final int INCREMENT_LENGTH = 3;
16 | private static final int OBJECT_ID_LENGTH =
17 | TIME_LENGTH + MACHINE_ID_LENGTH + PROCESS_ID_LENGTH + INCREMENT_LENGTH;
18 |
19 | private final ByteBuffer time;
20 | private final ByteBuffer machineId;
21 | private final ByteBuffer processId;
22 | private final ByteBuffer increment;
23 |
24 | private final ByteBuffer objectId = ByteBuffer.allocate(OBJECT_ID_LENGTH).order(ByteOrder.BIG_ENDIAN);
25 |
26 | BasicObjectId(ByteBuffer buffer) {
27 | int oldPosition = buffer.position();
28 |
29 | int oldLimit = buffer.limit();
30 | buffer.limit(buffer.position() + OBJECT_ID_LENGTH);
31 | objectId.put(buffer).flip();
32 | buffer.limit(oldLimit);
33 |
34 | assert buffer.position() == oldPosition + OBJECT_ID_LENGTH;
35 |
36 | time = ByteBuffer.wrap(objectId.array(), 0, TIME_LENGTH).order(ByteOrder.BIG_ENDIAN);
37 | machineId = ByteBuffer.wrap(objectId.array(), TIME_LENGTH, MACHINE_ID_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
38 | processId = ByteBuffer.wrap(objectId.array(), MACHINE_ID_LENGTH, PROCESS_ID_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
39 | increment = ByteBuffer.wrap(objectId.array(), PROCESS_ID_LENGTH, INCREMENT_LENGTH).order(ByteOrder.BIG_ENDIAN);
40 | }
41 |
42 | @Override
43 | public ByteBuffer objectId() {
44 | return objectId.asReadOnlyBuffer();
45 | }
46 |
47 | @Override
48 | public ByteBuffer time() {
49 | return time.asReadOnlyBuffer();
50 | }
51 |
52 | @Override
53 | public ByteBuffer machineId() {
54 | return machineId.asReadOnlyBuffer();
55 | }
56 |
57 | @Override
58 | public ByteBuffer processId() {
59 | return processId.asReadOnlyBuffer();
60 | }
61 |
62 | @Override
63 | public ByteBuffer increment() {
64 | return increment.asReadOnlyBuffer();
65 | }
66 |
67 | @Override
68 | public int hashCode() {
69 | return Objects.hashCode(objectId);
70 | }
71 |
72 | @Override
73 | public boolean equals(Object object) {
74 | if (object instanceof BsonObjectId) {
75 | BsonObjectId other = (BsonObjectId) object;
76 | return objectId.equals(other.objectId());
77 | }
78 | return false;
79 | }
80 |
81 | @Override
82 | public String toString() {
83 | return DatatypeConverter.printHexBinary(objectId.array()).toLowerCase();
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/main/java/com/github/kohanyirobert/ebson/BasicSymbol.java:
--------------------------------------------------------------------------------
1 | package com.github.kohanyirobert.ebson;
2 |
3 | import com.google.common.base.Objects;
4 | import com.google.common.primitives.Ints;
5 |
6 | import java.nio.ByteBuffer;
7 |
8 | import javax.xml.bind.DatatypeConverter;
9 |
10 | final class BasicSymbol implements BsonSymbol {
11 |
12 | private final ByteBuffer symbol;
13 |
14 | BasicSymbol(ByteBuffer buffer) {
15 | int oldPosition = buffer.position();
16 |
17 | int symbolLength = buffer.getInt();
18 | symbol = ByteBuffer.allocate(symbolLength - 1);
19 |
20 | int oldLimit = buffer.limit();
21 | buffer.limit(buffer.position() + symbolLength - 1);
22 | symbol.put(buffer).flip();
23 | buffer.limit(buffer.limit() + 1);
24 | buffer.get();
25 | buffer.limit(oldLimit);
26 |
27 | assert buffer.position() == Ints.BYTES + oldPosition + symbolLength;
28 | }
29 |
30 | @Override
31 | public ByteBuffer symbol() {
32 | return symbol.asReadOnlyBuffer();
33 | }
34 |
35 | @Override
36 | public int hashCode() {
37 | return Objects.hashCode(symbol);
38 | }
39 |
40 | @Override
41 | public boolean equals(Object object) {
42 | if (object instanceof BasicSymbol) {
43 | BasicSymbol other = (BasicSymbol) object;
44 | return symbol.equals(other.symbol());
45 | }
46 | return false;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return DatatypeConverter.printHexBinary(symbol.array()).toLowerCase();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/github/kohanyirobert/ebson/BasicTimestamp.java:
--------------------------------------------------------------------------------
1 | package com.github.kohanyirobert.ebson;
2 |
3 | import com.google.common.base.Objects;
4 |
5 | import java.nio.ByteBuffer;
6 | import java.nio.ByteOrder;
7 |
8 | import javax.xml.bind.DatatypeConverter;
9 |
10 | final class BasicTimestamp implements BsonTimestamp {
11 |
12 | private static final int TIME_LENGTH = 4;
13 | private static final int INCREMENT_LENGTH = 4;
14 | private static final int TIMESTAMP_LENGTH = TIME_LENGTH + INCREMENT_LENGTH;
15 |
16 | private final ByteBuffer time;
17 | private final ByteBuffer increment;
18 |
19 | private final ByteBuffer timestamp = ByteBuffer.allocate(TIMESTAMP_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
20 |
21 | BasicTimestamp(ByteBuffer buffer) {
22 | int oldPosition = buffer.position();
23 |
24 | int oldLimit = buffer.limit();
25 | buffer.limit(buffer.position() + TIMESTAMP_LENGTH);
26 | timestamp.put(buffer).flip();
27 | buffer.limit(oldLimit);
28 |
29 | assert buffer.position() == oldPosition + TIMESTAMP_LENGTH;
30 |
31 | time = ByteBuffer.wrap(timestamp.array(), 0, TIME_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
32 | increment = ByteBuffer.wrap(timestamp.array(), INCREMENT_LENGTH, TIME_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
33 | }
34 |
35 | @Override
36 | public ByteBuffer timestamp() {
37 | return timestamp.asReadOnlyBuffer();
38 | }
39 |
40 | @Override
41 | public ByteBuffer time() {
42 | return time.asReadOnlyBuffer();
43 | }
44 |
45 | @Override
46 | public ByteBuffer increment() {
47 | return increment.asReadOnlyBuffer();
48 | }
49 |
50 | @Override
51 | public int hashCode() {
52 | return Objects.hashCode(timestamp);
53 | }
54 |
55 | @Override
56 | public boolean equals(Object object) {
57 | if (object instanceof BsonTimestamp) {
58 | BsonTimestamp other = (BsonTimestamp) object;
59 | return timestamp.equals(other.timestamp());
60 | }
61 | return false;
62 | }
63 |
64 | @Override
65 | public String toString() {
66 | return DatatypeConverter.printHexBinary(timestamp.array()).toLowerCase();
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/com/github/kohanyirobert/ebson/BsonBinary.java:
--------------------------------------------------------------------------------
1 | package com.github.kohanyirobert.ebson;
2 |
3 | import com.google.common.base.Preconditions;
4 | import com.google.common.base.Predicate;
5 | import com.google.common.base.Predicates;
6 |
7 | import javax.annotation.Nullable;
8 |
9 | /**
10 | * Representation of a BSON binary
11 | * (sub-)type.
12 | */
13 | public enum BsonBinary {
14 |
15 | /**
16 | * Generic binary.
17 | *
18 | * Note: the most commonly used binary sub-type and should be the
19 | * 'default' for drivers and tools.
20 | *
21 | */
22 | GENERIC(BsonBytes.GENERIC, DefaultPredicate.GENERIC, DefaultReader.GENERIC,
23 | DefaultWriter.GENERIC),
24 |
25 | /**
26 | * Function binary.
27 | */
28 | FUNCTION(BsonBytes.FUNCTION),
29 |
30 | /**
31 | * Old binary.
32 | *
33 | * Note: this used to be the default subtype, but was deprecated in
34 | * favor of the generic binary type.
35 | *
36 | */
37 | OLD(BsonBytes.OLD),
38 |
39 | /**
40 | * UUID binary.
41 | */
42 | UUID(BsonBytes.UUID),
43 |
44 | /**
45 | * MD5 binary.
46 | */
47 | MD5(BsonBytes.MD5),
48 |
49 | /**
50 | * User-defined binary.
51 | */
52 | USER(BsonBytes.USER);
53 |
54 | private final byte terminal;
55 |
56 | private Predicate> predicate;
57 | private BsonReader reader;
58 | private BsonWriter writer;
59 |
60 | private BsonBinary(byte terminal) {
61 | this(terminal, Predicates.>alwaysFalse(), null, null);
62 | }
63 |
64 | private BsonBinary(byte terminal, Predicate> predicate,
65 | BsonReader reader, BsonWriter writer) {
66 | this.terminal = terminal;
67 | this.predicate = predicate;
68 | this.reader = reader;
69 | this.writer = writer;
70 | }
71 |
72 | /**
73 | * Returns this binary's associated terminal.
74 | *
75 | * @return this binary's associated terminal
76 | */
77 | public byte terminal() {
78 | return terminal;
79 | }
80 |
81 | /**
82 | * Returns this binary's associated {@linkplain Predicate predicate}.
83 | *
84 | * @return this binary's associated predicate
85 | * @throws IllegalStateException if this binary does not have an associated
86 | * predicate
87 | */
88 | public Predicate> predicate() {
89 | Preconditions.checkState(predicate != null, "'%s' does not have an associated predicate", this);
90 | return predicate;
91 | }
92 |
93 | /**
94 | * Associates {@link Predicate predicate} with this binary.
95 | *
96 | * @param predicate the predicate to be associated with this binary
97 | */
98 | public void predicate(Predicate> predicate) {
99 | Preconditions.checkNotNull(predicate, "cannot associate a null predicate with '%s'", this);
100 | this.predicate = predicate;
101 | }
102 |
103 | /**
104 | * Returns this binary's associated {@linkplain BsonReader reader}.
105 | *
106 | * @return this binary's associated reader
107 | * @throws IllegalStateException if this binary does not have an associated
108 | * reader
109 | */
110 | public BsonReader reader() {
111 | Preconditions.checkState(reader != null, "'%s' does not have an associated reader", this);
112 | return reader;
113 | }
114 |
115 | /**
116 | * Associates {@link BsonReader reader} with this binary.
117 | *
118 | * @param reader the reader to be associated with this binary
119 | */
120 | public void reader(BsonReader reader) {
121 | Preconditions.checkNotNull(reader, "cannot associate a null reader with '%s'", this);
122 | this.reader = reader;
123 | }
124 |
125 | /**
126 | * Returns this binary's associated {@linkplain BsonWriter writer}.
127 | *
128 | * @return this binary's associated writer
129 | * @throws IllegalStateException if this binary does not have an associated
130 | * writer
131 | */
132 | public BsonWriter writer() {
133 | Preconditions.checkState(writer != null, "'%s' does not have an associated writer", this);
134 | return writer;
135 | }
136 |
137 | /**
138 | * Associates {@link BsonWriter writer} with this binary.
139 | *
140 | * @param writer the writer to be associated with this binary
141 | */
142 | public void writer(BsonWriter writer) {
143 | Preconditions.checkNotNull(writer, "cannot associate a null writer with '%s'", this);
144 | this.writer = writer;
145 | }
146 |
147 | /**
148 | * Returns the binary representing {@code clazz}.
149 | *
150 | * @param clazz the class to return a binary representation for
151 | * @return the binary representing {@code clazz}
152 | * @throws IllegalArgumentException if no binary representing {@code clazz}
153 | * was found
154 | */
155 | public static BsonBinary find(@Nullable Class> clazz) {
156 | for (BsonBinary binary : values()) {
157 | if (binary.predicate().apply(clazz)) {
158 | return binary;
159 | }
160 | }
161 | throw new IllegalArgumentException(String.format("no binary "
162 | + "representing the '%s' type value was found", clazz));
163 | }
164 |
165 | /**
166 | * Returns the binary representing {@code terminal}.
167 | *
168 | * @param terminal the terminal to return a binary representation for
169 | * @return the binary representing {@code terminal}
170 | * @throws IllegalArgumentException if no binary representing {@code terminal}
171 | * was found
172 | */
173 | public static BsonBinary find(byte terminal) {
174 | for (BsonBinary binary : values()) {
175 | if (binary.terminal() - terminal == 0) {
176 | return binary;
177 | }
178 | }
179 | throw new IllegalArgumentException(String.format("no binary representing "
180 | + "the '%s' terminal value was found", Byte.valueOf(terminal)));
181 | }
182 | }
183 |
--------------------------------------------------------------------------------
/src/main/java/com/github/kohanyirobert/ebson/BsonBytes.java:
--------------------------------------------------------------------------------
1 | package com.github.kohanyirobert.ebson;
2 |
3 | /**
4 | * Frequently used byte values.
5 | */
6 | public final class BsonBytes {
7 |
8 | /**
9 | * EOF (-1).
10 | */
11 | public static final byte EOF = -1;
12 |
13 | /**
14 | * EOO (0).
15 | */
16 | public static final byte EOO = 0;
17 |
18 | /**
19 | * DOUBLE (1).
20 | */
21 | public static final byte DOUBLE = 1;
22 |
23 | /**
24 | * STRING (2).
25 | */
26 | public static final byte STRING = 2;
27 |
28 | /**
29 | * EMBEDDED (3).
30 | */
31 | public static final byte EMBEDDED = 3;
32 |
33 | /**
34 | * ARRAY (4).
35 | */
36 | public static final byte ARRAY = 4;
37 |
38 | /**
39 | * BINARY (5).
40 | */
41 | public static final byte BINARY = 5;
42 |
43 | /**
44 | * GENERIC (0).
45 | */
46 | public static final byte GENERIC = 0;
47 |
48 | /**
49 | * FUNCTION (1).
50 | */
51 | public static final byte FUNCTION = 1;
52 |
53 | /**
54 | * OLD (2).
55 | */
56 | public static final byte OLD = 2;
57 |
58 | /**
59 | * UUID (3).
60 | */
61 | public static final byte UUID = 3;
62 |
63 | /**
64 | * MD5 (5).
65 | */
66 | public static final byte MD5 = 5;
67 |
68 | /**
69 | * USER (128).
70 | */
71 | public static final byte USER = (byte) 128;
72 |
73 | /**
74 | * UNDEFINED (6).
75 | *
76 | * @deprecated See the BSON specification
77 | * for details.
78 | */
79 | @Deprecated
80 | public static final byte UNDEFINED = 6;
81 |
82 | /**
83 | * OBJECT_ID (7).
84 | */
85 | public static final byte OBJECT_ID = 7;
86 |
87 | /**
88 | * BOOLEAN (8).
89 | */
90 | public static final byte BOOLEAN = 8;
91 |
92 | /**
93 | * FALSE (0).
94 | */
95 | public static final byte FALSE = 0;
96 |
97 | /**
98 | * TRUE (1).
99 | */
100 | public static final byte TRUE = 1;
101 |
102 | /**
103 | * UTC_DATE_TIME (9).
104 | */
105 | public static final byte UTC_DATE_TIME = 9;
106 |
107 | /**
108 | * NULL (10).
109 | */
110 | public static final byte NULL = 10;
111 |
112 | /**
113 | * REGULAR_EXPRESSION (11).
114 | */
115 | public static final byte REGULAR_EXPRESSION = 11;
116 |
117 | /**
118 | * DB_POINTER (12).
119 | *
120 | * @deprecated See the following
122 | * link for more details.
123 | */
124 | @Deprecated
125 | public static final byte DB_POINTER = 12;
126 |
127 | /**
128 | * JAVASCRIPT_CODE (13).
129 | */
130 | public static final byte JAVASCRIPT_CODE = 13;
131 |
132 | /**
133 | * SYMBOL (14).
134 | */
135 | public static final byte SYMBOL = 14;
136 |
137 | /**
138 | * JAVASCRIPT_CODE_WITH_SCOPE (15).
139 | */
140 | public static final byte JAVASCRIPT_CODE_WITH_SCOPE = 15;
141 |
142 | /**
143 | * INT32 (16).
144 | */
145 | public static final byte INT32 = 16;
146 |
147 | /**
148 | * TIMESTAMP (17).
149 | */
150 | public static final byte TIMESTAMP = 17;
151 |
152 | /**
153 | * INT64 (18).
154 | */
155 | public static final byte INT64 = 18;
156 |
157 | /**
158 | * MAX_KEY (127).
159 | */
160 | public static final byte MAX_KEY = 127;
161 |
162 | /**
163 | * MIN_KEY (255).
164 | */
165 | public static final byte MIN_KEY = (byte) 255;
166 |
167 | private BsonBytes() {}
168 | }
169 |
--------------------------------------------------------------------------------
/src/main/java/com/github/kohanyirobert/ebson/BsonDocument.java:
--------------------------------------------------------------------------------
1 | package com.github.kohanyirobert.ebson;
2 |
3 | import java.util.Collection;
4 | import java.util.Map;
5 | import java.util.Set;
6 |
7 | import javax.annotation.CheckForNull;
8 | import javax.annotation.Nullable;
9 |
10 | /**
11 | * Immutable representation of a BSON
12 | * document.
13 | *
14 | * Notes:
15 | *
16 | * - None of the {@linkplain Map map interface's} optional operations are
17 | * supported.
18 | * - Use the {@linkplain BsonDocuments documents utility class} to create new
19 | * documents.
20 | *
21 | *
22 | */
23 | public interface BsonDocument extends Map {
24 |
25 | /**
26 | * Returns this document's size (the number of its keys).
27 | *
28 | * @return this document's size
29 | */
30 | @Override
31 | int size();
32 |
33 | /**
34 | * Returns true if this document contains no key-value pairs;
35 | * false otherwise.
36 | *
37 | * @return true if this document contains no key-value pairs;
38 | * false otherwise
39 | */
40 | @Override
41 | boolean isEmpty();
42 |
43 | /**
44 | * Returns an immutable view of this document's keys.
45 | *
46 | * @return an immutable view of this document's keys.
47 | */
48 | @Override
49 | Set keySet();
50 |
51 | /**
52 | * Returns an immutable view of this document's values.
53 | *
54 | * @return an immutable view of this document's values
55 | */
56 | @Override
57 | Collection