Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions msgpack-core/src/main/java/org/msgpack/value/IntegerValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,31 @@ public interface IntegerValue
*
* @throws MessageIntegerOverflowException If the value does not fit in the range of {@code byte} type.
*/
byte getByte();
byte asByte();

/**
* Returns the value as a {@code short}, otherwise throws an exception.
*
* @throws MessageIntegerOverflowException If the value does not fit in the range of {@code short} type.
*/
short getShort();
short asShort();

/**
* Returns the value as an {@code int}, otherwise throws an exception.
*
* @throws MessageIntegerOverflowException If the value does not fit in the range of {@code int} type.
*/
int getInt();
int asInt();

/**
* Returns the value as a {@code long}, otherwise throws an exception.
*
* @throws MessageIntegerOverflowException If the value does not fit in the range of {@code long} type.
*/
long getLong();
long asLong();

/**
* Returns the value as a {@code BigInteger}.
*/
BigInteger getBigInteger();
BigInteger asBigInteger();
}
16 changes: 8 additions & 8 deletions msgpack-core/src/main/java/org/msgpack/value/NumberValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.math.BigInteger;

/**
* The base interface {@code NumberValue} of {@code IntegerValue} and {@code FloatValue}.
* The base interface {@code NumberValue} of {@code IntegerValue} and {@code FloatValue}. To extract primitive type values, call toXXX methods, which may lose some information by rounding or truncation.
*
* @see org.msgpack.value.IntegerValue
* @see org.msgpack.value.FloatValue
Expand All @@ -30,36 +30,36 @@ public interface NumberValue
* Represent this value as a byte value, which may involve rounding or truncation of the original value.
* the value.
*/
byte castAsByte();
byte toByte();

/**
* Represent this value as a short value, which may involve rounding or truncation of the original value.
*/
short castAsShort();
short toShort();

/**
* Represent this value as an int value, which may involve rounding or truncation of the original value.
* value.
*/
int castAsInt();
int toInt();

/**
* Represent this value as a long value, which may involve rounding or truncation of the original value.
*/
long castAsLong();
long toLong();

/**
* Represent this value as a BigInteger, which may involve rounding or truncation of the original value.
*/
BigInteger castAsBigInteger();
BigInteger toBigInteger();

/**
* Represent this value as a 32-bit float value, which may involve rounding or truncation of the original value.
*/
float castAsFloat();
float toFloat();

/**
* Represent this value as a 64-bit double value, which may involve rounding or truncation of the original value.
*/
double castAsDouble();
double toDouble();
}
12 changes: 3 additions & 9 deletions msgpack-core/src/main/java/org/msgpack/value/RawValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public interface RawValue
* <p/>
* This method copies the byte array.
*/
byte[] getByteArray();
byte[] asByteArray();

/**
* Returns the value as {@code ByteBuffer}.
* <p/>
* Returned ByteBuffer is read-only. See {@code#asReadOnlyBuffer()}.
* This method doesn't copy the byte array as much as possible.
*/
ByteBuffer getByteBuffer();
ByteBuffer asByteBuffer();

/**
* Returns the value as {@code String}.
Expand All @@ -50,12 +50,6 @@ public interface RawValue
*
* @throws MessageStringCodingException If this value includes invalid UTF-8 byte sequence.
*/
String getString();
String asString();

/**
* Returns the value as {@code String}.
* <p/>
* This method replaces an invalid UTF-8 byte sequence with <code>U+FFFD replacement character</code>.
*/
String stringValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* <p/>
* MessagePack's String type can represent a UTF-8 string at most 2<sup>64</sup>-1 bytes.
* <p/>
* Note that the value could include invalid byte sequences. {@code getString()} method throws {@code MessageTypeStringCodingException} if the value includes invalid byte sequence. {@code stringValue()} method replaces an invalid byte sequence with <code>U+FFFD replacement character</code>.
* Note that the value could include invalid byte sequences. {@code asString()} method throws {@code MessageTypeStringCodingException} if the value includes invalid byte sequence. {@code toJson()} method replaces an invalid byte sequence with <code>U+FFFD replacement character</code>.
*
* @see org.msgpack.value.RawValue
*/
Expand Down
14 changes: 13 additions & 1 deletion msgpack-core/src/main/java/org/msgpack/value/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import java.io.IOException;

/**
* Value is an implementation of MessagePack type system.
* Value is an implementation of MessagePack type system. To retrieve values from a Value object,
* You need to check its {@link ValueType} then call an appropriate asXXXValue method.
*
*
*
*/
public interface Value
{
Expand Down Expand Up @@ -242,4 +246,12 @@ void writeTo(MessagePacker pk)
* If this value is {@code MapValue} or {@code ArrayValue}, this method check equivalence of elements recursively.
*/
boolean equals(Object obj);

/**
* Returns json representation of this Value for debugging purpose.
* This output json format is subject to change in future.
* Do not write code that depends on the resulting json format.
*/
String toJson();

}
12 changes: 8 additions & 4 deletions msgpack-core/src/main/java/org/msgpack/value/ValueFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,28 +191,32 @@ public MapValue build()
return newMap(map);
}

public void put(Map.Entry<? extends Value, ? extends Value> pair)
public MapBuilder put(Map.Entry<? extends Value, ? extends Value> pair)
{
put(pair.getKey(), pair.getValue());
return this;
}

