Repository navigation
Expand file tree
/
Copy pathValue.java
More file actions
82 lines (67 loc) · 1.82 KB
/
Copy pathValue.java
File metadata and controls
82 lines (67 loc) · 1.82 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
package org.tron.core.db2.common;
import java.util.Arrays;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.tron.core.db.common.WrappedByteArray;
@EqualsAndHashCode(exclude = "operator")
public final class Value {
public byte[] encode() {
if (data.getBytes() == null) {
return new byte[]{operator.getValue()};
}
byte[] r = new byte[1 + data.getBytes().length];
r[0] = operator.getValue();
System.arraycopy(data.getBytes(), 0, r, 1, data.getBytes().length);
return r;
}
public static Value decode(byte[] bytes) {
Operator operator = Operator.valueOf(bytes[0]);
byte[] value = null;
if (bytes.length > 1) {
value = Arrays.copyOfRange(bytes, 1, bytes.length);
}
return Value.of(operator, value);
}
public enum Operator {
CREATE((byte) 0),
MODIFY((byte) 1),
DELETE((byte) 2);
@Getter
private byte value;
Operator(byte value) {
this.value = value;
}
static Operator valueOf(byte b) {
switch (b) {
case 0:
return Operator.CREATE;
case 1:
return Operator.MODIFY;
case 2:
return Operator.DELETE;
default:
return null;
}
}
}
@Getter
final private Operator operator;
final private WrappedByteArray data;
private Value(Operator operator, WrappedByteArray data) {
this.operator = operator;
this.data = data;
}
public static Value copyOf(Operator operator, byte[] data) {
return new Value(operator, WrappedByteArray.copyOf(data));
}
public static Value of(Operator operator, byte[] data) {
return new Value(operator, WrappedByteArray.of(data));
}
public byte[] getBytes() {
byte[] value = data.getBytes();
if (value == null) {
return null;
}
return Arrays.copyOf(value, value.length);
}
}