mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 13:02:13 +01:00
java: update
This commit is contained in:
parent
a97f9081a3
commit
82a5dd6cf9
@ -223,6 +223,10 @@ public class Packer {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Packer packBoolean(boolean d) throws IOException {
|
||||
return d ? packTrue() : packFalse();
|
||||
}
|
||||
|
||||
public Packer packArray(int n) throws IOException {
|
||||
if(n < 16) {
|
||||
final int d = 0x90 | n;
|
||||
|
@ -20,28 +20,13 @@ package org.msgpack;
|
||||
import java.io.Writer;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.schema.SSchemaParser;
|
||||
import org.msgpack.schema.ClassGenerator;
|
||||
//import org.msgpack.schema.ClassGenerator;
|
||||
|
||||
public abstract class Schema {
|
||||
private String expression;
|
||||
private String name;
|
||||
public Schema() { }
|
||||
|
||||
public Schema(String name) {
|
||||
this.expression = expression;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getExpression() {
|
||||
return name;
|
||||
}
|
||||
public abstract String getClassName();
|
||||
public abstract String getExpression();
|
||||
|
||||
public static Schema parse(String source) {
|
||||
return SSchemaParser.parse(source);
|
||||
@ -51,83 +36,43 @@ public abstract class Schema {
|
||||
return SSchemaParser.load(source);
|
||||
}
|
||||
|
||||
public void write(Writer output) throws IOException {
|
||||
ClassGenerator.write(this, output);
|
||||
}
|
||||
|
||||
public abstract void pack(Packer pk, Object obj) throws IOException;
|
||||
|
||||
public abstract Object convert(Object obj) throws MessageTypeException;
|
||||
|
||||
|
||||
public Object createFromNil() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object createFromBoolean(boolean v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromByte(byte v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromShort(short v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromInt(int v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromLong(long v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromFloat(float v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromDouble(double v) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
throw new RuntimeException("type error");
|
||||
throw new MessageTypeException("type error");
|
||||
}
|
||||
|
||||
/* FIXME
|
||||
public Object createFromBoolean(boolean v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromByte(byte v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromShort(short v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromInt(int v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromLong(long v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromFloat(float v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromDouble(double v) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
throw MessageTypeException.schemaMismatch(this);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
@ -561,11 +561,11 @@ public class Unpacker implements Iterable<Object> {
|
||||
return impl.unpackObject();
|
||||
}
|
||||
|
||||
final void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
|
||||
final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
|
||||
obj.messageUnpack(this);
|
||||
}
|
||||
|
||||
final boolean tryUnpackNull() throws IOException {
|
||||
final public boolean tryUnpackNull() throws IOException {
|
||||
return impl.tryUnpackNull();
|
||||
}
|
||||
}
|
||||
|
64
java/src/main/java/org/msgpack/schema/BooleanSchema.java
Normal file
64
java/src/main/java/org/msgpack/schema/BooleanSchema.java
Normal file
@ -0,0 +1,64 @@
|
||||
//
|
||||
// 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.schema;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class BooleanSchema extends Schema {
|
||||
public BooleanSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Boolean";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "boolean";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Boolean) {
|
||||
pk.packBoolean((Boolean)obj);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final boolean convertBoolean(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Boolean) {
|
||||
return (Boolean)obj;
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertBoolean(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromBoolean(boolean v) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
@ -22,59 +22,48 @@ import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class RawSchema extends Schema {
|
||||
public RawSchema() {
|
||||
super("raw");
|
||||
}
|
||||
public class ByteArraySchema extends Schema {
|
||||
public ByteArraySchema() { }
|
||||
|
||||
public String getFullName() {
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "byte[]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "raw";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
// FIXME instanceof GenericObject
|
||||
if(obj instanceof byte[]) {
|
||||
byte[] d = (byte[])obj;
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
if(!d.hasArray()) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
pk.packRaw(d.capacity());
|
||||
pk.packRawBody(d.array(), d.position(), d.capacity());
|
||||
|
||||
byte[] b = (byte[])obj;
|
||||
pk.packRaw(b.length);
|
||||
pk.packRawBody(b);
|
||||
} else if(obj instanceof String) {
|
||||
try {
|
||||
byte[] d = ((String)obj).getBytes("UTF-8");
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
byte[] b = ((String)obj).getBytes("UTF-8");
|
||||
pk.packRaw(b.length);
|
||||
pk.packRawBody(b);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
// FIXME instanceof GenericObject
|
||||
public static final byte[] convertByteArray(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof byte[]) {
|
||||
// FIXME copy?
|
||||
//byte[] d = (byte[])obj;
|
||||
//byte[] v = new byte[d.length];
|
||||
//System.arraycopy(d, 0, v, 0, d.length);
|
||||
//return v;
|
||||
return obj;
|
||||
|
||||
return (byte[])obj;
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
byte[] v = new byte[d.capacity()];
|
||||
@ -82,19 +71,22 @@ public class RawSchema extends Schema {
|
||||
d.get(v);
|
||||
d.position(pos);
|
||||
return v;
|
||||
|
||||
} else if(obj instanceof String) {
|
||||
try {
|
||||
return ((String)obj).getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertByteArray(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
byte[] d = new byte[length];
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ByteSchema extends Schema {
|
||||
public ByteSchema() {
|
||||
super("Byte");
|
||||
public ByteSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Byte";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -33,27 +36,32 @@ public class ByteSchema extends Schema {
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packByte( ((Number)obj).byteValue() );
|
||||
|
||||
short value = ((Number)obj).shortValue();
|
||||
if(value > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
pk.packByte((byte)value);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final byte convertByte(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Number) {
|
||||
short value = ((Number)obj).shortValue();
|
||||
if(value > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)value;
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Byte) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).byteValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
return convertByte(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -63,26 +71,25 @@ public class ByteSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
if(v > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
if(v > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (byte)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
if(v > Byte.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (byte)v;
|
||||
}
|
||||
}
|
||||
|
@ -77,8 +77,11 @@ public class ClassGenerator {
|
||||
for(FieldSchema f : cs.getFields()) {
|
||||
findSubclassSchema(dst, f.getSchema());
|
||||
}
|
||||
} else if(s instanceof ArraySchema) {
|
||||
ArraySchema as = (ArraySchema)s;
|
||||
} else if(s instanceof ListSchema) {
|
||||
ListSchema as = (ListSchema)s;
|
||||
findSubclassSchema(dst, as.getElementSchema(0));
|
||||
} else if(s instanceof SetSchema) {
|
||||
SetSchema as = (SetSchema)s;
|
||||
findSubclassSchema(dst, as.getElementSchema(0));
|
||||
} else if(s instanceof MapSchema) {
|
||||
MapSchema as = (MapSchema)s;
|
||||
@ -105,7 +108,7 @@ public class ClassGenerator {
|
||||
|
||||
private void writeClass() throws IOException {
|
||||
line();
|
||||
line("public final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
|
||||
line("public final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable");
|
||||
line("{");
|
||||
pushIndent();
|
||||
writeSchema();
|
||||
@ -117,7 +120,7 @@ public class ClassGenerator {
|
||||
|
||||
private void writeSubclass() throws IOException {
|
||||
line();
|
||||
line("final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
|
||||
line("final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable");
|
||||
line("{");
|
||||
pushIndent();
|
||||
writeSchema();
|
||||
@ -135,7 +138,7 @@ public class ClassGenerator {
|
||||
private void writeMemberVariables() throws IOException {
|
||||
line();
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("public "+f.getSchema().getFullName()+" "+f.getName()+";");
|
||||
line("public "+f.getSchema().getClassName()+" "+f.getName()+";");
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,7 +159,7 @@ public class ClassGenerator {
|
||||
|
||||
private void writeConstructors() throws IOException {
|
||||
line();
|
||||
line("public "+schema.getName()+"() { }");
|
||||
line("public "+schema.getClassName()+"() { }");
|
||||
}
|
||||
|
||||
private void writeAccessors() throws IOException {
|
||||
@ -195,7 +198,7 @@ public class ClassGenerator {
|
||||
line("FieldSchema[] _fields = _SCHEMA.getFields();");
|
||||
int i = 0;
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getFullName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
|
||||
line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getClassName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
|
||||
++i;
|
||||
}
|
||||
popIndent();
|
||||
@ -205,13 +208,13 @@ public class ClassGenerator {
|
||||
private void writeFactoryFunction() throws IOException {
|
||||
line();
|
||||
line("@SuppressWarnings(\"unchecked\")");
|
||||
line("public static "+schema.getName()+" createFromMessage(Object[] _message)");
|
||||
line("public static "+schema.getClassName()+" createFromMessage(Object[] _message)");
|
||||
line("{");
|
||||
pushIndent();
|
||||
line(schema.getName()+" _self = new "+schema.getName()+"();");
|
||||
line(schema.getClassName()+" _self = new "+schema.getClassName()+"();");
|
||||
int i = 0;
|
||||
for(FieldSchema f : schema.getFields()) {
|
||||
line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getFullName()+")_message["+i+"];");
|
||||
line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getClassName()+")_message["+i+"];");
|
||||
++i;
|
||||
}
|
||||
line("return _self;");
|
||||
|
@ -22,6 +22,7 @@ import java.util.List;
|
||||
import org.msgpack.*;
|
||||
|
||||
public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
protected String name;
|
||||
protected FieldSchema[] fields;
|
||||
protected List<String> imports;
|
||||
protected String namespace;
|
||||
@ -30,7 +31,7 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
public ClassSchema(
|
||||
String name, String namespace,
|
||||
List<String> imports, List<FieldSchema> fields) {
|
||||
super(name);
|
||||
this.name = name;
|
||||
this.namespace = namespace;
|
||||
this.imports = imports; // FIXME clone?
|
||||
this.fields = new FieldSchema[fields.size()];
|
||||
@ -42,6 +43,31 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
StringBuffer b = new StringBuffer();
|
||||
b.append("(class ");
|
||||
b.append(name);
|
||||
if(namespace != null) {
|
||||
b.append(" (package "+namespace+")");
|
||||
}
|
||||
for(FieldSchema f : fields) {
|
||||
b.append(" "+f.getExpression());
|
||||
}
|
||||
b.append(")");
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public boolean equals(ClassSchema o) {
|
||||
return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
|
||||
name.equals(o.name);
|
||||
}
|
||||
|
||||
public final FieldSchema[] getFields() {
|
||||
return fields;
|
||||
}
|
||||
@ -61,35 +87,5 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
|
||||
void setImports(List<String> imports) {
|
||||
this.imports = imports; // FIXME clone?
|
||||
}
|
||||
|
||||
//@Override
|
||||
//public String getFullName()
|
||||
//{
|
||||
// if(namespace == null) {
|
||||
// return getName();
|
||||
// } else {
|
||||
// return namespace+"."+getName();
|
||||
// }
|
||||
//}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
StringBuffer b = new StringBuffer();
|
||||
b.append("(class ");
|
||||
b.append(getName());
|
||||
if(namespace != null) {
|
||||
b.append(" (package "+namespace+")");
|
||||
}
|
||||
for(FieldSchema f : fields) {
|
||||
b.append(" "+f.getExpression());
|
||||
}
|
||||
b.append(")");
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public boolean equals(SpecificClassSchema o) {
|
||||
return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
|
||||
getName().equals(o.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class DoubleSchema extends Schema {
|
||||
public DoubleSchema() {
|
||||
super("Double");
|
||||
public DoubleSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Double";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -32,43 +35,30 @@ public class DoubleSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packDouble( ((Number)obj).doubleValue() );
|
||||
|
||||
if(obj instanceof Double) {
|
||||
pk.packDouble((Double)obj);
|
||||
} else if(obj instanceof Float) {
|
||||
pk.packFloat((Float)obj);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final double convertDouble(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Double) {
|
||||
return (Double)obj;
|
||||
} else if(obj instanceof Float) {
|
||||
return ((Float)obj).doubleValue();
|
||||
} else {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Double) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).doubleValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (double)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (double)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (double)v;
|
||||
return convertDouble(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class FloatSchema extends Schema {
|
||||
public FloatSchema() {
|
||||
super("Float");
|
||||
public FloatSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Float";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -32,43 +35,30 @@ public class FloatSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packFloat( ((Number)obj).floatValue() );
|
||||
|
||||
if(obj instanceof Double) {
|
||||
pk.packDouble((Double)obj);
|
||||
} else if(obj instanceof Float) {
|
||||
pk.packFloat((Float)obj);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final float convertFloat(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Double) {
|
||||
return ((Double)obj).floatValue();
|
||||
} else if(obj instanceof Float) {
|
||||
return (Float)obj;
|
||||
} else {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Float) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).floatValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromByte(byte v) {
|
||||
return (float)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return (float)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return (float)v;
|
||||
return convertFloat(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -41,10 +41,8 @@ public class GenericClassSchema extends ClassSchema {
|
||||
FieldSchema f = fields[i];
|
||||
f.getSchema().pack(pk, d.get(f.getName()));
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
@ -55,7 +53,6 @@ public class GenericClassSchema extends ClassSchema {
|
||||
if(obj instanceof Collection) {
|
||||
// FIXME optimize
|
||||
return createFromArray( ((Collection)obj).toArray() );
|
||||
|
||||
} else if(obj instanceof Map) {
|
||||
HashMap<String,Object> m = new HashMap<String,Object>(fields.length);
|
||||
Map d = (Map)obj;
|
||||
@ -65,7 +62,6 @@ public class GenericClassSchema extends ClassSchema {
|
||||
m.put(fieldName, f.getSchema().convert(d.get(fieldName)));
|
||||
}
|
||||
return m;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
|
@ -22,11 +22,13 @@ import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
//import org.msgpack.generic.*;
|
||||
|
||||
public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
|
||||
public GenericSchema() {
|
||||
super("Object");
|
||||
public GenericSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Object";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -123,70 +125,5 @@ public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public Object createFromNil() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromBoolean(boolean v) {
|
||||
return new GenericBoolean(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFromByte(byte v) {
|
||||
return new GenericByte(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromShort(short v) {
|
||||
return new GenericShort(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
return new GenericInt(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return new GenericLong(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return new GenericFloat(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return new GenericDouble(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
return new GenericRaw(b, offset, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromArray(Object[] obj) {
|
||||
// FIXME GenericArray
|
||||
return Arrays.asList(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromMap(Object[] obj) {
|
||||
GenericMap m = new GenericMap(obj.length / 2);
|
||||
int i = 0;
|
||||
while(i < obj.length) {
|
||||
Object k = obj[i++];
|
||||
Object v = obj[i++];
|
||||
m.put(k, v);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class IntSchema extends Schema {
|
||||
public IntSchema() {
|
||||
super("Integer");
|
||||
public IntSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Integer";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -33,27 +36,38 @@ public class IntSchema extends Schema {
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packInt( ((Number)obj).intValue() );
|
||||
|
||||
int value = ((Number)obj).intValue();
|
||||
if(value >= Short.MAX_VALUE) {
|
||||
long lvalue = ((Number)obj).longValue();
|
||||
if(lvalue > Integer.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
pk.packInt(value);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final int convertInt(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Number) {
|
||||
int value = ((Number)obj).intValue();
|
||||
if(value >= Integer.MAX_VALUE) {
|
||||
long lvalue = ((Number)obj).longValue();
|
||||
if(lvalue > Integer.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Integer) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).intValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
return convertInt(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -73,16 +87,9 @@ public class IntSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
if(v > Integer.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (int)v;
|
||||
}
|
||||
}
|
||||
|
@ -22,37 +22,32 @@ import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.RandomAccess;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ArraySchema extends Schema implements IArraySchema {
|
||||
public class ListSchema extends Schema implements IArraySchema {
|
||||
private Schema elementSchema;
|
||||
|
||||
public ArraySchema(Schema elementSchema)
|
||||
{
|
||||
super("array");
|
||||
public ListSchema(Schema elementSchema) {
|
||||
this.elementSchema = elementSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName()
|
||||
{
|
||||
return "List<"+elementSchema.getFullName()+">";
|
||||
public String getClassName() {
|
||||
return "List<"+elementSchema.getClassName()+">";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression()
|
||||
{
|
||||
public String getExpression() {
|
||||
return "(array "+elementSchema.getExpression()+")";
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void pack(Packer pk, Object obj) throws IOException
|
||||
{
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof List) {
|
||||
ArrayList<Object> d = (ArrayList<Object>)obj;
|
||||
List<Object> d = (List<Object>)obj;
|
||||
pk.packArray(d.size());
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
@ -63,62 +58,53 @@ public class ArraySchema extends Schema implements IArraySchema {
|
||||
elementSchema.pack(pk, e);
|
||||
}
|
||||
}
|
||||
|
||||
} else if(obj instanceof Set) {
|
||||
Set<Object> d = (Set<Object>)obj;
|
||||
pk.packArray(d.size());
|
||||
for(Object e : d) {
|
||||
elementSchema.pack(pk, e);
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object obj) throws MessageTypeException
|
||||
{
|
||||
if(obj instanceof List) {
|
||||
List d = (List)obj;
|
||||
ArrayList ar = new ArrayList(d.size());
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
ar.add( elementSchema.convert(d.get(i)) );
|
||||
}
|
||||
} else {
|
||||
for(Object e : d) {
|
||||
ar.add( elementSchema.convert(e) );
|
||||
}
|
||||
}
|
||||
return ar;
|
||||
|
||||
} else if(obj instanceof Collection) {
|
||||
Collection d = (Collection)obj;
|
||||
ArrayList ar = new ArrayList(d.size());
|
||||
for(Object e : (Collection)obj) {
|
||||
ar.add( elementSchema.convert(e) );
|
||||
}
|
||||
return ar;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
public static final <T> List<T> convertList(Object obj,
|
||||
Schema elementSchema, List<T> dest) throws MessageTypeException {
|
||||
if(!(obj instanceof List)) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
List<Object> d = (List<Object>)obj;
|
||||
if(dest == null) {
|
||||
dest = new ArrayList<T>(d.size());
|
||||
}
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
dest.add( (T)elementSchema.convert(d.get(i)) );
|
||||
}
|
||||
} else {
|
||||
for(Object e : d) {
|
||||
dest.add( (T)elementSchema.convert(e) );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getElementSchema(int index)
|
||||
{
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertList(obj, elementSchema, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getElementSchema(int index) {
|
||||
return elementSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromArray(Object[] obj)
|
||||
{
|
||||
public Object createFromArray(Object[] obj) {
|
||||
return Arrays.asList(obj);
|
||||
}
|
||||
}
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class LongSchema extends Schema {
|
||||
public LongSchema() {
|
||||
super("Long");
|
||||
public LongSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Long";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -33,27 +36,25 @@ public class LongSchema extends Schema {
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packLong( ((Number)obj).longValue() );
|
||||
|
||||
long value = ((Number)obj).longValue();
|
||||
pk.packLong(value);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final long convertLong(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Number) {
|
||||
return ((Number)obj).longValue();
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Long) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).longValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
return convertLong(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -75,15 +76,5 @@ public class LongSchema extends Schema {
|
||||
public Object createFromLong(long v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (long)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
return (long)v;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,14 +27,13 @@ public class MapSchema extends Schema implements IMapSchema {
|
||||
private Schema valueSchema;
|
||||
|
||||
public MapSchema(Schema keySchema, Schema valueSchema) {
|
||||
super("map");
|
||||
this.keySchema = keySchema;
|
||||
this.valueSchema = valueSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return "HashList<"+keySchema.getFullName()+", "+valueSchema.getFullName()+">";
|
||||
public String getClassName() {
|
||||
return "Map<"+keySchema.getClassName()+", "+valueSchema.getClassName()+">";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -52,29 +51,33 @@ public class MapSchema extends Schema implements IMapSchema {
|
||||
keySchema.pack(pk, e.getKey());
|
||||
valueSchema.pack(pk, e.getValue());
|
||||
}
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Map) {
|
||||
Map<Object,Object> d = (Map<Object,Object>)obj;
|
||||
Map<Object,Object> m = new HashMap<Object,Object>();
|
||||
for(Map.Entry<Object,Object> e : d.entrySet()) {
|
||||
m.put(keySchema.convert(e.getKey()), valueSchema.convert(e.getValue()));
|
||||
}
|
||||
return m;
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
public static final <K,V> Map<K,V> convertMap(Object obj,
|
||||
Schema keySchema, Schema valueSchema, Map<K,V> dest) throws MessageTypeException {
|
||||
if(!(obj instanceof Map)) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
Map<Object,Object> d = (Map<Object,Object>)obj;
|
||||
if(dest == null) {
|
||||
dest = new HashMap<K,V>(d.size());
|
||||
}
|
||||
for(Map.Entry<Object,Object> e : d.entrySet()) {
|
||||
dest.put((K)keySchema.convert(e.getKey()),
|
||||
(V)valueSchema.convert(e.getValue()));
|
||||
}
|
||||
return (Map<K,V>)d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertMap(obj, keySchema, valueSchema, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -90,14 +93,14 @@ public class MapSchema extends Schema implements IMapSchema {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object createFromMap(Object[] obj) {
|
||||
HashMap m = new HashMap(obj.length / 2);
|
||||
HashMap dest = new HashMap(obj.length / 2);
|
||||
int i = 0;
|
||||
while(i < obj.length) {
|
||||
Object k = obj[i++];
|
||||
Object v = obj[i++];
|
||||
m.put(k, v);
|
||||
dest.put(k, v);
|
||||
}
|
||||
return m;
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -140,7 +140,7 @@ public class SSchemaParser {
|
||||
if(type.equals("string")) {
|
||||
return new StringSchema();
|
||||
} else if(type.equals("raw")) {
|
||||
return new RawSchema();
|
||||
return new ByteArraySchema();
|
||||
} else if(type.equals("byte")) {
|
||||
return new ByteSchema();
|
||||
} else if(type.equals("short")) {
|
||||
@ -163,11 +163,13 @@ public class SSchemaParser {
|
||||
if(type.equals("class")) {
|
||||
return parseClass(exp);
|
||||
} else if(type.equals("array")) {
|
||||
return parseArray(exp);
|
||||
return parseList(exp);
|
||||
} else if(type.equals("set")) {
|
||||
return parseSet(exp);
|
||||
} else if(type.equals("map")) {
|
||||
return parseMap(exp);
|
||||
} else {
|
||||
throw new RuntimeException("class, array or map is expected but got '"+type+"': "+exp);
|
||||
throw new RuntimeException("class, list, set or map is expected but got '"+type+"': "+exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -209,12 +211,20 @@ public class SSchemaParser {
|
||||
}
|
||||
}
|
||||
|
||||
private ArraySchema parseArray(SExp exp) {
|
||||
private ListSchema parseList(SExp exp) {
|
||||
if(exp.size() != 2) {
|
||||
throw new RuntimeException("array is (array ELEMENT_TYPE): "+exp);
|
||||
throw new RuntimeException("list is (list ELEMENT_TYPE): "+exp);
|
||||
}
|
||||
Schema elementType = readType(exp.getTuple(1));
|
||||
return new ArraySchema(elementType);
|
||||
return new ListSchema(elementType);
|
||||
}
|
||||
|
||||
private SetSchema parseSet(SExp exp) {
|
||||
if(exp.size() != 2) {
|
||||
throw new RuntimeException("list is (list ELEMENT_TYPE): "+exp);
|
||||
}
|
||||
Schema elementType = readType(exp.getTuple(1));
|
||||
return new SetSchema(elementType);
|
||||
}
|
||||
|
||||
private MapSchema parseMap(SExp exp) {
|
||||
|
115
java/src/main/java/org/msgpack/schema/SetSchema.java
Normal file
115
java/src/main/java/org/msgpack/schema/SetSchema.java
Normal file
@ -0,0 +1,115 @@
|
||||
//
|
||||
// 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.schema;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.RandomAccess;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class SetSchema extends Schema implements IArraySchema {
|
||||
private Schema elementSchema;
|
||||
|
||||
public SetSchema(Schema elementSchema) {
|
||||
this.elementSchema = elementSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Set<"+elementSchema.getClassName()+">";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "(set "+elementSchema.getExpression()+")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof List) {
|
||||
List<Object> d = (List<Object>)obj;
|
||||
pk.packArray(d.size());
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
elementSchema.pack(pk, d.get(i));
|
||||
}
|
||||
} else {
|
||||
for(Object e : d) {
|
||||
elementSchema.pack(pk, e);
|
||||
}
|
||||
}
|
||||
} else if(obj instanceof Set) {
|
||||
Set<Object> d = (Set<Object>)obj;
|
||||
pk.packArray(d.size());
|
||||
for(Object e : d) {
|
||||
elementSchema.pack(pk, e);
|
||||
}
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static final <T> Set<T> convertSet(Object obj,
|
||||
Schema elementSchema, Set<T> dest) throws MessageTypeException {
|
||||
if(!(obj instanceof List)) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
List<Object> d = (List<Object>)obj;
|
||||
if(dest == null) {
|
||||
dest = new HashSet<T>(d.size());
|
||||
}
|
||||
if(obj instanceof RandomAccess) {
|
||||
for(int i=0; i < d.size(); ++i) {
|
||||
dest.add( (T)elementSchema.convert(d.get(i)) );
|
||||
}
|
||||
} else {
|
||||
for(Object e : d) {
|
||||
dest.add( (T)elementSchema.convert(e) );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertSet(obj, elementSchema, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Schema getElementSchema(int index) {
|
||||
return elementSchema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromArray(Object[] obj) {
|
||||
Set m = new HashSet(obj.length);
|
||||
for(int i=0; i < obj.length; i++) {
|
||||
m.add(obj[i]);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,11 @@ import java.io.IOException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class ShortSchema extends Schema {
|
||||
public ShortSchema() {
|
||||
super("Short");
|
||||
public ShortSchema() { }
|
||||
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return "Short";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -33,27 +36,32 @@ public class ShortSchema extends Schema {
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
if(obj instanceof Number) {
|
||||
pk.packShort( ((Number)obj).shortValue() );
|
||||
|
||||
int value = ((Number)obj).intValue();
|
||||
if(value > Short.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
pk.packShort((short)value);
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static final short convertShort(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Number) {
|
||||
int value = ((Number)obj).intValue();
|
||||
if(value > Short.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (short)value;
|
||||
}
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof Short) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof Number) {
|
||||
return ((Number)obj).shortValue();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
return convertShort(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -68,21 +76,17 @@ public class ShortSchema extends Schema {
|
||||
|
||||
@Override
|
||||
public Object createFromInt(int v) {
|
||||
if(v > Short.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (short)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromLong(long v) {
|
||||
return (short)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromFloat(float v) {
|
||||
return (short)v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromDouble(double v) {
|
||||
if(v > Short.MAX_VALUE) {
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
return (short)v;
|
||||
}
|
||||
}
|
||||
|
@ -23,61 +23,48 @@ import java.io.UnsupportedEncodingException;
|
||||
import org.msgpack.*;
|
||||
|
||||
public class StringSchema extends Schema {
|
||||
public StringSchema() {
|
||||
super("string");
|
||||
}
|
||||
public StringSchema() { }
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
public String getClassName() {
|
||||
return "String";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExpression() {
|
||||
return "string";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(Packer pk, Object obj) throws IOException {
|
||||
// FIXME instanceof GenericObject
|
||||
if(obj instanceof String) {
|
||||
if(obj instanceof byte[]) {
|
||||
byte[] b = (byte[])obj;
|
||||
pk.packRaw(b.length);
|
||||
pk.packRawBody(b);
|
||||
} else if(obj instanceof String) {
|
||||
try {
|
||||
byte[] d = ((String)obj).getBytes("UTF-8");
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
byte[] b = ((String)obj).getBytes("UTF-8");
|
||||
pk.packRaw(b.length);
|
||||
pk.packRawBody(b);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
} else if(obj instanceof byte[]) {
|
||||
byte[] d = (byte[])obj;
|
||||
pk.packRaw(d.length);
|
||||
pk.packRawBody(d);
|
||||
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
if(!d.hasArray()) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
pk.packRaw(d.capacity());
|
||||
pk.packRawBody(d.array(), d.position(), d.capacity());
|
||||
|
||||
} else if(obj == null) {
|
||||
pk.packNil();
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
// FIXME instanceof GenericObject
|
||||
if(obj instanceof String) {
|
||||
return obj;
|
||||
|
||||
} else if(obj instanceof byte[]) {
|
||||
public static final String convertString(Object obj) throws MessageTypeException {
|
||||
if(obj instanceof byte[]) {
|
||||
try {
|
||||
return new String((byte[])obj, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
} else if(obj instanceof String) {
|
||||
return (String)obj;
|
||||
} else if(obj instanceof ByteBuffer) {
|
||||
ByteBuffer d = (ByteBuffer)obj;
|
||||
try {
|
||||
@ -91,14 +78,18 @@ public class StringSchema extends Schema {
|
||||
return new String(v, "UTF-8");
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
|
||||
} else {
|
||||
throw MessageTypeException.invalidConvert(obj, this);
|
||||
throw new MessageTypeException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object obj) throws MessageTypeException {
|
||||
return convertString(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createFromRaw(byte[] b, int offset, int length) {
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user