public void put(Value key, Value value)
public MapBuilder put(Value key, Value value)
{
map.put(key, value);
return this;
}

public void putAll(Iterable<? extends Map.Entry<? extends Value, ? extends Value>> entries)
public MapBuilder putAll(Iterable<? extends Map.Entry<? extends Value, ? extends Value>> entries)
{
for (Map.Entry<? extends Value, ? extends Value> entry : entries) {
put(entry.getKey(), entry.getValue());
}
return this;
}

public void putAll(Map<? extends Value, ? extends Value> map)
public MapBuilder putAll(Map<? extends Value, ? extends Value> map)
{
for (Map.Entry<? extends Value, ? extends Value> entry : map.entrySet()) {
put(entry);
}
return this;
}
}

Expand Down
56 changes: 34 additions & 22 deletions msgpack-core/src/main/java/org/msgpack/value/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,16 @@ public int hashCode()
return Variable.this.hashCode();
}

@Override
public String toJson()
{
return Variable.this.toJson();
}

@Override
public String toString()
{
return Variable.this.toString();
return toJson();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean Variable.this.toString();?

}
}

Expand Down Expand Up @@ -357,7 +363,7 @@ public NumberValue asNumberValue()
}

@Override
public byte castAsByte()
public byte toByte()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).byteValue();
Expand All @@ -366,7 +372,7 @@ public byte castAsByte()
}

@Override
public short castAsShort()
public short toShort()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).shortValue();
Expand All @@ -375,7 +381,7 @@ public short castAsShort()
}

@Override
public int castAsInt()
public int toInt()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).intValue();
Expand All @@ -384,7 +390,7 @@ public int castAsInt()
}

@Override
public long castAsLong()
public long toLong()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).longValue();
Expand All @@ -393,7 +399,7 @@ public long castAsLong()
}

@Override
public BigInteger castAsBigInteger()
public BigInteger toBigInteger()
{
if (type == Type.BIG_INTEGER) {
return (BigInteger) objectValue;
Expand All @@ -405,7 +411,7 @@ else if (type == Type.DOUBLE) {
}

@Override
public float castAsFloat()
public float toFloat()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).floatValue();
Expand All @@ -417,7 +423,7 @@ else if (type == Type.DOUBLE) {
}

@Override
public double castAsDouble()
public double toDouble()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).doubleValue();
Expand Down Expand Up @@ -524,7 +530,7 @@ public MessageFormat mostSuccinctMessageFormat()
}

@Override
public byte getByte()
public byte asByte()
{
if (!isInByteRange()) {
throw new MessageIntegerOverflowException(longValue);
Expand All @@ -533,7 +539,7 @@ public byte getByte()
}

@Override
public short getShort()
public short asShort()
{
if (!isInByteRange()) {
throw new MessageIntegerOverflowException(longValue);
Expand All @@ -542,7 +548,7 @@ public short getShort()
}

@Override
public int getInt()
public int asInt()
{
if (!isInIntRange()) {
throw new MessageIntegerOverflowException(longValue);
Expand All @@ -551,7 +557,7 @@ public int getInt()
}

@Override
public long getLong()
public long asLong()
{
if (!isInLongRange()) {
throw new MessageIntegerOverflowException(longValue);
Expand All @@ -560,7 +566,7 @@ public long getLong()
}

@Override
public BigInteger getBigInteger()
public BigInteger asBigInteger()
{
if (type == Type.BIG_INTEGER) {
return (BigInteger) objectValue;
Expand Down Expand Up @@ -592,15 +598,15 @@ public Variable setFloatValue(double v)
this.type = Type.DOUBLE;
this.accessor = floatAccessor;
this.doubleValue = v;
this.longValue = (long) v; // AbstractNumberValueAccessor uses castAsLong
this.longValue = (long) v; // AbstractNumberValueAccessor uses toLong
return this;
}

public Variable setFloatValue(float v)
{
this.type = Type.DOUBLE;
this.accessor = floatAccessor;
this.longValue = (long) v; // AbstractNumberValueAccessor uses castAsLong
this.longValue = (long) v; // AbstractNumberValueAccessor uses toLong
return this;
}

Expand Down Expand Up @@ -651,19 +657,19 @@ public RawValue asRawValue()
}

@Override
public byte[] getByteArray()
public byte[] asByteArray()
{
return (byte[]) objectValue;
}

@Override
public ByteBuffer getByteBuffer()
public ByteBuffer asByteBuffer()
{
return ByteBuffer.wrap(getByteArray());
return ByteBuffer.wrap(asByteArray());
}

@Override
public String getString()
public String asString()
{
byte[] raw = (byte[]) objectValue;
try {
Expand All @@ -678,7 +684,7 @@ public String getString()
}

@Override
public String stringValue()
public String toJson()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this toString()?

{
byte[] raw = (byte[]) objectValue;
try {
Expand Down Expand Up @@ -724,7 +730,7 @@ public BinaryValue asBinaryValue()
@Override
public ImmutableBinaryValue immutableValue()
{
return ValueFactory.newBinary(getByteArray());
return ValueFactory.newBinary(asByteArray());
}

@Override
Expand Down Expand Up @@ -1044,11 +1050,17 @@ public boolean equals(Object o)
}

@Override
public String toString()
public String toJson()
{
return immutableValue().toString(); // TODO optimize
}

@Override
public String toString()
{
return toJson();
}

@Override
public ValueType getValueType()
{
Expand Down
Loading