Skip to content

msgpack-value v07 API #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.msgpack.core;

/**
* Created on 5/28/14.
* Thrown when converting a message into a type that causes truncation or rounding.
* For example, {@link org.msgpack.value.NumberValue#asInt()} throws this error if
* it is a long value more than {@link java.lang.Integer#MAX_VALUE}.
*/
public class MessageOverflowException extends MessageTypeException {

Expand Down
10 changes: 5 additions & 5 deletions msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ public long unpackLong() throws IOException {
case Code.UINT32: // unsigned int 32
int u32 = readInt();
if(u32 < 0) {
return (long) (u32 & 0x7fffffff) + 0x80000000L;
return u32 & 0xffffffffL;
} else {
return (long) u32;
}
Expand Down Expand Up @@ -808,7 +808,7 @@ public BigInteger unpackBigInteger() throws IOException {
case Code.UINT32: // unsigned int 32
int u32 = readInt();
if(u32 < 0) {
return BigInteger.valueOf((long) (u32 & 0x7fffffff) + 0x80000000L);
return BigInteger.valueOf(u32 & 0xffffffffL);
} else {
return BigInteger.valueOf((long) u32);
}
Expand Down Expand Up @@ -884,7 +884,7 @@ public void unpackInteger(IntegerHolder holder) throws IOException {
case Code.UINT32: // unsigned int 32
int u32 = readInt();
if(u32 < 0) {
holder.setLong((long) (u32 & 0x7fffffff) + 0x80000000L);
holder.setLong(u32 & 0xffffffffL);
} else {
holder.setInt(u32);
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ private static MessageIntegerOverflowException overflowU16(short u16) {
}

private static MessageIntegerOverflowException overflowU32(int u32) {
BigInteger bi = BigInteger.valueOf((long) (u32 & 0x7fffffff) + 0x80000000L);
BigInteger bi = BigInteger.valueOf(u32 & 0xffffffffL);
return new MessageIntegerOverflowException(bi);
}

Expand All @@ -1254,7 +1254,7 @@ private static MessageIntegerOverflowException overflowI64(long i64) {
}

private static MessageSizeException overflowU32Size(int u32) {
long lv = (long) (u32 & 0x7fffffff) + 0x80000000L;
long lv = u32 & 0xffffffffL;
return new MessageSizeException(lv);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ public static void readAndWriteFile() throws IOException {
switch(format.getValueType()) {
case NIL:
Value nil = v.get();
nil.isNil(); // true
nil.isNilValue(); // true
System.out.println("read nil");
break;
case BOOLEAN:
boolean b = v.get().asBoolean().toBoolean();
boolean b = v.get().asBooleanValue().toBoolean();
System.out.println("read boolean: " + b);
break;
case INTEGER:
Expand All @@ -206,22 +206,22 @@ public static void readAndWriteFile() throws IOException {
System.out.println("read float: " + d);
break;
case STRING:
String s = v.get().asString().toString();
String s = v.get().asStringValue().toString();
System.out.println("read string: " + s);
break;
case BINARY:
// Message buffer is an efficient byte buffer
MessageBuffer mb = v.get().asBinary().toMessageBuffer();
MessageBuffer mb = v.get().asBinaryValue().toMessageBuffer();
System.out.println("read binary: " + mb.toHexString(0, mb.size()));
break;
case ARRAY:
ArrayValue arr = v.get().asArrayValue();
for(ValueRef a : arr) {
for(Value a : arr) {
System.out.println("read array element: " + a);
}
break;
case EXTENDED:
ExtendedValue ev = v.get().asExtended();
ExtendedValue ev = v.get().asExtendedValue();
int extType = ev.getExtType();
byte[] extValue = ev.toByteArray();
break;
Expand Down
8 changes: 4 additions & 4 deletions msgpack-core/src/main/java/org/msgpack/value/ArrayCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
/**
* Created on 6/16/14.
*/
public interface ArrayCursor extends ValueRef, Iterable<ValueRef> {
public interface ArrayCursor extends Value, Iterable<Value> {
public int size();

public boolean hasNext();
public ValueRef next();
public Value next();
public void skip();

/**
* Skips all of the remaining values
*/
public void skipAll();

public Iterator<ValueRef> iterator();
public Iterator<Value> iterator();

public ArrayValue toValue();
public ArrayValue toImmutable();

}
6 changes: 3 additions & 3 deletions msgpack-core/src/main/java/org/msgpack/value/ArrayValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* Implementation note: We do not implement List<Value> interface here, because
* we cannot reuse AbstractList and AbstractValue implementations simultaneously since
* Java does not support mixin of classes. Instead, it provides {@link #iterator} or
* {@link #toValueArray()} methods to traverse the array contents.
* {@link #toArray()} methods to traverse the array contents.
*/
public interface ArrayValue extends Value, ArrayCursor {

public Value[] toValueArray();
public Value[] toArray();

public Value get(int index);
public Value apply(int index);

public ArrayValue toValue();
public ArrayValue toImmutable();

}
13 changes: 4 additions & 9 deletions msgpack-core/src/main/java/org/msgpack/value/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.util.Iterator;

/**
* Cursor for traversing a stream of message-packed values
* Cursor for traversing a stream of message-packed values.
* Returned value might be changed for efficiency. To retrieving an immutable value,
* call {@link org.msgpack.value.Value#toImmutable()} after retrieving a value.
*/
public interface Cursor extends Iterator<Value>, Closeable {

Expand All @@ -16,14 +18,7 @@ public interface Cursor extends Iterator<Value>, Closeable {
public boolean hasNext();

/**
* Returns a reference to the value, then proceeds the cursor.
* The returned reference is valid until {@link #hasNext()} is called.
* @return
*/
public ValueRef nextRef();

/**
* Returns the materialized value of the referenced value, then proceeds the cursor.
* Returns the next value, then proceeds the cursor.
* @return
*/
public Value next();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.msgpack.value;

/**
* Created on 5/30/14.
* ExtendedValue interface
*/
public interface ExtendedValue extends RawValue {
public int getExtType();
public ExtendedValue toValue();
public ExtendedValue toImmutable();
}
4 changes: 2 additions & 2 deletions msgpack-core/src/main/java/org/msgpack/value/FloatValue.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.msgpack.value;

/**
* Created on 5/30/14.
* FloatValue interface
*/
public interface FloatValue extends NumberValue {
FloatValue toValue();
FloatValue toImmutable();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.msgpack.value;

/**
* Created on 5/30/14.
* IntegerValue interface
*/
public interface IntegerValue extends NumberValue {
IntegerValue toValue();
IntegerValue toImmutable();
}
14 changes: 14 additions & 0 deletions msgpack-core/src/main/java/org/msgpack/value/KeyValuePair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.msgpack.value;

/**
*
*/
public class KeyValuePair {
public final Value key;
public final Value value;

public KeyValuePair(Value key, Value value) {
this.key = key;
this.value = value;
}
}
14 changes: 6 additions & 8 deletions msgpack-core/src/main/java/org/msgpack/value/MapCursor.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.msgpack.value;

import java.util.Iterator;
import java.util.Map;

/**
* Cursor for traversing map value entries. This cursor reports a sequence of key and value pairs.
*/
public interface MapCursor extends ValueRef {
public interface MapCursor extends Value {
public int size();

/**
Expand All @@ -16,20 +14,20 @@ public interface MapCursor extends ValueRef {
public boolean hasNext();

/**
* Retrieves a reference to the next key or value.
* Retrieves the next key and value.
* @return
*/
public ValueRef nextKeyOrValue();
public KeyValuePair next();

/**
* Skips a next key or value
* Skips a next key-value pair
*/
public void skipKeyOrValue();
public void skip();

/**
* Skips all of the remaining keys and values.
*/
public void skipAll();

MapValue toValue();
public MapValue toImmutable();
}
7 changes: 4 additions & 3 deletions msgpack-core/src/main/java/org/msgpack/value/MapValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.util.Map;

/**
* Created on 5/30/14.
* MapValue interface
*/
public interface MapValue extends Value, MapCursor {
public Value[] toKeyValueSeq();
public Value[] toKeyValueArray();
public KeyValuePair[] toArray();
public Map<Value, Value> toMap();

public MapValue toValue();
public MapValue toImmutable();
}
2 changes: 1 addition & 1 deletion msgpack-core/src/main/java/org/msgpack/value/NilValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* References to values
*/
public interface NilValue extends Value {
NilValue toValue();
NilValue toImmutable();
}
6 changes: 3 additions & 3 deletions msgpack-core/src/main/java/org/msgpack/value/NumberValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.math.BigInteger;

/**
* Created on 5/30/14.
* NumberValue interface
*/
public interface NumberValue extends Value {

Expand Down Expand Up @@ -60,11 +60,11 @@ public interface NumberValue extends Value {
*/
public BigInteger toBigInteger();
/**
* Convert this value into a float value
* Convert this value into a 32-bit float
*/
public float toFloat();
/**
* Convert this value into a double value
* Convert this value into a 64-bit double
*/
public double toDouble();

Expand Down
18 changes: 16 additions & 2 deletions msgpack-core/src/main/java/org/msgpack/value/RawValue.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.msgpack.value;

import org.msgpack.core.MessageStringCodingException;
import org.msgpack.core.buffer.MessageBuffer;

import java.nio.ByteBuffer;
Expand All @@ -9,12 +8,27 @@
* Base type of StringValue, BinaryValue and ExtendedValue
*/
public interface RawValue extends Value {

/**
* Returns byte array representation of this value
* @return
*/
public byte[] toByteArray();

/**
* Returns ByteBuffer representation of this value
* @return
*/
public ByteBuffer toByteBuffer();

/**
* Returns MessageBuffer representation of this value
* @return
*/
public MessageBuffer toMessageBuffer();

@Override
public String toString();

public RawValue toValue();
public RawValue toImmutable();
}
4 changes: 2 additions & 2 deletions msgpack-core/src/main/java/org/msgpack/value/StringValue.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.msgpack.value;

/**
* Created on 5/30/14.
* StringValue interface
*/
public interface StringValue extends RawValue {
public StringValue toValue();
public StringValue toImmutable();
}
Loading