Skip to content

Commit 7eec8b2

Browse files
committed
Added CharacterCodec for encoding and decoding from and to a Character.
Added CharacterCodec to ValueCodecProvider. JAVA-1804
1 parent 34c2e2c commit 7eec8b2

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.codecs;
18+
19+
import org.bson.BsonInvalidOperationException;
20+
import org.bson.BsonReader;
21+
import org.bson.BsonWriter;
22+
23+
import static java.lang.String.format;
24+
import static org.bson.assertions.Assertions.notNull;
25+
26+
/**
27+
* Encodes and decodes {@code Character} objects.
28+
*
29+
* @since 3.0
30+
*/
31+
public class CharacterCodec implements Codec<Character> {
32+
@Override
33+
public void encode(final BsonWriter writer, final Character value, final EncoderContext encoderContext) {
34+
notNull("value", value);
35+
36+
writer.writeString(value.toString());
37+
}
38+
39+
@Override
40+
public Character decode(final BsonReader reader, final DecoderContext decoderContext) {
41+
String string = reader.readString();
42+
if (string.length() != 1) {
43+
throw new BsonInvalidOperationException(format("Attempting to decode the string '%s' to a character, but its length is not "
44+
+ "equal to one", string));
45+
}
46+
47+
return string.charAt(0);
48+
}
49+
50+
@Override
51+
public Class<Character> getEncoderClass() {
52+
return Character.class;
53+
}
54+
}

bson/src/main/org/bson/codecs/ValueCodecProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* <li>{@link org.bson.codecs.MaxKeyCodec}</li>
3838
* <li>{@link org.bson.codecs.CodeCodec}</li>
3939
* <li>{@link org.bson.codecs.ObjectIdCodec}</li>
40+
* <li>{@link org.bson.codecs.CharacterCodec}</li>
4041
* <li>{@link org.bson.codecs.StringCodec}</li>
4142
* <li>{@link org.bson.codecs.SymbolCodec}</li>
4243
* <li>{@link org.bson.codecs.UuidCodec}</li>
@@ -78,6 +79,7 @@ private void addCodecs() {
7879
addCodec(new MaxKeyCodec());
7980
addCodec(new CodeCodec());
8081
addCodec(new ObjectIdCodec());
82+
addCodec(new CharacterCodec());
8183
addCodec(new StringCodec());
8284
addCodec(new SymbolCodec());
8385
addCodec(new UuidCodec());
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.codecs
18+
19+
import org.bson.BsonDocument
20+
import org.bson.BsonDocumentReader
21+
import org.bson.BsonDocumentWriter
22+
import org.bson.BsonInvalidOperationException
23+
import org.bson.BsonString
24+
import spock.lang.Specification
25+
26+
class CharacterCodecSpecification extends Specification {
27+
private final CharacterCodec codec = new CharacterCodec()
28+
29+
def 'should get encoder class'() {
30+
expect:
31+
codec.encoderClass == Character
32+
}
33+
def 'when encoding a character, should throw if it is null'() {
34+
given:
35+
def writer = new BsonDocumentWriter(new BsonDocument())
36+
37+
when:
38+
codec.encode(writer, null, EncoderContext.builder().build())
39+
40+
then:
41+
thrown(IllegalArgumentException)
42+
}
43+
44+
def 'should encode a character'() {
45+
given:
46+
def writer = new BsonDocumentWriter(new BsonDocument())
47+
48+
when:
49+
writer.writeStartDocument()
50+
writer.writeName('str')
51+
codec.encode(writer, 'c' as char, EncoderContext.builder().build())
52+
writer.writeEndDocument()
53+
54+
then:
55+
writer.document == new BsonDocument('str', new BsonString('c'))
56+
}
57+
58+
def 'should decode a character'() {
59+
given:
60+
def reader = new BsonDocumentReader(new BsonDocument('str', new BsonString('c')))
61+
62+
when:
63+
reader.readStartDocument()
64+
reader.readName()
65+
def character = codec.decode(reader, DecoderContext.builder().build())
66+
67+
then:
68+
character == 'c' as char
69+
}
70+
71+
def 'when decoding a string whose length is not 1, should throw a BsonInvalidOperationException'() {
72+
given:
73+
def reader = new BsonDocumentReader(new BsonDocument('str', new BsonString('cc')))
74+
75+
when:
76+
reader.readStartDocument()
77+
reader.readName()
78+
codec.decode(reader, DecoderContext.builder().build())
79+
80+
then:
81+
thrown(BsonInvalidOperationException)
82+
}
83+
}

bson/src/test/unit/org/bson/codecs/ValueCodecProviderSpecification.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ValueCodecProviderSpecification extends Specification {
4545
provider.get(Integer, registry) instanceof IntegerCodec
4646
provider.get(Long, registry) instanceof LongCodec
4747
provider.get(Double, registry) instanceof DoubleCodec
48+
provider.get(Character, registry) instanceof CharacterCodec
4849
provider.get(String, registry) instanceof StringCodec
4950
provider.get(Date, registry) instanceof DateCodec
5051
provider.get(Byte, registry) instanceof ByteCodec
@@ -64,4 +65,4 @@ class ValueCodecProviderSpecification extends Specification {
6465
provider.get(Document, registry) == null
6566
}
6667

67-
}
68+
}

0 commit comments

Comments
 (0)