mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-23 09:14:00 +01:00
java: adds type-conversion mechanisms
This commit is contained in:
parent
446a7fbd67
commit
391034a785
27
java/src/main/java/org/msgpack/AbstractTemplate.java
Normal file
27
java/src/main/java/org/msgpack/AbstractTemplate.java
Normal file
@ -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());
|
||||
}
|
||||
}
|
||||
|
25
java/src/main/java/org/msgpack/MessageConverter.java
Normal file
25
java/src/main/java/org/msgpack/MessageConverter.java
Normal file
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
25
java/src/main/java/org/msgpack/MessagePacker.java
Normal file
25
java/src/main/java/org/msgpack/MessagePacker.java
Normal file
@ -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;
|
||||
}
|
||||
|
25
java/src/main/java/org/msgpack/MessageUnpacker.java
Normal file
25
java/src/main/java/org/msgpack/MessageUnpacker.java
Normal file
@ -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;
|
||||
}
|
||||
|
@ -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()+")");
|
||||
}
|
||||
}
|
||||
|
22
java/src/main/java/org/msgpack/Template.java
Normal file
22
java/src/main/java/org/msgpack/Template.java
Normal file
@ -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 {
|
||||
}
|
||||
|
45
java/src/main/java/org/msgpack/Templates.java
Normal file
45
java/src/main/java/org/msgpack/Templates.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
45
java/src/main/java/org/msgpack/template/ClassTemplate.java
Normal file
45
java/src/main/java/org/msgpack/template/ClassTemplate.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
|
50
java/src/main/java/org/msgpack/template/ListTemplate.java
Normal file
50
java/src/main/java/org/msgpack/template/ListTemplate.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
56
java/src/main/java/org/msgpack/template/MapTemplate.java
Normal file
56
java/src/main/java/org/msgpack/template/MapTemplate.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
44
java/src/main/java/org/msgpack/template/StringTemplate.java
Normal file
44
java/src/main/java/org/msgpack/template/StringTemplate.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user