Skip to content

Commit b18aecf

Browse files
committed
#259: Rename data access methods
1 parent 46f74cc commit b18aecf

22 files changed

+260
-142
lines changed

msgpack-core/src/main/java/org/msgpack/value/IntegerValue.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,31 @@ public interface IntegerValue
5858
*
5959
* @throws MessageIntegerOverflowException If the value does not fit in the range of {@code byte} type.
6060
*/
61-
byte getByte();
61+
byte asByte();
6262

6363
/**
6464
* Returns the value as a {@code short}, otherwise throws an exception.
6565
*
6666
* @throws MessageIntegerOverflowException If the value does not fit in the range of {@code short} type.
6767
*/
68-
short getShort();
68+
short asShort();
6969

7070
/**
7171
* Returns the value as an {@code int}, otherwise throws an exception.
7272
*
7373
* @throws MessageIntegerOverflowException If the value does not fit in the range of {@code int} type.
7474
*/
75-
int getInt();
75+
int asInt();
7676

7777
/**
7878
* Returns the value as a {@code long}, otherwise throws an exception.
7979
*
8080
* @throws MessageIntegerOverflowException If the value does not fit in the range of {@code long} type.
8181
*/
82-
long getLong();
82+
long asLong();
8383

8484
/**
8585
* Returns the value as a {@code BigInteger}.
8686
*/
87-
BigInteger getBigInteger();
87+
BigInteger asBigInteger();
8888
}

msgpack-core/src/main/java/org/msgpack/value/NumberValue.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.math.BigInteger;
1919

2020
/**
21-
* The base interface {@code NumberValue} of {@code IntegerValue} and {@code FloatValue}.
21+
* 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.
2222
*
2323
* @see org.msgpack.value.IntegerValue
2424
* @see org.msgpack.value.FloatValue
@@ -30,36 +30,36 @@ public interface NumberValue
3030
* Represent this value as a byte value, which may involve rounding or truncation of the original value.
3131
* the value.
3232
*/
33-
byte castAsByte();
33+
byte toByte();
3434

3535
/**
3636
* Represent this value as a short value, which may involve rounding or truncation of the original value.
3737
*/
38-
short castAsShort();
38+
short toShort();
3939

4040
/**
4141
* Represent this value as an int value, which may involve rounding or truncation of the original value.
4242
* value.
4343
*/
44-
int castAsInt();
44+
int toInt();
4545

4646
/**
4747
* Represent this value as a long value, which may involve rounding or truncation of the original value.
4848
*/
49-
long castAsLong();
49+
long toLong();
5050

5151
/**
5252
* Represent this value as a BigInteger, which may involve rounding or truncation of the original value.
5353
*/
54-
BigInteger castAsBigInteger();
54+
BigInteger toBigInteger();
5555

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

6161
/**
6262
* Represent this value as a 64-bit double value, which may involve rounding or truncation of the original value.
6363
*/
64-
double castAsDouble();
64+
double toDouble();
6565
}

msgpack-core/src/main/java/org/msgpack/value/RawValue.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public interface RawValue
3333
* <p/>
3434
* This method copies the byte array.
3535
*/
36-
byte[] getByteArray();
36+
byte[] asByteArray();
3737

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

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

55-
/**
56-
* Returns the value as {@code String}.
57-
* <p/>
58-
* This method replaces an invalid UTF-8 byte sequence with <code>U+FFFD replacement character</code>.
59-
*/
60-
String stringValue();
6155
}

msgpack-core/src/main/java/org/msgpack/value/StringValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* <p/>
2121
* MessagePack's String type can represent a UTF-8 string at most 2<sup>64</sup>-1 bytes.
2222
* <p/>
23-
* 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>.
23+
* 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>.
2424
*
2525
* @see org.msgpack.value.RawValue
2626
*/

msgpack-core/src/main/java/org/msgpack/value/Value.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import java.io.IOException;
2121

