forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValue.java
More file actions
353 lines (290 loc) · 8.09 KB
/
Value.java
File metadata and controls
353 lines (290 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.common.utils;
import com.cedarsoftware.util.DeepEquals;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import org.spongycastle.util.encoders.Hex;
/**
* Class to encapsulate an object and provide utilities for conversion
*/
public class Value {
private Object value;
private byte[] serializable;
private byte[] sha3;
private boolean decoded = false;
public Value() {
}
public Value(Object obj) {
this.decoded = true;
if (obj == null) {
return;
}
if (obj instanceof Value) {
this.value = ((Value) obj).asObj();
} else {
this.value = obj;
}
}
public static Value fromSerEncoded(byte[] data) {
if (data != null && data.length != 0) {
Value v = new Value();
v.init(data);
return v;
}
return null;
}
public void init(byte[] serializable) {
this.serializable = serializable;
}
public Value withHash(byte[] hash) {
sha3 = hash;
return this;
}
/* *****************
* Convert
* *****************/
public Object asObj() {
// decode();
return value;
}
public List<Object> asList() {
// decode();
Object[] valueArray = (Object[]) value;
return Arrays.asList(valueArray);
}
public int asInt() {
//decode();
if (isInt()) {
return (Integer) value;
} else if (isBytes()) {
return new BigInteger(1, asBytes()).intValue();
}
return 0;
}
public long asLong() {
// decode();
if (isLong()) {
return (Long) value;
} else if (isBytes()) {
return new BigInteger(1, asBytes()).longValueExact();
}
return 0;
}
public BigInteger asBigInt() {
// decode();
return (BigInteger) value;
}
public String asString() {
// decode();
if (isBytes()) {
return new String((byte[]) value);
} else if (isString()) {
return (String) value;
}
return "";
}
public byte[] asBytes() {
// decode();
if (isBytes()) {
return (byte[]) value;
} else if (isString()) {
return asString().getBytes();
}
return ByteUtil.EMPTY_BYTE_ARRAY;
}
public int[] asSlice() {
return (int[]) value;
}
public Value get(int index) {
if (isList()) {
// Guard for OutOfBounds
if (asList().size() <= index) {
return new Value(null);
}
if (index < 0) {
throw new RuntimeException("Negative index not allowed");
}
return new Value(asList().get(index));
}
// If this wasn't a slice you probably shouldn't be using this function
return new Value(null);
}
/* *****************
* Utility
* *****************/
public boolean cmp(Value o) {
return DeepEquals.deepEquals(this, o);
}
/* *****************
* Checks
* *****************/
public boolean isList() {
// decode();
return value != null && value.getClass().isArray() && !value.getClass().getComponentType()
.isPrimitive();
}
public boolean isString() {
// decode();
return value instanceof String;
}
public boolean isInt() {
//decode();
return value instanceof Integer;
}
public boolean isLong() {
//decode();
return value instanceof Long;
}
public boolean isBigInt() {
// decode();
return value instanceof BigInteger;
}
public boolean isBytes() {
//decode();
return value instanceof byte[];
}
// it's only if the isBytes() = true;
public boolean isReadableString() {
// decode();
int readableChars = 0;
byte[] data = (byte[]) value;
if (data.length == 1 && data[0] > 31 && data[0] < 126) {
return true;
}
for (byte aData : data) {
if (aData > 32 && aData < 126) {
++readableChars;
}
}
return (double) readableChars / (double) data.length > 0.55;
}
// it's only if the isBytes() = true;
public boolean isHexString() {
//decode();
int hexChars = 0;
byte[] data = (byte[]) value;
for (byte aData : data) {
if ((aData >= 48 && aData <= 57)
|| (aData >= 97 && aData <= 102)) {
++hexChars;
}
}
return (double) hexChars / (double) data.length > 0.9;
}
public boolean isHashCode() {
//decode();
return this.asBytes().length == 32;
}
public boolean isNull() {
//decode();
return value == null;
}
public boolean isEmpty() {
// decode();
if (isNull()) {
return true;
}
if (isBytes() && asBytes().length == 0) {
return true;
}
if (isList() && asList().isEmpty()) {
return true;
}
return isString() && asString().equals("");
}
public int length() {
//decode();
if (isList()) {
return asList().size();
} else if (isBytes()) {
return asBytes().length;
} else if (isString()) {
return asString().length();
}
return 0;
}
public String toString() {
//decode();
StringBuilder stringBuilder = new StringBuilder();
if (isList()) {
Object[] list = (Object[]) value;
// special case - key/value node
if (list.length == 2) {
stringBuilder.append("[ ");
Value key = new Value(list[0]);
byte[] keyNibbles = CompactEncoder.binToNibblesNoTerminator(key.asBytes());
String keyString = ByteUtil.nibblesToPrettyString(keyNibbles);
stringBuilder.append(keyString);
stringBuilder.append(",");
Value val = new Value(list[1]);
stringBuilder.append(val.toString());
stringBuilder.append(" ]");
return stringBuilder.toString();
}
stringBuilder.append(" [");
for (int i = 0; i < list.length; ++i) {
Value val = new Value(list[i]);
if (val.isString() || val.isEmpty()) {
stringBuilder.append("'").append(val.toString()).append("'");
} else {
stringBuilder.append(val.toString());
}
if (i < list.length - 1) {
stringBuilder.append(", ");
}
}
stringBuilder.append("] ");
return stringBuilder.toString();
} else if (isEmpty()) {
return "";
} else if (isBytes()) {
StringBuilder output = new StringBuilder();
if (isHashCode()) {
output.append(Hex.toHexString(asBytes()));
} else if (isReadableString()) {
output.append("'");
for (byte oneByte : asBytes()) {
if (oneByte < 16) {
output.append("\\x").append(ByteUtil.oneByteToHexString(oneByte));
} else {
output.append(Character.valueOf((char) oneByte));
}
}
output.append("'");
return output.toString();
}
return Hex.toHexString(this.asBytes());
} else if (isString()) {
return asString();
}
return "Unexpected type";
}
public int countBranchNodes() {
// decode();
if (this.isList()) {
return this.asList().stream()
.mapToInt(obj -> (new Value(obj)).countBranchNodes())
.sum();
} else if (this.isBytes()) {
this.asBytes();
}
return 0;
}
}