diff --git a/java/src/main/java/org/msgpack/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java index f004e6c4..cfd3d22b 100644 --- a/java/src/main/java/org/msgpack/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -124,7 +124,7 @@ public class UnpackerImpl { if((b & 0xe0) == 0xa0) { // FixRaw trail = b & 0x1f; if(trail == 0) { - obj = new RawType(new byte[0]); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; @@ -139,7 +139,7 @@ public class UnpackerImpl { //System.out.println("fixarray count:"+count); obj = new MessagePackObject[count]; if(count == 0) { - obj = new ArrayType((MessagePackObject[])obj); + obj = ArrayType.create((MessagePackObject[])obj); break _push; } ++top; @@ -159,7 +159,7 @@ public class UnpackerImpl { count = b & 0x0f; obj = new MessagePackObject[count*2]; if(count == 0) { - obj = new MapType((MessagePackObject[])obj); + obj = MapType.create((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -175,13 +175,13 @@ public class UnpackerImpl { switch(b & 0xff) { // FIXME case 0xc0: // nil - obj = new NilType(); + obj = NilType.create(); break _push; case 0xc2: // false - obj = new BooleanType(false); + obj = BooleanType.create(false); break _push; case 0xc3: // true - obj = new BooleanType(true); + obj = BooleanType.create(true); break _push; case 0xca: // float case 0xcb: // double @@ -293,7 +293,7 @@ public class UnpackerImpl { castBuffer.put(src, n, 2); trail = ((int)castBuffer.getShort(0)) & 0xffff; if(trail == 0) { - obj = new RawType(new byte[0]); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; @@ -304,14 +304,14 @@ public class UnpackerImpl { // FIXME overflow check trail = castBuffer.getInt(0) & 0x7fffffff; if(trail == 0) { - obj = new RawType(new byte[0]); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; case ACS_RAW_VALUE: { byte[] raw = new byte[trail]; System.arraycopy(src, n, raw, 0, trail); - obj = new RawType(raw); + obj = RawType.create(raw); } break _push; case CS_ARRAY_16: @@ -323,7 +323,7 @@ public class UnpackerImpl { count = ((int)castBuffer.getShort(0)) & 0xffff; obj = new MessagePackObject[count]; if(count == 0) { - obj = new ArrayType((MessagePackObject[])obj); + obj = ArrayType.create((MessagePackObject[])obj); break _push; } ++top; @@ -344,7 +344,7 @@ public class UnpackerImpl { count = castBuffer.getInt(0) & 0x7fffffff; obj = new MessagePackObject[count]; if(count == 0) { - obj = new ArrayType((MessagePackObject[])obj); + obj = ArrayType.create((MessagePackObject[])obj); break _push; } ++top; @@ -364,7 +364,7 @@ public class UnpackerImpl { count = ((int)castBuffer.getShort(0)) & 0xffff; obj = new MessagePackObject[count*2]; if(count == 0) { - obj = new MapType((MessagePackObject[])obj); + obj = MapType.create((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -386,7 +386,7 @@ public class UnpackerImpl { count = castBuffer.getInt(0) & 0x7fffffff; obj = new MessagePackObject[count*2]; if(count == 0) { - obj = new MapType((MessagePackObject[])obj); + obj = MapType.create((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -425,7 +425,7 @@ public class UnpackerImpl { top_obj = stack_obj[top]; top_ct = stack_ct[top]; top_count = stack_count[top]; - obj = new ArrayType((MessagePackObject[])ar); + obj = ArrayType.create((MessagePackObject[])ar); stack_obj[top] = null; --top; break _push; @@ -447,7 +447,7 @@ public class UnpackerImpl { top_obj = stack_obj[top]; top_ct = stack_ct[top]; top_count = stack_count[top]; - obj = new MapType((MessagePackObject[])mp); + obj = MapType.create((MessagePackObject[])mp); stack_obj[top] = null; --top; break _push; diff --git a/java/src/main/java/org/msgpack/object/ArrayType.java b/java/src/main/java/org/msgpack/object/ArrayType.java index 350ce32e..36134dc7 100644 --- a/java/src/main/java/org/msgpack/object/ArrayType.java +++ b/java/src/main/java/org/msgpack/object/ArrayType.java @@ -25,10 +25,14 @@ import org.msgpack.*; public class ArrayType extends MessagePackObject { private MessagePackObject[] array; - public ArrayType(MessagePackObject[] array) { + ArrayType(MessagePackObject[] array) { this.array = array; } + public static ArrayType create(MessagePackObject[] array) { + return new ArrayType(array); + } + @Override public boolean isArrayType() { return true; diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java index 7b060eef..b29879f4 100644 --- a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java @@ -109,7 +109,7 @@ class BigIntegerTypeIMPL extends IntegerType { public boolean equals(Object obj) { if(obj.getClass() != getClass()) { if(obj.getClass() == ShortIntegerTypeIMPL.class) { - return BigInteger.valueOf((long)((ShortIntegerTypeIMPL)obj).shortValue()).equals(value); + return BigInteger.valueOf(((ShortIntegerTypeIMPL)obj).longValue()).equals(value); } else if(obj.getClass() == LongIntegerTypeIMPL.class) { return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value); } diff --git a/java/src/main/java/org/msgpack/object/BooleanType.java b/java/src/main/java/org/msgpack/object/BooleanType.java index 1d12c1ce..65bd42b4 100644 --- a/java/src/main/java/org/msgpack/object/BooleanType.java +++ b/java/src/main/java/org/msgpack/object/BooleanType.java @@ -23,10 +23,14 @@ import org.msgpack.*; public class BooleanType extends MessagePackObject { private boolean value; - public BooleanType(boolean value) { + BooleanType(boolean value) { this.value = value; } + public static BooleanType create(boolean value) { + return new BooleanType(value); + } + @Override public boolean isBooleanType() { return true; diff --git a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java index 3928a299..d052e77a 100644 --- a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java @@ -108,7 +108,7 @@ class LongIntegerTypeIMPL extends IntegerType { if(obj.getClass() == ShortIntegerTypeIMPL.class) { return value == ((ShortIntegerTypeIMPL)obj).longValue(); } else if(obj.getClass() == BigIntegerTypeIMPL.class) { - return (long)value == ((BigIntegerTypeIMPL)obj).longValue(); + return BigInteger.valueOf(value).equals(((BigIntegerTypeIMPL)obj).bigIntegerValue()); } return false; } diff --git a/java/src/main/java/org/msgpack/object/MapType.java b/java/src/main/java/org/msgpack/object/MapType.java index 619d3887..148c0bf4 100644 --- a/java/src/main/java/org/msgpack/object/MapType.java +++ b/java/src/main/java/org/msgpack/object/MapType.java @@ -26,10 +26,14 @@ import org.msgpack.*; public class MapType extends MessagePackObject { private MessagePackObject[] map; - public MapType(MessagePackObject[] map) { + MapType(MessagePackObject[] map) { this.map = map; } + public static MapType create(MessagePackObject[] map) { + return new MapType(map); + } + @Override public boolean isMapType() { return true; diff --git a/java/src/main/java/org/msgpack/object/NilType.java b/java/src/main/java/org/msgpack/object/NilType.java index ece62f0a..d0572f19 100644 --- a/java/src/main/java/org/msgpack/object/NilType.java +++ b/java/src/main/java/org/msgpack/object/NilType.java @@ -21,6 +21,12 @@ import java.io.IOException; import org.msgpack.*; public class NilType extends MessagePackObject { + private static NilType instance = new NilType(); + + public static NilType create() { + return instance; + } + @Override public boolean isNull() { return true; diff --git a/java/src/main/java/org/msgpack/object/RawType.java b/java/src/main/java/org/msgpack/object/RawType.java index 3a394862..26f6e0d9 100644 --- a/java/src/main/java/org/msgpack/object/RawType.java +++ b/java/src/main/java/org/msgpack/object/RawType.java @@ -24,10 +24,26 @@ import org.msgpack.*; public class RawType extends MessagePackObject { private byte[] bytes; - public RawType(byte[] bytes) { + RawType(byte[] bytes) { this.bytes = bytes; } + RawType(String str) { + try { + this.bytes = str.getBytes("UTF-8"); + } catch (Exception e) { + throw new MessageTypeException("type error"); + } + } + + public static RawType create(byte[] bytes) { + return new RawType(bytes); + } + + public static RawType create(String str) { + return new RawType(str); + } + @Override public boolean isRawType() { return true; @@ -58,7 +74,7 @@ public class RawType extends MessagePackObject { if(obj.getClass() != getClass()) { return false; } - return ((RawType)obj).bytes.equals(bytes); + return Arrays.equals(((RawType)obj).bytes, bytes); } @Override diff --git a/java/src/test/java/org/msgpack/TestObjectEquals.java b/java/src/test/java/org/msgpack/TestObjectEquals.java new file mode 100644 index 00000000..b2b018d1 --- /dev/null +++ b/java/src/test/java/org/msgpack/TestObjectEquals.java @@ -0,0 +1,96 @@ +package org.msgpack; + +import org.msgpack.*; +import org.msgpack.object.*; +import java.math.BigInteger; +import java.util.*; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestObjectEquals { + public void testInt(int val) throws Exception { + MessagePackObject objInt = IntegerType.create(val); + MessagePackObject objLong = IntegerType.create((long)val); + MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf((long)val)); + assertTrue(objInt.equals(objInt)); + assertTrue(objInt.equals(objLong)); + assertTrue(objInt.equals(objBigInt)); + assertTrue(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertTrue(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } + + public void testLong(long val) throws Exception { + MessagePackObject objInt = IntegerType.create((int)val); + MessagePackObject objLong = IntegerType.create(val); + MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf(val)); + if(val > (long)Integer.MAX_VALUE || val < (long)Integer.MIN_VALUE) { + assertTrue(objInt.equals(objInt)); + assertFalse(objInt.equals(objLong)); + assertFalse(objInt.equals(objBigInt)); + assertFalse(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertFalse(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } else { + assertTrue(objInt.equals(objInt)); + assertTrue(objInt.equals(objLong)); + assertTrue(objInt.equals(objBigInt)); + assertTrue(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertTrue(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } + } + @Test + public void testLong() throws Exception { + testLong(0); + testLong(-1); + testLong(1); + testLong(Integer.MIN_VALUE); + testLong(Integer.MAX_VALUE); + testLong(Long.MIN_VALUE); + testLong(Long.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testLong(rand.nextLong()); + } + + @Test + public void testNil() throws Exception { + assertTrue(NilType.create().equals(NilType.create())); + assertFalse(NilType.create().equals(IntegerType.create(0))); + assertFalse(NilType.create().equals(BooleanType.create(false))); + } + + public void testString(String str) throws Exception { + assertTrue(RawType.create(str).equals(RawType.create(str))); + } + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); + } +} +