1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  package org.waarp.common.guid;
21  
22  import org.waarp.common.utility.Hexa;
23  import org.waarp.common.utility.StringUtils;
24  
25  import java.util.Arrays;
26  import java.util.concurrent.atomic.AtomicInteger;
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  public final class IntegerUuid {
42    
43  
44  
45    private static final AtomicInteger COUNTER =
46        new AtomicInteger(StringUtils.RANDOM.nextInt());
47    
48  
49  
50    private static final int UUIDSIZE = 4;
51  
52    
53  
54  
55    private final byte[] uuid = { 0, 0, 0, 0 };
56  
57    static final synchronized int getCounter() {
58      if (COUNTER.compareAndSet(Integer.MAX_VALUE, Integer.MIN_VALUE)) {
59        return Integer.MAX_VALUE;
60      } else {
61        return COUNTER.getAndIncrement();
62      }
63    }
64  
65    
66  
67  
68  
69    public IntegerUuid() {
70      
71      final int count = getCounter();
72      uuid[0] = (byte) (count >> 24);
73      uuid[1] = (byte) (count >> 16);
74      uuid[2] = (byte) (count >> 8);
75      uuid[3] = (byte) count;
76    }
77  
78    
79  
80  
81  
82  
83    public IntegerUuid(final byte[] bytes) {
84      if (bytes.length != UUIDSIZE) {
85        throw new RuntimeException(
86            "Attempted to parse malformed UUID: " + Arrays.toString(bytes));
87      }
88      System.arraycopy(bytes, 0, uuid, 0, UUIDSIZE);
89    }
90  
91    public IntegerUuid(final int value) {
92      uuid[0] = (byte) (value >> 24);
93      uuid[1] = (byte) (value >> 16);
94      uuid[2] = (byte) (value >> 8);
95      uuid[3] = (byte) value;
96    }
97  
98    public IntegerUuid(final String idsource) {
99      final String id = idsource.trim();
100 
101     if (id.length() != UUIDSIZE * 2) {
102       throw new IllegalArgumentException(
103           "Attempted to parse malformed UUID: " + id);
104     }
105     System.arraycopy(Hexa.fromHex(id), 0, uuid, 0, UUIDSIZE);
106   }
107 
108   @Override
109   public String toString() {
110     return Hexa.toHex(uuid);
111   }
112 
113   
114 
115 
116 
117 
118   public final byte[] getBytes() {
119     return Arrays.copyOf(uuid, UUIDSIZE);
120   }
121 
122   
123 
124 
125 
126 
127   public final long getTimestamp() {
128     long time;
129     time = ((long) uuid[0] & 0xFF) << 24;
130     time |= ((long) uuid[2] & 0xFF) << 16;
131     time |= ((long) uuid[2] & 0xFF) << 8;
132     time |= (long) uuid[3] & 0xFF;
133     return time;
134   }
135 
136   @Override
137   public final boolean equals(final Object o) {
138     if (!(o instanceof IntegerUuid)) {
139       return false;
140     }
141     return this == o || Arrays.equals(uuid, ((IntegerUuid) o).uuid);
142   }
143 
144   @Override
145   public final int hashCode() {
146     return Arrays.hashCode(uuid);
147   }
148 
149   
150 
151 
152   public final int getInt() {
153     int value = ((int) uuid[0] & 0xFF) << 24;
154     value |= ((long) uuid[1] & 0xFF) << 16;
155     value |= ((long) uuid[2] & 0xFF) << 8;
156     value |= (long) uuid[3] & 0xFF;
157     return value;
158   }
159 }