Skip to content

Commit 2f23bc7

Browse files
committed
java: add CharacterTemplate
1 parent 106a281 commit 2f23bc7

File tree

4 files changed

+173
-2
lines changed

4 files changed

+173
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.msgpack.template;
2+
3+
import java.io.IOException;
4+
import org.msgpack.MessageTypeException;
5+
import org.msgpack.packer.Packer;
6+
import org.msgpack.unpacker.Unpacker;
7+
8+
/**
9+
* CharacterTemplate<br/>
10+
*
11+
* @author watabiki
12+
*/
13+
public class CharacterTemplate extends AbstractTemplate<Character> {
14+
15+
private CharacterTemplate() { }
16+
17+
@Override
18+
public void write(Packer pk, Character v, boolean required) throws IOException {
19+
if (v == null) {
20+
if (required) {
21+
throw new MessageTypeException("Attempted to write null");
22+
}
23+
pk.writeNil();
24+
return;
25+
}
26+
pk.writeInt(v.charValue());
27+
}
28+
29+
@Override
30+
public Character read(Unpacker u, Character to, boolean required) throws IOException {
31+
if (!required && u.trySkipNil()) {
32+
return null;
33+
}
34+
return (char) u.readInt();
35+
}
36+
37+
static public CharacterTemplate getInstance() {
38+
return instance;
39+
}
40+
41+
static final CharacterTemplate instance = new CharacterTemplate();
42+
}

src/main/java/org/msgpack/template/TemplateRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ private void registerTemplates() {
9595
register(double.class, DoubleTemplate.getInstance());
9696
register(Double.class, DoubleTemplate.getInstance());
9797
register(BigInteger.class, BigIntegerTemplate.getInstance());
98+
register(char.class, CharacterTemplate.getInstance());
99+
register(Character.class, CharacterTemplate.getInstance());
98100
register(boolean[].class, BooleanArrayTemplate.getInstance());
99101
register(short[].class, ShortArrayTemplate.getInstance());
100102
register(int[].class, IntegerArrayTemplate.getInstance());

src/test/java/org/msgpack/TestSet.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void testBoolean(boolean v) throws Exception {
2525
}
2626

2727
public void testBooleanArray() throws Exception {
28-
testBooleanArray(null);
28+
testBooleanArray(null);
2929
testBooleanArray(new boolean[0]);
3030
testBooleanArray(new boolean[] { true });
3131
testBooleanArray(new boolean[] { false });
@@ -278,7 +278,7 @@ public void testNil() throws Exception {
278278
}
279279

280280
public void testString() throws Exception {
281-
// TODO testString(null); // #MN considering next version
281+
// TODO testString(null); // #MN considering next version
282282
testString("");
283283
testString("a");
284284
testString("ab");
@@ -436,4 +436,16 @@ public void testDate() throws Exception {
436436
public void testDate(Date v) throws Exception {
437437
}
438438

439+
public void testCharacter() throws Exception {
440+
testCharacter(null);
441+
testCharacter('a');
442+
testCharacter('あ');
443+
testCharacter((char) 1);
444+
testCharacter(Character.MIN_VALUE);
445+
testCharacter(Character.MAX_VALUE);
446+
}
447+
448+
public void testCharacter(Character v) throws Exception {
449+
}
450+
439451
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package org.msgpack.template;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.ByteArrayOutputStream;
7+
8+
import org.junit.Test;
9+
import org.msgpack.MessagePack;
10+
import org.msgpack.TestSet;
11+
import org.msgpack.packer.BufferPacker;
12+
import org.msgpack.packer.Packer;
13+
import org.msgpack.unpacker.BufferUnpacker;
14+
import org.msgpack.unpacker.Unpacker;
15+
16+
public class TestCharacterTemplate {
17+
18+
@Test
19+
public void testPackUnpack() throws Exception {
20+
new TestPackUnpack().testCharacter();
21+
}
22+
23+
@Test
24+
public void testPackBufferUnpack() throws Exception {
25+
new TestPackBufferUnpack().testCharacter();
26+
}
27+
28+
@Test
29+
public void testBufferPackBufferUnpack() throws Exception {
30+
new TestBufferPackBufferUnpack().testCharacter();
31+
}
32+
33+
@Test
34+
public void testBufferPackUnpack() throws Exception {
35+
new TestBufferPackUnpack().testCharacter();
36+
}
37+
38+
private static class TestPackUnpack extends TestSet {
39+
@Test @Override
40+
public void testCharacter() throws Exception {
41+
super.testCharacter();
42+
}
43+
44+
@Override
45+
public void testCharacter(Character v) throws Exception {
46+
MessagePack msgpack = new MessagePack();
47+
Template<Character> tmpl = CharacterTemplate.instance;
48+
ByteArrayOutputStream out = new ByteArrayOutputStream();
49+
Packer packer = msgpack.createPacker(out);
50+
tmpl.write(packer, v);
51+
byte[] bytes = out.toByteArray();
52+
Unpacker unpacker = msgpack.createUnpacker(new ByteArrayInputStream(bytes));
53+
Character ret = tmpl.read(unpacker, null);
54+
assertEquals(v, ret);
55+
}
56+
}
57+
58+
private static class TestPackBufferUnpack extends TestSet {
59+
@Test @Override
60+
public void testCharacter() throws Exception {
61+
super.testCharacter();
62+
}
63+
64+
@Override
65+
public void testCharacter(Character v) throws Exception {
66+
MessagePack msgpack = new MessagePack();
67+
Template<Character> tmpl = CharacterTemplate.instance;
68+
ByteArrayOutputStream out = new ByteArrayOutputStream();
69+
Packer packer = msgpack.createPacker(out);
70+
tmpl.write(packer, v);
71+
byte[] bytes = out.toByteArray();
72+
BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes);
73+
Character ret = tmpl.read(unpacker, null);
74+
assertEquals(v, ret);
75+
}
76+
}
77+
78+
private static class TestBufferPackBufferUnpack extends TestSet {
79+
@Test @Override
80+
public void testCharacter() throws Exception {
81+
super.testCharacter();
82+
}
83+
84+
@Override
85+
public void testCharacter(Character v) throws Exception {
86+
MessagePack msgpack = new MessagePack();
87+
Template<Character> tmpl = CharacterTemplate.instance;
88+
BufferPacker packer = msgpack.createBufferPacker();
89+
tmpl.write(packer, v);
90+
byte[] bytes = packer.toByteArray();
91+
BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes);
92+
Character ret = tmpl.read(unpacker, null);
93+
assertEquals(v, ret);
94+
}
95+
}
96+
97+
private static class TestBufferPackUnpack extends TestSet {
98+
@Test @Override
99+
public void testCharacter() throws Exception {
100+
super.testCharacter();
101+
}
102+
103+
@Override
104+
public void testCharacter(Character v) throws Exception {
105+
MessagePack msgpack = new MessagePack();
106+
Template<Character> tmpl = CharacterTemplate.instance;
107+
BufferPacker packer = msgpack.createBufferPacker();
108+
tmpl.write(packer, v);
109+
byte[] bytes = packer.toByteArray();
110+
Unpacker unpacker = msgpack.createUnpacker(new ByteArrayInputStream(bytes));
111+
Character ret = tmpl.read(unpacker, null);
112+
assertEquals(v, ret);
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)