2222
/**
23-
* Value is an implementation of MessagePack type system.
23+
* Value is an implementation of MessagePack type system. To retrieve values from a Value object,
24+
* You need to check its {@link ValueType} then call an appropriate asXXXValue method.
25+
*
26+
*
27+
*
2428
*/
2529
public interface Value
2630
{
@@ -242,4 +246,12 @@ void writeTo(MessagePacker pk)
242246
* If this value is {@code MapValue} or {@code ArrayValue}, this method check equivalence of elements recursively.
243247
*/
244248
boolean equals(Object obj);
249+
250+
/**
251+
* Returns json representation of this Value for debugging purpose.
252+
* This output json format is subject to change in future.
253+
* Do not write code that depends on the resulting json format.
254+
*/
255+
String toJson();
256+
245257
}

msgpack-core/src/main/java/org/msgpack/value/ValueFactory.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,28 +191,32 @@ public MapValue build()
191191
return newMap(map);
192192
}
193193

194-
public void put(Map.Entry<? extends Value, ? extends Value> pair)
194+
public MapBuilder put(Map.Entry<? extends Value, ? extends Value> pair)
195195
{
196196
put(pair.getKey(), pair.getValue());
197+
return this;
197198
}
198199

199-
public void put(Value key, Value value)
200+
public MapBuilder put(Value key, Value value)
200201
{
201202
map.put(key, value);
203+
return this;
202204
}
203205

204-
public void putAll(Iterable<? extends Map.Entry<? extends Value, ? extends Value>> entries)
206+
public MapBuilder putAll(Iterable<? extends Map.Entry<? extends Value, ? extends Value>> entries)
205207
{
206208
for (Map.Entry<? extends Value, ? extends Value> entry : entries) {
207209
put(entry.getKey(), entry.getValue());
208210
}
211+
return this;
209212
}
210213

211-
public void putAll(Map<? extends Value, ? extends Value> map)
214+
public MapBuilder putAll(Map<? extends Value, ? extends Value> map)
212215
{
213216
for (Map.Entry<? extends Value, ? extends Value> entry : map.entrySet()) {
214217
put(entry);
215218
}
219+
return this;
216220
}
217221
}
218222

msgpack-core/src/main/java/org/msgpack/value/Variable.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,16 @@ public int hashCode()
186186
return Variable.this.hashCode();
187187
}
188188

189+
@Override
190+
public String toJson()
191+
{
192+
return Variable.this.toJson();
193+
}
194+
189195
@Override
190196
public String toString()
191197
{
192-
return Variable.this.toString();
198+
return toJson();
193199
}
194200
}
195201

@@ -357,7 +363,7 @@ public NumberValue asNumberValue()
357363
}
358364

