java: adds MessagePackObject class

This commit is contained in:
frsyuki 2010-07-24 18:08:00 +09:00
parent 227c168b65
commit 2aef495d62
12 changed files with 783 additions and 0 deletions

View File

@ -0,0 +1,130 @@
//
// 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.util.List;
import java.util.Set;
import java.util.Map;
import java.math.BigInteger;
public abstract class MessagePackObject {
public boolean isNull() {
return false;
}
public boolean isBooleanType() {
return false;
}
public boolean isIntegerType() {
return false;
}
public boolean isFloatType() {
return false;
}
public boolean isArrayType() {
return false;
}
public boolean isMapType() {
return false;
}
public boolean isRawType() {
return false;
}
public boolean asBoolean() {
throw new MessageTypeException("type error");
}
public byte asByte() {
throw new MessageTypeException("type error");
}
public short asShort() {
throw new MessageTypeException("type error");
}
public int asInt() {
throw new MessageTypeException("type error");
}
public long asLong() {
throw new MessageTypeException("type error");
}
public BigInteger asBigInteger() {
throw new MessageTypeException("type error");
}
public float asFloat() {
throw new MessageTypeException("type error");
}
public double asDouble() {
throw new MessageTypeException("type error");
}
public byte[] asByteArray() {
throw new MessageTypeException("type error");
}
public String asString() {
throw new MessageTypeException("type error");
}
public MessagePackObject[] asArray() {
throw new MessageTypeException("type error");
}
public List<MessagePackObject> asList() {
throw new MessageTypeException("type error");
}
public Map<MessagePackObject, MessagePackObject> asMap() {
throw new MessageTypeException("type error");
}
public byte byteValue() {
throw new MessageTypeException("type error");
}
public short shortValue() {
throw new MessageTypeException("type error");
}
public int intValue() {
throw new MessageTypeException("type error");
}
public long longValue() {
throw new MessageTypeException("type error");
}
public float floatValue() {
throw new MessageTypeException("type error");
}
public double doubleValue() {
throw new MessageTypeException("type error");
}
}

View File

@ -0,0 +1,48 @@
//
// 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.object;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.Arrays;
import org.msgpack.*;
public class ArrayType extends MessagePackObject {
private MessagePackObject[] array;
public ArrayType(MessagePackObject[] array) {
this.array = array;
}
@Override
public boolean isArrayType() {
return true;
}
@Override
public MessagePackObject[] asArray() {
return array;
}
@Override
public List<MessagePackObject> asList() {
return Arrays.asList(array);
}
}

View File

@ -0,0 +1,92 @@
//
// 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.object;
import java.math.BigInteger;
import org.msgpack.*;
class BigIntegerTypeIMPL extends IntegerType {
private BigInteger value;
BigIntegerTypeIMPL(BigInteger vlaue) {
this.value = value;
}
@Override
public byte asByte() {
if(value.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) > 0) {
throw new MessageTypeException("type error");
}
return value.byteValue();
}
@Override
public short asShort() {
if(value.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) > 0) {
throw new MessageTypeException("type error");
}
return value.shortValue();
}
@Override
public int asInt() {
if(value.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) > 0) {
throw new MessageTypeException("type error");
}
return value.intValue();
}
@Override
public long asLong() {
if(value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
throw new MessageTypeException("type error");
}
return value.longValue();
}
@Override
public byte byteValue() {
return value.byteValue();
}
@Override
public short shortValue() {
return value.shortValue();
}
@Override
public int intValue() {
return value.intValue();
}
@Override
public long longValue() {
return value.longValue();
}
@Override
public float floatValue() {
return value.floatValue();
}
@Override
public double doubleValue() {
return value.doubleValue();
}
}

View File

@ -0,0 +1,39 @@
//
// 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.object;
import org.msgpack.*;
public class BooleanType extends MessagePackObject {
private boolean value;
public BooleanType(boolean value) {
this.value = value;
}
@Override
public boolean isBooleanType() {
return false;
}
@Override
public boolean asBoolean() {
return value;
}
}

View File

