Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.common

File Identifier.java

 

Coverage histogram

../../../img/srcFileCovDistChart7.png
25% of files have more coverage

Code metrics

24
49
18
1
153
125
32
0.65
2.72
18
1.78

Classes

Class Line # Actions
Identifier 22 49 32
0.659340765.9%
 

Contributing tests

This file is covered by 17 tests. .

Source view

1    package io.sunshower.common;
2   
3    import static io.sunshower.encodings.Base58.Alphabets.Default;
4    import static java.lang.String.format;
5   
6    import io.sunshower.encodings.Base58;
7    import io.sunshower.encodings.Encoding;
8    import io.sunshower.persist.Sequence;
9    import io.sunshower.persist.internal.jaxb.Base58ByteArrayConverter;
10    import java.io.Serializable;
11    import java.nio.ByteBuffer;
12    import java.util.Arrays;
13    import java.util.UUID;
14    import javax.xml.bind.annotation.XmlAccessType;
15    import javax.xml.bind.annotation.XmlAccessorType;
16    import javax.xml.bind.annotation.XmlAttribute;
17    import javax.xml.bind.annotation.XmlRootElement;
18    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
19   
20    @XmlRootElement(name = "id")
21    @XmlAccessorType(XmlAccessType.NONE)
 
22    public class Identifier implements Comparable<Identifier>, Serializable {
23   
24    static final transient Encoding base58 = Base58.getInstance(Default);
25   
26    static final transient Sequence<Identifier> randomSequence =
27    io.sunshower.persist.Identifiers.randomSequence();
28   
29    @XmlAttribute
30    @XmlJavaTypeAdapter(Base58ByteArrayConverter.class)
31    volatile byte[] id;
32   
 
33  2 toggle protected Identifier() {}
34   
 
35  0 toggle public byte[] value() {
36  0 return id;
37    }
38   
 
39  100003 toggle public static Identifier valueOf(UUID id) {
40  100003 final ByteBuffer bb = ByteBuffer.allocate(16);
41  100003 bb.putLong(id.getMostSignificantBits());
42  100003 bb.putLong(id.getLeastSignificantBits());
43  100003 return new Identifier(bb.array());
44    }
45   
 
46  101909 toggle public static Identifier valueOf(byte[] id) {
47  101909 return new Identifier(id);
48    }
49   
 
50  501918 toggle Identifier(byte[] id) {
51  501918 if (id == null || id.length != 16) {
52  0 throw new IllegalArgumentException("Argument cannot possibly be a valid identifier");
53    }
54  501918 this.id = id;
55    }
56   
 
57  417 toggle @Override
58    public int compareTo(Identifier o) {
59  417 if (o == null) {
60  0 return 1;
61    }
62   
63  6664 for (int i = 0; i < 16; i++) {
64  6664 final byte v = id[i];
65  6664 final byte tv = o.id[i];
66   
67  6664 if (v > tv) {
68  386 return 1;
69  6278 } else if (tv > v) {
70  31 return -1;
71    }
72    }
73  0 return 0;
74    }
75   
 
76  1597 toggle @Override
77    public boolean equals(Object o) {
78  1597 if (o == null) {
79  0 return false;
80    }
81   
82  1597 if (o == this) {
83  2 return true;
84    }
85   
86  1595 if (o.getClass().equals(Identifier.class)) {
87  1595 final Identifier other = (Identifier) o;
88  1595 return Arrays.equals(id, other.id);
89    }
90  0 return false;
91    }
92   
 
93  1506 toggle @Override
94    public int hashCode() {
95  1506 return id == null ? 0 : Arrays.hashCode(id);
96    }
97   
 
98  300008 toggle @Override
99    public String toString() {
100  300008 return toString(Default);
101    }
102   
 
103  0 toggle public static final Identifier random() {
104  0 return randomSequence.next();
105    }
106   
 
107  300008 toggle public String toString(Base58.Alphabets alphabet) {
108  300008 if (id == null) {
109  0 return "id[null]";
110    }
111  300008 switch (alphabet) {
112  300008 case Default:
113  300008 return base58.encode(Arrays.copyOf(id, id.length));
114  0 default:
115  0 return Base58.getInstance(alphabet).encode(Arrays.copyOf(id, id.length));
116    }
117    }
118   
 
119  100004 toggle public static Identifier valueOf(String id) {
120  100004 if (id == null) {
121  0 throw new IllegalArgumentException("Identifier cannot be null");
122    }
123  100004 return new Identifier(base58.decode(id));
124    }
125   
 
126  0 toggle public static Identifier copyOf(Identifier id) {
127  0 return new Identifier(id.id);
128    }
129   
 
130  0 toggle public byte[] getBytes() {
131  0 return Arrays.copyOf(id, id.length);
132    }
133   
 
134  200003 toggle public static boolean isIdentifier(String id) {
135  200003 byte[] decode = base58.decode(id);
136  200003 return id != null && decode.length == 16 && base58.test(id);
137    }
138   
 
139  200002 toggle public static Identifier decode(String s) {
140  200002 if (isIdentifier(s)) {
141  200002 return new Identifier(base58.decode(s));
142    }
143  0 throw new IllegalArgumentException(format("Invalid identifier: '%s'", s));
144    }
145   
 
146  0 toggle public void setId(String id) {
147  0 this.id = base58.decode(id);
148    }
149   
 
150  0 toggle public byte[] getId() {
151  0 return id;
152    }
153    }