-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentifier.java
More file actions
81 lines (71 loc) · 1.93 KB
/
Identifier.java
File metadata and controls
81 lines (71 loc) · 1.93 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
package model.lightchain;
import java.io.Serializable;
import java.util.Arrays;
import io.ipfs.multibase.Multibase;
/**
* Represents a 32-byte unique identifier for an entity. Normally is computed as the hash value of the entity.
*/
public class Identifier implements Serializable {
public static final int Size = 32;
private final byte[] value;
public Identifier(byte[] value) {
this.value = value.clone();
}
/**
* Returns if objects equal.
*
* @param o an identifier object.
* @return true if objcets equal.
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Identifier that = (Identifier) o;
return Arrays.equals(value, that.value);
}
/**
* Return the hashCode.
*
* @return hashCode.
*/
@Override
public int hashCode() {
return Arrays.hashCode(value);
}
public byte[] getBytes() {
return this.value.clone();
}
/**
* Returns string representation of identifier in Base58BTC.
*
* @return string representation of identifier in Base58BTC.
*/
public String toString() {
return pretty(this.value);
}
/**
* Converts identifier from its byte representation to Base58BTC.
*
* @param identifier input identifier in byte representation.
* @return Base58BTC representation of identifier.
*/
private static String pretty(byte[] identifier) {
return Multibase.encode(Multibase.Base.Base58BTC, identifier);
}
/**
* Compares this identifier with the other identifier.
*
* @param other represents other identifier to compared to.
* @return 0 if two identifiers are equal, 1 if this identifier is greater than other,
* -1 if other identifier is greater than this.
*/
public int comparedTo(Identifier other) {
int result = Arrays.compare(this.value, other.value);
return Integer.compare(result, 0);
}
}