From 0a41b253f3a688cbdd11c2cfcb574f1333b32a0e Mon Sep 17 00:00:00 2001 From: frsyuki Date: Mon, 27 Sep 2010 04:27:44 +0900 Subject: [PATCH] java: adds templates for primitive types --- java/src/main/java/org/msgpack/Templates.java | 43 ++++++++++++++++-- .../msgpack/template/BigIntegerTemplate.java | 45 +++++++++++++++++++ .../org/msgpack/template/BooleanTemplate.java | 44 ++++++++++++++++++ .../org/msgpack/template/ByteTemplate.java | 44 ++++++++++++++++++ .../org/msgpack/template/DoubleTemplate.java | 44 ++++++++++++++++++ .../org/msgpack/template/FloatTemplate.java | 44 ++++++++++++++++++ .../org/msgpack/template/IntegerTemplate.java | 44 ++++++++++++++++++ .../org/msgpack/template/LongTemplate.java | 44 ++++++++++++++++++ .../org/msgpack/template/ShortTemplate.java | 44 ++++++++++++++++++ .../msgpack/TestReflectionPackerTemplate.java | 2 +- 10 files changed, 394 insertions(+), 4 deletions(-) create mode 100644 java/src/main/java/org/msgpack/template/BigIntegerTemplate.java create mode 100644 java/src/main/java/org/msgpack/template/BooleanTemplate.java create mode 100644 java/src/main/java/org/msgpack/template/ByteTemplate.java create mode 100644 java/src/main/java/org/msgpack/template/DoubleTemplate.java create mode 100644 java/src/main/java/org/msgpack/template/FloatTemplate.java create mode 100644 java/src/main/java/org/msgpack/template/IntegerTemplate.java create mode 100644 java/src/main/java/org/msgpack/template/LongTemplate.java create mode 100644 java/src/main/java/org/msgpack/template/ShortTemplate.java diff --git a/java/src/main/java/org/msgpack/Templates.java b/java/src/main/java/org/msgpack/Templates.java index a31dd919..222f6257 100644 --- a/java/src/main/java/org/msgpack/Templates.java +++ b/java/src/main/java/org/msgpack/Templates.java @@ -33,15 +33,52 @@ public class Templates { } - public static final Template TString = StringTemplate.getInstance(); + public static final Template TByte = ByteTemplate.getInstance(); + public static Template tByte() { + return TByte; + } + public static final Template TShort = ShortTemplate.getInstance(); + public static Template tShort() { + return TShort; + } + + public static final Template TInteger = IntegerTemplate.getInstance(); + public static Template tInteger() { + return TInteger; + } + + public static final Template TLong = LongTemplate.getInstance(); + public static Template tLong() { + return TLong; + } + + public static final Template TBigInteger = BigIntegerTemplate.getInstance(); + public static Template tBigInteger() { + return TBigInteger; + } + + public static final Template TFloat = FloatTemplate.getInstance(); + public static Template tFloat() { + return TFloat; + } + + public static final Template TDouble = DoubleTemplate.getInstance(); + public static Template tDouble() { + return TDouble; + } + + public static final Template TBoolean = BooleanTemplate.getInstance(); + public static Template tBoolean() { + return TBoolean; + } + + public static final Template TString = StringTemplate.getInstance(); public static Template tString() { return TString; } - public static final Template TByteArray = ByteArrayTemplate.getInstance(); - public static Template tByteArray() { return TByteArray; } diff --git a/java/src/main/java/org/msgpack/template/BigIntegerTemplate.java b/java/src/main/java/org/msgpack/template/BigIntegerTemplate.java new file mode 100644 index 00000000..e8a2993e --- /dev/null +++ b/java/src/main/java/org/msgpack/template/BigIntegerTemplate.java @@ -0,0 +1,45 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import java.math.BigInteger; +import org.msgpack.*; + +public class BigIntegerTemplate implements Template { + private BigIntegerTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackBigInteger(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asBigInteger(); + } + + static public BigIntegerTemplate getInstance() { + return instance; + } + + static final BigIntegerTemplate instance = new BigIntegerTemplate(); + + static { + CustomMessage.registerTemplate(BigInteger.class, instance); + } +} + diff --git a/java/src/main/java/org/msgpack/template/BooleanTemplate.java b/java/src/main/java/org/msgpack/template/BooleanTemplate.java new file mode 100644 index 00000000..0d64ecc6 --- /dev/null +++ b/java/src/main/java/org/msgpack/template/BooleanTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class BooleanTemplate implements Template { + private BooleanTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackBoolean(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asBoolean(); + } + + static public BooleanTemplate getInstance() { + return instance; + } + + static final BooleanTemplate instance = new BooleanTemplate(); + + static { + CustomMessage.registerTemplate(Boolean.class, instance); + } +} + diff --git a/java/src/main/java/org/msgpack/template/ByteTemplate.java b/java/src/main/java/org/msgpack/template/ByteTemplate.java new file mode 100644 index 00000000..8d0e6e65 --- /dev/null +++ b/java/src/main/java/org/msgpack/template/ByteTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class ByteTemplate implements Template { + private ByteTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackByte(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asByte(); + } + + static public ByteTemplate getInstance() { + return instance; + } + + static final ByteTemplate instance = new ByteTemplate(); + + static { + CustomMessage.registerTemplate(Byte.class, instance); + } +} + diff --git a/java/src/main/java/org/msgpack/template/DoubleTemplate.java b/java/src/main/java/org/msgpack/template/DoubleTemplate.java new file mode 100644 index 00000000..2e26f508 --- /dev/null +++ b/java/src/main/java/org/msgpack/template/DoubleTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class DoubleTemplate implements Template { + private DoubleTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackDouble(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asDouble(); + } + + static public DoubleTemplate getInstance() { + return instance; + } + + static final DoubleTemplate instance = new DoubleTemplate(); + + static { + CustomMessage.registerTemplate(Double.class, instance); + } +} + diff --git a/java/src/main/java/org/msgpack/template/FloatTemplate.java b/java/src/main/java/org/msgpack/template/FloatTemplate.java new file mode 100644 index 00000000..87301723 --- /dev/null +++ b/java/src/main/java/org/msgpack/template/FloatTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class FloatTemplate implements Template { + private FloatTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackFloat(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asFloat(); + } + + static public FloatTemplate getInstance() { + return instance; + } + + static final FloatTemplate instance = new FloatTemplate(); + + static { + CustomMessage.registerTemplate(Float.class, instance); + } +} + diff --git a/java/src/main/java/org/msgpack/template/IntegerTemplate.java b/java/src/main/java/org/msgpack/template/IntegerTemplate.java new file mode 100644 index 00000000..c56c0449 --- /dev/null +++ b/java/src/main/java/org/msgpack/template/IntegerTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class IntegerTemplate implements Template { + private IntegerTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackInt(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asInt(); + } + + static public IntegerTemplate getInstance() { + return instance; + } + + static final IntegerTemplate instance = new IntegerTemplate(); + + static { + CustomMessage.registerTemplate(Integer.class, instance); + } +} + diff --git a/java/src/main/java/org/msgpack/template/LongTemplate.java b/java/src/main/java/org/msgpack/template/LongTemplate.java new file mode 100644 index 00000000..a0c8210d --- /dev/null +++ b/java/src/main/java/org/msgpack/template/LongTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class LongTemplate implements Template { + private LongTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackLong(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asLong(); + } + + static public LongTemplate getInstance() { + return instance; + } + + static final LongTemplate instance = new LongTemplate(); + + static { + CustomMessage.registerTemplate(Long.class, instance); + } +} + diff --git a/java/src/main/java/org/msgpack/template/ShortTemplate.java b/java/src/main/java/org/msgpack/template/ShortTemplate.java new file mode 100644 index 00000000..b3bf43be --- /dev/null +++ b/java/src/main/java/org/msgpack/template/ShortTemplate.java @@ -0,0 +1,44 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.template; + +import java.io.IOException; +import org.msgpack.*; + +public class ShortTemplate implements Template { + private ShortTemplate() { } + + public Object unpack(Unpacker pac) throws IOException, MessageTypeException { + return pac.unpackShort(); + } + + public Object convert(MessagePackObject from) throws MessageTypeException { + return from.asShort(); + } + + static public ShortTemplate getInstance() { + return instance; + } + + static final ShortTemplate instance = new ShortTemplate(); + + static { + CustomMessage.registerTemplate(Short.class, instance); + } +} + diff --git a/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java b/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java index ddf88a06..f361eb46 100644 --- a/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java +++ b/java/src/test/java/org/msgpack/TestReflectionPackerTemplate.java @@ -18,7 +18,7 @@ public class TestReflectionPackerTemplate { @Test public void testPackConvert() throws Exception { - tString(); + tString(); // FIXME link StringTemplate ByteArrayOutputStream out = new ByteArrayOutputStream();