mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-04-02 17:50:51 +02:00
java: MessagePackObject implements Cloneable and MessagePackable interfaces
This commit is contained in:
parent
cba47b635a
commit
6c91b862c9
@ -22,7 +22,7 @@ import java.util.Set;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
|
||||||
public abstract class MessagePackObject {
|
public abstract class MessagePackObject implements Cloneable, MessagePackable {
|
||||||
public boolean isNull() {
|
public boolean isNull() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -126,5 +126,7 @@ public abstract class MessagePackObject {
|
|||||||
public double doubleValue() {
|
public double doubleValue() {
|
||||||
throw new MessageTypeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract public Object clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ import java.io.IOException;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Packer enables you to serialize objects into OutputStream.
|
* Packer enables you to serialize objects into OutputStream.
|
||||||
@ -194,6 +195,27 @@ public class Packer {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Packer packBigInteger(BigInteger d) throws IOException {
|
||||||
|
if(d.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) {
|
||||||
|
return packLong(d.longValue());
|
||||||
|
} else if(d.bitLength() <= 64) {
|
||||||
|
castBytes[0] = (byte)0xcf;
|
||||||
|
byte[] barray = d.toByteArray();
|
||||||
|
castBytes[1] = barray[0];
|
||||||
|
castBytes[2] = barray[1];
|
||||||
|
castBytes[3] = barray[2];
|
||||||
|
castBytes[4] = barray[3];
|
||||||
|
castBytes[5] = barray[4];
|
||||||
|
castBytes[6] = barray[5];
|
||||||
|
castBytes[7] = barray[6];
|
||||||
|
castBytes[8] = barray[7];
|
||||||
|
out.write(castBytes);
|
||||||
|
return this;
|
||||||
|
} else {
|
||||||
|
throw new MessageTypeException("can't BigInteger larger than 0xffffffffffffffff");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Packer packFloat(float d) throws IOException {
|
public Packer packFloat(float d) throws IOException {
|
||||||
castBytes[0] = (byte)0xca;
|
castBytes[0] = (byte)0xca;
|
||||||
castBuffer.putFloat(1, d);
|
castBuffer.putFloat(1, d);
|
||||||
|
@ -18,9 +18,8 @@
|
|||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class ArrayType extends MessagePackObject {
|
public class ArrayType extends MessagePackObject {
|
||||||
@ -44,5 +43,30 @@ public class ArrayType extends MessagePackObject {
|
|||||||
public List<MessagePackObject> asList() {
|
public List<MessagePackObject> asList() {
|
||||||
return Arrays.asList(array);
|
return Arrays.asList(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packArray(array.length);
|
||||||
|
for(int i=0; i < array.length; i++) {
|
||||||
|
array[i].messagePack(pk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Arrays.equals(((ArrayType)obj).array, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
MessagePackObject[] copy = new MessagePackObject[array.length];
|
||||||
|
for(int i=0; i < array.length; i++) {
|
||||||
|
copy[i] = (MessagePackObject)array[i].clone();
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
class BigIntegerTypeIMPL extends IntegerType {
|
class BigIntegerTypeIMPL extends IntegerType {
|
||||||
@ -88,5 +89,23 @@ class BigIntegerTypeIMPL extends IntegerType {
|
|||||||
public double doubleValue() {
|
public double doubleValue() {
|
||||||
return value.doubleValue();
|
return value.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packBigInteger(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ((BigIntegerTypeIMPL)obj).value.equals(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
return new BigIntegerTypeIMPL(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
//
|
//
|
||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class BooleanType extends MessagePackObject {
|
public class BooleanType extends MessagePackObject {
|
||||||
@ -35,5 +36,23 @@ public class BooleanType extends MessagePackObject {
|
|||||||
public boolean asBoolean() {
|
public boolean asBoolean() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packBoolean(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ((BooleanType)obj).value == value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
return new BooleanType(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
class DoubleTypeIMPL extends FloatType {
|
class DoubleTypeIMPL extends FloatType {
|
||||||
@ -67,5 +68,23 @@ class DoubleTypeIMPL extends FloatType {
|
|||||||
public double doubleValue() {
|
public double doubleValue() {
|
||||||
return (double)value;
|
return (double)value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packDouble(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ((DoubleTypeIMPL)obj).value == value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
return new DoubleTypeIMPL(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
class FloatTypeIMPL extends FloatType {
|
class FloatTypeIMPL extends FloatType {
|
||||||
@ -66,5 +67,23 @@ class FloatTypeIMPL extends FloatType {
|
|||||||
public double doubleValue() {
|
public double doubleValue() {
|
||||||
return (double)value;
|
return (double)value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packFloat(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ((FloatTypeIMPL)obj).value == value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
return new FloatTypeIMPL(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
class LongIntegerTypeIMPL extends IntegerType {
|
class LongIntegerTypeIMPL extends IntegerType {
|
||||||
@ -85,5 +86,23 @@ class LongIntegerTypeIMPL extends IntegerType {
|
|||||||
public double doubleValue() {
|
public double doubleValue() {
|
||||||
return (double)value;
|
return (double)value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packLong(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ((LongIntegerTypeIMPL)obj).value == value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
return new LongIntegerTypeIMPL(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,8 @@ package org.msgpack.object;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class MapType extends MessagePackObject {
|
public class MapType extends MessagePackObject {
|
||||||
@ -44,5 +46,30 @@ public class MapType extends MessagePackObject {
|
|||||||
}
|
}
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packMap(map.length / 2);
|
||||||
|
for(int i=0; i < map.length; i++) {
|
||||||
|
map[i].messagePack(pk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Arrays.equals(((MapType)obj).map, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
MessagePackObject[] copy = new MessagePackObject[map.length];
|
||||||
|
for(int i=0; i < map.length; i++) {
|
||||||
|
copy[i] = (MessagePackObject)map[i].clone();
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
//
|
//
|
||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
public class NilType extends MessagePackObject {
|
public class NilType extends MessagePackObject {
|
||||||
@ -24,5 +25,23 @@ public class NilType extends MessagePackObject {
|
|||||||
public boolean isNull() {
|
public boolean isNull() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packNil();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
return new NilType();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,9 +17,11 @@
|
|||||||
//
|
//
|
||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
class RawType extends MessagePackObject {
|
public class RawType extends MessagePackObject {
|
||||||
private byte[] bytes;
|
private byte[] bytes;
|
||||||
|
|
||||||
public RawType(byte[] bytes) {
|
public RawType(byte[] bytes) {
|
||||||
@ -44,5 +46,24 @@ class RawType extends MessagePackObject {
|
|||||||
throw new MessageTypeException("type error");
|
throw new MessageTypeException("type error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packRaw(bytes.length);
|
||||||
|
pk.packRawBody(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ((RawType)obj).bytes.equals(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
return new RawType((byte[])bytes.clone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package org.msgpack.object;
|
package org.msgpack.object;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.io.IOException;
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
|
|
||||||
class ShortIntegerTypeIMPL extends IntegerType {
|
class ShortIntegerTypeIMPL extends IntegerType {
|
||||||
@ -87,5 +88,23 @@ class ShortIntegerTypeIMPL extends IntegerType {
|
|||||||
public double doubleValue() {
|
public double doubleValue() {
|
||||||
return (double)value;
|
return (double)value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void messagePack(Packer pk) throws IOException {
|
||||||
|
pk.packInt(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj.getClass() != getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ((ShortIntegerTypeIMPL)obj).value == value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
return new ShortIntegerTypeIMPL(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user