summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2010-09-27 03:06:06 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2010-09-27 03:06:06 +0900
commit391034a7859dac3e1c77164c46da03026ec4d743 (patch)
treef134ae7fb9709592b542ac167bcf87d9545cb114 /java
parent446a7fbd679fffd6b2b5e3c8b5673ed824aa133f (diff)
downloadmsgpack-python-391034a7859dac3e1c77164c46da03026ec4d743.tar.gz
java: adds type-conversion mechanisms
Diffstat (limited to 'java')
-rw-r--r--java/src/main/java/org/msgpack/AbstractTemplate.java27
-rw-r--r--java/src/main/java/org/msgpack/MessageConverter.java25
-rw-r--r--java/src/main/java/org/msgpack/MessagePackObject.java4
-rw-r--r--java/src/main/java/org/msgpack/MessagePacker.java25
-rw-r--r--java/src/main/java/org/msgpack/MessageUnpacker.java25
-rw-r--r--java/src/main/java/org/msgpack/Packer.java2
-rw-r--r--java/src/main/java/org/msgpack/Template.java22
-rw-r--r--java/src/main/java/org/msgpack/Templates.java45
-rw-r--r--java/src/main/java/org/msgpack/Unpacker.java15
-rw-r--r--java/src/main/java/org/msgpack/template/ByteArrayTemplate.java44
-rw-r--r--java/src/main/java/org/msgpack/template/ClassTemplate.java45
-rw-r--r--java/src/main/java/org/msgpack/template/ListTemplate.java50
-rw-r--r--java/src/main/java/org/msgpack/template/MapTemplate.java56
-rw-r--r--java/src/main/java/org/msgpack/template/StringTemplate.java44
14 files changed, 427 insertions, 2 deletions
diff --git a/java/src/main/java/org/msgpack/AbstractTemplate.java b/java/src/main/java/org/msgpack/AbstractTemplate.java
new file mode 100644
index 0000000..5b4442e
--- /dev/null
+++ b/java/src/main/java/org/msgpack/AbstractTemplate.java
@@ -0,0 +1,27 @@
+//
+// 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;
+
+import java.io.IOException;
+
+public abstract class AbstractTemplate implements Template {
+ public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
+ return convert(pac.unpackObject());
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/MessageConverter.java b/java/src/main/java/org/msgpack/MessageConverter.java
new file mode 100644
index 0000000..8388ddc
--- /dev/null
+++ b/java/src/main/java/org/msgpack/MessageConverter.java
@@ -0,0 +1,25 @@
+//
+// 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;
+
+import java.io.IOException;
+
+public interface MessageConverter {
+ public Object convert(MessagePackObject obj) throws MessageTypeException;
+}
+
diff --git a/java/src/main/java/org/msgpack/MessagePackObject.java b/java/src/main/java/org/msgpack/MessagePackObject.java
index 2424446..f7e9e0e 100644
--- a/java/src/main/java/org/msgpack/MessagePackObject.java
+++ b/java/src/main/java/org/msgpack/MessagePackObject.java
@@ -132,5 +132,9 @@ public abstract class MessagePackObject implements Cloneable, MessagePackable {
}
abstract public Object clone();
+
+ public Object convert(Template tmpl) throws MessageTypeException {
+ return tmpl.convert(this);
+ }
}
diff --git a/java/src/main/java/org/msgpack/MessagePacker.java b/java/src/main/java/org/msgpack/MessagePacker.java
new file mode 100644
index 0000000..05d4de5
--- /dev/null
+++ b/java/src/main/java/org/msgpack/MessagePacker.java
@@ -0,0 +1,25 @@
+//
+// 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;
+
+import java.io.IOException;
+
+public interface MessagePacker {
+ public void pack(Packer pk, Object target) throws IOException;
+}
+
diff --git a/java/src/main/java/org/msgpack/MessageUnpacker.java b/java/src/main/java/org/msgpack/MessageUnpacker.java
new file mode 100644
index 0000000..1817269
--- /dev/null
+++ b/java/src/main/java/org/msgpack/MessageUnpacker.java
@@ -0,0 +1,25 @@
+//
+// 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;
+
+import java.io.IOException;
+
+public interface MessageUnpacker {
+ public Object unpack(Unpacker pac) throws IOException, MessageTypeException;
+}
+
diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java
index 139b3b1..687f09c 100644
--- a/java/src/main/java/org/msgpack/Packer.java
+++ b/java/src/main/java/org/msgpack/Packer.java
@@ -474,6 +474,8 @@ public class Packer {
} else if(o instanceof BigInteger) {
return packBigInteger((BigInteger)o);
} else {
+ // FIXME check CustomPacker.get(o.getClass());
+ // FIXME check annotations
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
}
}
diff --git a/java/src/main/java/org/msgpack/Template.java b/java/src/main/java/org/msgpack/Template.java
new file mode 100644
index 0000000..71e64e0
--- /dev/null
+++ b/java/src/main/java/org/msgpack/Template.java
@@ -0,0 +1,22 @@
+//
+// 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;
+
+public interface Template extends MessageUnpacker, MessageConverter {
+}
+
diff --git a/java/src/main/java/org/msgpack/Templates.java b/java/src/main/java/org/msgpack/Templates.java
new file mode 100644
index 0000000..4c3fdae
--- /dev/null
+++ b/java/src/main/java/org/msgpack/Templates.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;
+
+import org.msgpack.template.*;
+
+public class Templates {
+ public static Template tList(Template elementTemplate) {
+ return new ListTemplate(elementTemplate);
+ }
+
+ public static Template tMap(Template keyTemplate, Template valueTemplate) {
+ return new MapTemplate(keyTemplate, valueTemplate);
+ }
+
+
+ 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/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java
index 3cae502..1b5621f 100644
--- a/java/src/main/java/org/msgpack/Unpacker.java
+++ b/java/src/main/java/org/msgpack/Unpacker.java
@@ -561,12 +561,23 @@ public class Unpacker implements Iterable<MessagePackObject> {
return impl.unpackObject();
}
+ final public boolean tryUnpackNull() throws IOException {
+ return impl.tryUnpackNull();
+ }
+
+ final public Object unpack(MessageUnpacker unpacker) throws IOException, MessageTypeException {
+ return unpacker.unpack(this);
+ }
+
final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
obj.messageUnpack(this);
}
- final public boolean tryUnpackNull() throws IOException {
- return impl.tryUnpackNull();
+ final public Object unpack(Class klass) throws MessageTypeException {
+ // FIXME check MessageUnpackable
+ // FIXME check CustomPacker
+ // FIXME check annotations
+ throw new MessageTypeException();
}
}
diff --git a/java/src/main/java/org/msgpack/template/ByteArrayTemplate.java b/java/src/main/java/org/msgpack/template/ByteArrayTemplate.java
new file mode 100644
index 0000000..fe26369
--- /dev/null
+++ b/java/src/main/java/org/msgpack/template/ByteArrayTemplate.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 ByteArrayTemplate implements Template {
+ private ByteArrayTemplate() { }
+
+ public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
+ return pac.unpackByteArray();
+ }
+
+ public Object convert(MessagePackObject from) throws MessageTypeException {
+ return from.asByteArray();
+ }
+
+ static public ByteArrayTemplate getInstance() {
+ return instance;
+ }
+
+ static final ByteArrayTemplate instance = new ByteArrayTemplate();
+
+ static {
+ CustomMessage.registerTemplate(byte[].class, instance);
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/template/ClassTemplate.java b/java/src/main/java/org/msgpack/template/ClassTemplate.java
new file mode 100644
index 0000000..b5ed854
--- /dev/null
+++ b/java/src/main/java/org/msgpack/template/ClassTemplate.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 org.msgpack.*;
+
+public class ClassTemplate implements Template {
+ private Class klass;
+
+ public ClassTemplate(Class klass) {
+ this.klass = klass;
+ }
+
+ public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
+ return pac.unpack(klass);
+ }
+
+ public Object convert(MessagePackObject from) throws MessageTypeException {
+ MessageConverter converter = CustomConverter.get(klass);
+ if(converter != null) {
+ return converter.convert(from);
+ }
+
+ // FIXME check annotations
+
+ throw new MessageTypeException();
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/template/ListTemplate.java b/java/src/main/java/org/msgpack/template/ListTemplate.java
new file mode 100644
index 0000000..54975f8
--- /dev/null
+++ b/java/src/main/java/org/msgpack/template/ListTemplate.java
@@ -0,0 +1,50 @@
+//
+// 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.util.List;
+import java.util.ArrayList;
+import java.io.IOException;
+import org.msgpack.*;
+
+public class ListTemplate implements Template {
+ private Template elementTemplate;
+
+ public ListTemplate(Template elementTemplate) {
+ this.elementTemplate = elementTemplate;
+ }
+
+ public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
+ int length = pac.unpackArray();
+ List<Object> list = new ArrayList<Object>(length);
+ for(; length > 0; length--) {
+ list.add( elementTemplate.unpack(pac) );
+ }
+ return list;
+ }
+
+ public Object convert(MessagePackObject from) throws MessageTypeException {
+ MessagePackObject[] array = from.asArray();
+ List<Object> list = new ArrayList<Object>(array.length);
+ for(MessagePackObject element : array) {
+ list.add( elementTemplate.convert(element) );
+ }
+ return list;
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/template/MapTemplate.java b/java/src/main/java/org/msgpack/template/MapTemplate.java
new file mode 100644
index 0000000..d2b4eff
--- /dev/null
+++ b/java/src/main/java/org/msgpack/template/MapTemplate.java
@@ -0,0 +1,56 @@
+//
+// 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.util.Map;
+import java.util.HashMap;
+import java.io.IOException;
+import org.msgpack.*;
+
+public class MapTemplate implements Template {
+ private Template keyTemplate;
+ private Template valueTemplate;
+
+ public MapTemplate(Template keyTemplate, Template valueTemplate) {
+ this.keyTemplate = keyTemplate;
+ this.valueTemplate = valueTemplate;
+ }
+
+ public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
+ int length = pac.unpackMap();
+ Map<Object,Object> map = new HashMap<Object,Object>(length);
+ for(; length > 0; length--) {
+ Object key = keyTemplate.unpack(pac);
+ Object value = valueTemplate.unpack(pac);
+ map.put(key, value);
+ }
+ return map;
+ }
+
+ public Object convert(MessagePackObject from) throws MessageTypeException {
+ Map<MessagePackObject,MessagePackObject> src = from.asMap();
+ Map<Object,Object> map = new HashMap();
+ for(Map.Entry<MessagePackObject,MessagePackObject> pair : src.entrySet()) {
+ Object key = keyTemplate.convert(pair.getKey());
+ Object value = valueTemplate.convert(pair.getValue());
+ map.put(key, value);
+ }
+ return map;
+ }
+}
+
diff --git a/java/src/main/java/org/msgpack/template/StringTemplate.java b/java/src/main/java/org/msgpack/template/StringTemplate.java
new file mode 100644
index 0000000..563112d
--- /dev/null
+++ b/java/src/main/java/org/msgpack/template/StringTemplate.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 StringTemplate implements Template {
+ private StringTemplate() { }
+
+ public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
+ return pac.unpackString();
+ }
+
+ public Object convert(MessagePackObject from) throws MessageTypeException {
+ return from.asString();
+ }
+
+ static public StringTemplate getInstance() {
+ return instance;
+ }
+
+ static final StringTemplate instance = new StringTemplate();
+
+ static {
+ CustomMessage.registerTemplate(String.class, instance);
+ }
+}
+