@ -0,0 +1,71 @@
//
// 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.object;
import java.math.BigInteger;
import org.msgpack.*;
class DoubleTypeIMPL extends FloatType {
private double value;
public DoubleTypeIMPL(double vlaue) {
this.value = value;
}
@Override
public float asFloat() {
// FIXME check overflow, underflow?
return (float)value;
}
@Override
public double asDouble() {
return value;
}
@Override
public byte byteValue() {
return (byte)value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
}

View File

@ -0,0 +1,28 @@
//
// 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.object;
import org.msgpack.*;
public abstract class FloatType extends MessagePackObject {
@Override
public boolean isFloatType() {
return true;
}
}

View File

@ -0,0 +1,70 @@
//
// 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.object;
import java.math.BigInteger;
import org.msgpack.*;
class FloatTypeIMPL extends FloatType {
private float value;
public FloatTypeIMPL(float vlaue) {
this.value = value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return (double)value;
}
@Override
public byte byteValue() {
return (byte)value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
}

View File

@ -0,0 +1,49 @@
//
// 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.object;
import java.math.BigInteger;
import org.msgpack.*;
public abstract class IntegerType extends MessagePackObject {
public static IntegerType create(byte value) {
return new ShortIntegerTypeIMPL((int)value);
}
public static IntegerType create(short value) {
return new ShortIntegerTypeIMPL((int)value);
}
public static IntegerType create(int value) {
return new ShortIntegerTypeIMPL(value);
}
public static IntegerType create(long value) {
return new LongIntegerTypeIMPL(value);
}
public static IntegerType create(BigInteger value) {
return new BigIntegerTypeIMPL(value);
}
@Override
public boolean isIntegerType() {
return true;
}
}

View File

@ -0,0 +1,89 @@
//
// 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.object;
import java.math.BigInteger;
import org.msgpack.*;
class LongIntegerTypeIMPL extends IntegerType {
private long value;
LongIntegerTypeIMPL(long value) {
this.value = value;
}
@Override
public byte asByte() {
if(value > (long)Byte.MAX_VALUE) {
throw new MessageTypeException("type error");
}
return (byte)value;
}
@Override
public short asShort() {
if(value > (long)Short.MAX_VALUE) {
throw new MessageTypeException("type error");
}
return (short)value;
}
@Override
public int asInt() {
if(value > (long)Integer.MAX_VALUE) {
throw new MessageTypeException("type error");
}
return (int)value;
}
@Override
public long asLong() {
return value;
}
@Override
public byte byteValue() {
return (byte)value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
}

View File

@ -0,0 +1,48 @@
//
// 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.object;
import java.util.HashMap;
import java.util.Map;
import org.msgpack.*;
public class MapType extends MessagePackObject {
MessagePackObject[] map;
public MapType(MessagePackObject[] map) {
this.map = map;
}
@Override
public boolean isMapType() {
return false;
}
@Override
public Map<MessagePackObject, MessagePackObject> asMap() {
HashMap<MessagePackObject, MessagePackObject> m = new HashMap(map.length / 2);
int i = 0;
while(i < map.length) {
MessagePackObject k = map[i++];
MessagePackObject v = map[i++];
m.put(k, v);
}
return m;
}
}

View File

@ -0,0 +1,28 @@
//
// 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.object;
import org.msgpack.*;
public class NilType extends MessagePackObject {
@Override
public boolean isNull() {
return true;
}
}

View File

@ -0,0 +1,91 @@
//
// 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.object;
import java.math.BigInteger;
import org.msgpack.*;
class ShortIntegerTypeIMPL extends IntegerType {
private int value;
ShortIntegerTypeIMPL(int value) {
this.value = value;
}
@Override
public byte asByte() {
if(value > (int)Byte.MAX_VALUE) {
throw new MessageTypeException("type error");
}
return (byte)value;
}
@Override
public short asShort() {
if(value > (int)Short.MAX_VALUE) {
throw new MessageTypeException("type error");
}
return (short)value;
}
@Override
public int asInt() {
return value;
}
@Override
public long asLong() {
return value;
}
@Override
public BigInteger asBigInteger() {
return BigInteger.valueOf((long)value);
}
@Override
public byte byteValue() {
return (byte)value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
}