Skip to content

Commit 9dae25e

Browse files
committed
msgpack#109: Applied fixes suggested by @komamitsu
1 parent ed3fdd6 commit 9dae25e

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

msgpack-core/src/main/java/org/msgpack/value/holder/IntegerHolder.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ValueType getValueType() {
2222
}
2323
@Override
2424
public void writeTo(MessagePacker packer) throws IOException {
25-
switch(tpe) {
25+
switch(type) {
2626
case BIG_INTEGER:
2727
packer.packBigInteger(biValue);
2828
break;
@@ -44,7 +44,7 @@ public void accept(ValueVisitor visitor) {
4444

4545
@Override
4646
public IntegerValue toValue() {
47-
switch(tpe){
47+
switch(type){
4848
case BYTE:
4949
return ValueFactory.newByte(toByte());
5050
case SHORT:
@@ -68,32 +68,32 @@ public static enum Type {
6868
BIG_INTEGER
6969
}
7070

71-
private Type tpe;
71+
private Type type;
7272
private long longValue;
7373
private BigInteger biValue;
7474

7575
public Type getType() {
76-
return tpe;
76+
return type;
7777
}
7878

7979
public void setByte(byte v){
80-
tpe = Type.BYTE;
80+
type = Type.BYTE;
8181
longValue = v;
8282
}
8383
public void setShort(short v) {
84-
tpe = Type.SHORT;
84+
type = Type.SHORT;
8585
longValue = v;
8686
}
8787
public void setInt(int v) {
88-
tpe = Type.INT;
88+
type = Type.INT;
8989
longValue = v;
9090
}
9191
public void setLong(long v) {
92-
tpe = Type.LONG;
92+
type = Type.LONG;
9393
longValue = v;
9494
}
9595
public void setBigInteger(BigInteger v) {
96-
tpe = Type.BIG_INTEGER;
96+
type = Type.BIG_INTEGER;
9797
biValue = v;
9898
}
9999

@@ -102,47 +102,52 @@ private RuntimeException failure() {
102102
}
103103

104104
public boolean isBigInteger() {
105-
return tpe == Type.BIG_INTEGER;
105+
return type == Type.BIG_INTEGER;
106106
}
107107

108108
@Override
109109
public boolean isValidByte() {
110-
return tpe == Type.BYTE;
110+
return type == Type.BYTE;
111111
}
112112
@Override
113113
public boolean isValidShort() {
114-
return tpe.ordinal() <= Type.SHORT.ordinal();
114+
return type.ordinal() <= Type.SHORT.ordinal();
115115
}
116116
@Override
117117
public boolean isValidInt() {
118-
return tpe.ordinal() <= Type.INT.ordinal();
118+
return type.ordinal() <= Type.INT.ordinal();
119119
}
120120
@Override
121121
public boolean isValidLong() {
122-
return tpe.ordinal() <= Type.LONG.ordinal();
122+
return type.ordinal() <= Type.LONG.ordinal();
123123
}
124124

125125
@Override
126126
public boolean isWhole() {
127127
return true;
128128
}
129129

130+
@Override
130131
public byte toByte() {
131132
return isBigInteger() ? biValue.byteValue() : (byte) longValue;
132133
}
133134

135+
@Override
134136
public short toShort() {
135137
return isBigInteger() ? biValue.shortValue() : (short) longValue;
136138
}
137139

140+
@Override
138141
public int toInt() {
139142
return isBigInteger() ? biValue.intValue() : (int) longValue;
140143
}
141144

145+
@Override
142146
public long toLong(){
143147
return isBigInteger() ? biValue.longValue() : longValue;
144148
}
145149

150+
@Override
146151
public BigInteger toBigInteger() {
147152
return isBigInteger() ? biValue : BigInteger.valueOf(longValue);
148153
}
@@ -158,7 +163,7 @@ public double toDouble() {
158163

159164
@Override
160165
public byte asByte() throws MessageIntegerOverflowException {
161-
switch(tpe) {
166+
switch(type) {
162167
case BYTE:
163168
return (byte) longValue;
164169
case SHORT:
@@ -185,7 +190,7 @@ public byte asByte() throws MessageIntegerOverflowException {
185190

186191
@Override
187192
public short asShort() throws MessageIntegerOverflowException {
188-
switch(tpe) {
193+
switch(type) {
189194
case BYTE:
190195
case SHORT:
191196
return (short) longValue;
@@ -212,7 +217,7 @@ public short asShort() throws MessageIntegerOverflowException {
212217

213218
@Override
214219
public int asInt() throws MessageIntegerOverflowException {
215-
switch(tpe) {
220+
switch(type) {
216221
case BYTE:
217222
case SHORT:
218223
case INT:
@@ -256,7 +261,14 @@ public BigInteger asBigInteger() {
256261

257262
@Override
258263
public int hashCode() {
259-
return isBigInteger() ? biValue.hashCode() : (int) longValue;
264+
int hash = 0;
265+
if(isBigInteger()) {
266+
hash = biValue.hashCode();
267+
}
268+
else {
269+
hash = (int)((longValue >>> 32) * 31 + longValue & 0xFFFFFFFF);
270+
}
271+
return hash;
260272
}
261273

262274
@Override

msgpack-core/src/main/java/org/msgpack/value/holder/RawHolder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,21 @@ public RawValue toValue() {
135135
public class RawHolder extends RawHolderImpl {
136136

137137
private static class StringValueWrap extends RawHolderImpl implements StringValue {
138+
@Override
138139
public StringValue toValue() {
139140
return ValueFactory.newRawString(buf.toByteArray());
140141
}
141142
}
142143

143144
private static class BinaryValueWrap extends RawHolderImpl implements BinaryValue {
145+
@Override
144146
public BinaryValue toValue() {
145147
return ValueFactory.newBinary(buf.toByteArray());
146148
}
147149
}
148150

149-
private StringValueWrap stringWrap = new StringValueWrap();
150-
private BinaryValueWrap binaryWrap = new BinaryValueWrap();
151+
private final StringValueWrap stringWrap = new StringValueWrap();
152+
private final BinaryValueWrap binaryWrap = new BinaryValueWrap();
151153

152154
@Override
153155
public void setString(MessageBuffer buf) {
@@ -180,6 +182,7 @@ public byte[] toByteArray() {
180182
public ByteBuffer toByteBuffer() {
181183
switch(tpe) {
182184
case STRING:
185+
case BINARY:
183186
return buf.toByteBuffer();
184187
default:
185188
throw UNREACHABLE;

0 commit comments

Comments
 (0)