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
213 lines (183 loc) · 4.64 KB
/
Value.java
File metadata and controls
213 lines (183 loc) · 4.64 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
package org.tron.common.storage;
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.AssetIssueCapsule;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.BytesCapsule;
import org.tron.core.capsule.CodeCapsule;
import org.tron.core.capsule.ContractCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.capsule.VotesCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.exception.BadItemException;
/**
* @author Guo Yonggang
* @since 27.04.2018
*/
public class Value {
private Type type;
private byte[] any = null;
/**
* @param any
*/
public Value(byte[] any, Type type) {
if (any != null && any.length > 0) {
this.any = new byte[any.length];
System.arraycopy(any, 0, this.any, 0, any.length);
this.type = type.clone();
}
}
/**
* @param any
* @param type
*/
public Value(byte[] any, int type) {
if (any != null && any.length > 0) {
this.any = new byte[any.length];
System.arraycopy(any, 0, this.any, 0, any.length);
this.type = new Type(type);
}
}
/**
* @param value
*/
private Value(Value value) {
if (value.getAny() != null && value.getAny().length > 0) {
this.any = new byte[any.length];
System.arraycopy(value.getAny(), 0, this.any, 0, value.getAny().length);
this.type = value.getType().clone();
}
}
/**
* @return
*/
public Value clone() {
return new Value(this);
}
/**
* @return
*/
public byte[] getAny() {
return any;
}
/**
* @return
*/
public Type getType() {
return type;
}
/**
* @param type
*/
public void setType(Type type) {
this.type = type;
}
/**
* @param type
*/
public void addType(Type type) {
this.type.addType(type);
}
/**
* @param type
*/
public void addType(int type) {
this.type.addType(type);
}
/**
* @return
*/
public AccountCapsule getAccount() {
if (ArrayUtils.isEmpty(any)) return null;
return new AccountCapsule(any);
}
/**
* @return
*/
public BytesCapsule getBytes() {
if (ArrayUtils.isEmpty(any)) {
return null;
}
return new BytesCapsule(any);
}
/**
* @return
*/
public TransactionCapsule getTransaction() {
if (ArrayUtils.isEmpty(any)) return null;
try {
return new TransactionCapsule(any);
} catch (BadItemException e) {
return null;
}
}
/**
* @return
*/
public BlockCapsule getBlock() {
if (ArrayUtils.isEmpty(any)) return null;
try {
return new BlockCapsule(any);
} catch (Exception e) {
return null;
}
}
/**
* @return
*/
public WitnessCapsule getWitness() {
if (ArrayUtils.isEmpty(any)) return null;
return new WitnessCapsule(any);
}
public VotesCapsule getVotes() {
if (ArrayUtils.isEmpty(any)) return null;
return new VotesCapsule(any);
}
/**
* @return
*/
public BytesCapsule getBlockIndex() {
if (ArrayUtils.isEmpty(any)) return null;
return new BytesCapsule(any);
}
/**
* @return
*/
public CodeCapsule getCode() {
if (ArrayUtils.isEmpty(any)) return null;
return new CodeCapsule(any);
}
/**
* @return
*/
public ContractCapsule getContract() {
if (ArrayUtils.isEmpty(any)) return null;
return new ContractCapsule(any);
}
public AssetIssueCapsule getAssetIssue() {
if (ArrayUtils.isEmpty(any)) return null;
return new AssetIssueCapsule(any);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || obj.getClass() != getClass()) return false;
Value V = (Value)obj;
if (Arrays.equals(this.any, V.getAny())) return true;
return false;
}
@Override
public int hashCode() {
return new Integer(type.hashCode() + Arrays.hashCode(any)).hashCode();
}
public static Value create(byte[] any, int type) {
return new Value(any, type);
}
public static Value create(byte[] any, Type type) {
return new Value(any, type);
}
public static Value create(byte[] any) {
return new Value(any, Type.VALUE_TYPE_NORMAL);
}
}