359365
@Override
360-
public byte castAsByte()
366+
public byte toByte()
361367
{
362368
if (type == Type.BIG_INTEGER) {
363369
return ((BigInteger) objectValue).byteValue();
@@ -366,7 +372,7 @@ public byte castAsByte()
366372
}
367373

368374
@Override
369-
public short castAsShort()
375+
public short toShort()
370376
{
371377
if (type == Type.BIG_INTEGER) {
372378
return ((BigInteger) objectValue).shortValue();
@@ -375,7 +381,7 @@ public short castAsShort()
375381
}
376382

377383
@Override
378-
public int castAsInt()
384+
public int toInt()
379385
{
380386
if (type == Type.BIG_INTEGER) {
381387
return ((BigInteger) objectValue).intValue();
@@ -384,7 +390,7 @@ public int castAsInt()
384390
}
385391

386392
@Override
387-
public long castAsLong()
393+
public long toLong()
388394
{
389395
if (type == Type.BIG_INTEGER) {
390396
return ((BigInteger) objectValue).longValue();
@@ -393,7 +399,7 @@ public long castAsLong()
393399
}
394400

395401
@Override
396-
public BigInteger castAsBigInteger()
402+
public BigInteger toBigInteger()
397403
{
398404
if (type == Type.BIG_INTEGER) {
399405
return (BigInteger) objectValue;
@@ -405,7 +411,7 @@ else if (type == Type.DOUBLE) {
405411
}
406412

407413
@Override
408-
public float castAsFloat()
414+
public float toFloat()
409415
{
410416
if (type == Type.BIG_INTEGER) {
411417
return ((BigInteger) objectValue).floatValue();
@@ -417,7 +423,7 @@ else if (type == Type.DOUBLE) {
417423
}
418424

419425
@Override
420-
public double castAsDouble()
426+
public double toDouble()
421427
{
422428
if (type == Type.BIG_INTEGER) {
423429
return ((BigInteger) objectValue).doubleValue();
@@ -524,7 +530,7 @@ public MessageFormat mostSuccinctMessageFormat()
524530
}
525531

526532
@Override
527-
public byte getByte()
533+
public byte asByte()
528534
{
529535
if (!isInByteRange()) {
530536
throw new MessageIntegerOverflowException(longValue);
@@ -533,7 +539,7 @@ public byte getByte()
533539
}
534540

535541
@Override
536-
public short getShort()
542+
public short asShort()
537543
{
538544
if (!isInByteRange()) {
539545
throw new MessageIntegerOverflowException(longValue);
@@ -542,7 +548,7 @@ public short getShort()
542548
}
543549

544550
@Override
545-
public int getInt()
551+
public int asInt()
546552
{
547553
if (!isInIntRange()) {
548554
throw new MessageIntegerOverflowException(longValue);
@@ -551,7 +557,7 @@ public int getInt()
551557
}
552558

553559
@Override
554-
public long getLong()
560+
public long asLong()
555561
{
556562
if (!isInLongRange()) {
557563
throw new MessageIntegerOverflowException(longValue);
@@ -560,7 +566,7 @@ public long getLong()
560566
}
561567

562568
@Override
563-
public BigInteger getBigInteger()
569+
public BigInteger asBigInteger()
564570
{
565571
if (type == Type.BIG_INTEGER) {
566572
return (BigInteger) objectValue;
@@ -592,15 +598,15 @@ public Variable setFloatValue(double v)
592598
this.type = Type.DOUBLE;
593599
this.accessor = floatAccessor;
594600
this.doubleValue = v;
595-
this.longValue = (long) v; // AbstractNumberValueAccessor uses castAsLong
601+
this.longValue = (long) v; // AbstractNumberValueAccessor uses toLong
596602
return this;
597603
}
598604

599605
public Variable setFloatValue(float v)
600606
{
601607
this.type = Type.DOUBLE;
602608
this.accessor = floatAccessor;
603-
this.longValue = (long) v; // AbstractNumberValueAccessor uses castAsLong
609+
this.longValue = (long) v; // AbstractNumberValueAccessor uses toLong
604610
return this;
605611
}
606612

@@ -651,19 +657,19 @@ public RawValue asRawValue()
651657
}
652658

653659
@Override
654-
public byte[] getByteArray()
660+
public byte[] asByteArray()
655661
{
656662
return (byte[]) objectValue;
657663
}
658664

659665
@Override
660-
public ByteBuffer getByteBuffer()
666+
public ByteBuffer asByteBuffer()
661667
{
662-
return ByteBuffer.wrap(getByteArray());
668+
return ByteBuffer.wrap(asByteArray());
663669
}
664670

665671
@Override
666-
public String getString()
672+
public String asString()
667673
{
668674
byte[] raw = (byte[]) objectValue;
669675
try {
@@ -678,7 +684,7 @@ public String getString()
678684
}
679685

680686
@Override
681-
public String stringValue()
687+
public String toJson()
682688
{
683689
byte[] raw = (byte[]) objectValue;
684690
try {
@@ -724,7 +730,7 @@ public BinaryValue asBinaryValue()
724730
@Override
725731
public ImmutableBinaryValue immutableValue()
726732
{
727-
return ValueFactory.newBinary(getByteArray());
733+
return ValueFactory.newBinary(asByteArray());
728734
}
729735

730736
@Override
@@ -1044,11 +1050,17 @@ public boolean equals(Object o)
10441050
}
10451051

10461052
@Override
1047-
public String toString()
1053+
public String toJson()
10481054
{
10491055
return immutableValue().toString(); // TODO optimize
10501056
}
10511057

1058+
@Override
1059+
public String toString()
1060+
{
1061+
return toJson();
1062+
}
1063+
10521064
@Override
10531065
public ValueType getValueType()
10541066
{

0 commit comments

Comments
 (0)