-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueId.java
More file actions
453 lines (400 loc) · 13.2 KB
/
UniqueId.java
File metadata and controls
453 lines (400 loc) · 13.2 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package dev.imprex.zip.common;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Enumeration;
import java.util.concurrent.atomic.AtomicInteger;
import dev.imprex.zip.api.ZIPUniqueId;
/**
*
* @author dbs-leipzig
* @see https://github.com/dbs-leipzig/gradoop/blob/develop/gradoop-common/src/main/java/org/gradoop/common/model/impl/id/ScriptedItemId.java
*/
public class UniqueId implements Comparable<UniqueId>, ZIPUniqueId {
/**
* Number of bytes to represent an id internally.
*/
public static final int ID_SIZE = 12;
/**
* Represents a null id.
*/
public static final UniqueId NULL_VALUE =
new UniqueId(0, 0, (short) 0, 0);
/**
* Integer containing a unique identifier of the machine
*/
private static final int MACHINE_IDENTIFIER;
/**
* Short containing a unique identifier of the process
*/
private static final short PROCESS_IDENTIFIER;
/**
* Integer containing a counter that is increased whenever a new id is created
*/
private static final AtomicInteger NEXT_COUNTER = new AtomicInteger(new SecureRandom().nextInt());
/**
* Bit mask used to extract the lowest three bytes of four
*/
private static final int LOW_ORDER_THREE_BYTES = 0x00ffffff;
/**
* Bit mask used to extract the highest byte of four
*/
private static final int HIGH_ORDER_ONE_BYTE = 0xff000000;
/**
* Required for {@link UniqueId#toString()}
*/
private static final char[] HEX_CHARS = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Internal byte representation
*/
private byte[] bytes;
static {
MACHINE_IDENTIFIER = createMachineIdentifier();
PROCESS_IDENTIFIER = createProcessIdentifier();
}
/**
* Required default constructor for instantiation by serialization logic.
*/
public UniqueId() {
bytes = new byte[ID_SIZE];
}
/**
* Creates a ScriptedItemId from a given byte representation
*
* @param bytes the ScriptedItemId represented by the byte array
*/
private UniqueId(byte[] bytes) {
this.bytes = bytes;
}
/**
* Creates a ScriptedItemId using the given time, machine identifier, process identifier, and counter.
* <p>
* Note: Implementation taken from org.bson.types.ObjectId
*
* @param timestamp the time in seconds
* @param machineIdentifier the machine identifier
* @param processIdentifier the process identifier
* @param counter the counter
* @throws IllegalArgumentException if the high order byte of machineIdentifier
* or counter is not zero
*/
public UniqueId(final int timestamp, final int machineIdentifier,
final short processIdentifier, final int counter) {
this(timestamp, machineIdentifier, processIdentifier, counter, true);
}
/**
* Creates a ScriptedItemId using the given time, machine identifier, process identifier, and counter.
* <p>
* Note: Implementation taken from org.bson.types.ObjectId
*
* @param timestamp the time in seconds
* @param machineIdentifier the machine identifier
* @param processIdentifier the process identifier
* @param counter the counter
* @param checkCounter if the constructor should test if the counter is between 0 and
* 16777215
*/
private UniqueId(final int timestamp, final int machineIdentifier, final short processIdentifier,
final int counter, final boolean checkCounter) {
if ((machineIdentifier & HIGH_ORDER_ONE_BYTE) != 0) {
throw new IllegalArgumentException("The machine identifier must be between 0" +
" and 16777215 (it must fit in three bytes).");
}
if (checkCounter && ((counter & HIGH_ORDER_ONE_BYTE) != 0)) {
throw new IllegalArgumentException("The counter must be between 0" +
" and 16777215 (it must fit in three bytes).");
}
ByteBuffer buffer = ByteBuffer.allocate(12);
buffer.put((byte) (timestamp >> 24));
buffer.put((byte) (timestamp >> 16));
buffer.put((byte) (timestamp >> 8));
buffer.put((byte) timestamp);
buffer.put((byte) (machineIdentifier >> 16));
buffer.put((byte) (machineIdentifier >> 8));
buffer.put((byte) machineIdentifier);
buffer.put((byte) (processIdentifier >> 8));
buffer.put((byte) processIdentifier);
buffer.put((byte) (counter >> 16));
buffer.put((byte) (counter >> 8));
buffer.put((byte) counter);
this.bytes = buffer.array();
}
/**
* Creates the machine identifier from the network interface.
* <p>
* Note: Implementation taken from org.bson.types.ObjectId
*
* @return a short representing the process
*/
private static int createMachineIdentifier() {
int machinePiece;
try {
StringBuilder sb = new StringBuilder();
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
sb.append(ni.toString());
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
ByteBuffer bb = ByteBuffer.wrap(mac);
try {
sb.append(bb.getChar());
sb.append(bb.getChar());
sb.append(bb.getChar());
} catch (BufferUnderflowException shortHardwareAddressException) {
// mac with less than 6 bytes. continue
}
}
}
machinePiece = sb.toString().hashCode();
} catch (SocketException t) {
machinePiece = new SecureRandom().nextInt();
}
machinePiece = machinePiece & LOW_ORDER_THREE_BYTES;
return machinePiece;
}
/**
* Creates the process identifier. This does not have to be unique per class loader because
* NEXT_COUNTER will provide the uniqueness.
* <p>
* Note: Implementation taken from org.bson.types.ObjectId
*
* @return a short representing the process
*/
private static short createProcessIdentifier() {
short processId;
String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
if (processName.contains("@")) {
processId = (short) Integer.parseInt(processName.substring(0, processName.indexOf('@')));
} else {
processId = (short) java.lang.management.ManagementFactory
.getRuntimeMXBean().getName().hashCode();
}
return processId;
}
/**
* Returns a new ScriptedItemId
*
* @return new ScriptedItemId
*/
public static UniqueId get() {
return new UniqueId(dateToTimestampSeconds(new Date()), MACHINE_IDENTIFIER,
PROCESS_IDENTIFIER, NEXT_COUNTER.getAndIncrement(), false);
}
/**
* Converts a date into the seconds since unix epoch.
*
* @param time a time
* @return int representing the seconds between unix epoch and the given time
*/
private static int dateToTimestampSeconds(final Date time) {
return (int) (time.getTime() / 1000);
}
/**
* Returns the Gradoop ID represented by a specified hexadecimal string.
* <p>
* Note: Implementation taken from org.bson.types.ObjectId
*
* @param string hexadecimal ScriptedItemId representation
* @return ScriptedItemId
*/
public static UniqueId fromString(String string) {
if (!UniqueId.isValid(string)) {
throw new IllegalArgumentException(
"invalid hexadecimal representation of a ScriptedItemId: [" + string + "]");
}
byte[] b = new byte[12];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) Integer.parseInt(string.substring(i * 2, i * 2 + 2), 16);
}
return new UniqueId(b);
}
/**
* Checks if a string can be transformed into a ScriptedItemId.
* <p>
* Note: Implementation taken from org.bson.types.ObjectId
*
* @param hexString a potential ScriptedItemId as a String.
* @return whether the string could be an object id
* @throws IllegalArgumentException if hexString is null
*/
public static boolean isValid(final String hexString) {
if (hexString == null) {
throw new IllegalArgumentException();
}
int len = hexString.length();
if (len != 24) {
return false;
}
for (int i = 0; i < len; i++) {
char c = hexString.charAt(i);
if (c >= '0' && c <= '9') {
continue;
}
if (c >= 'a' && c <= 'f') {
continue;
}
if (c >= 'A' && c <= 'F') {
continue;
}
return false;
}
return true;
}
/**
* Returns the Gradoop ID represented by a byte array
*
* @param bytes byte representation
* @return Gradoop ID
*/
public static UniqueId fromByteArray(byte[] bytes) {
return new UniqueId(bytes);
}
/**
* Returns byte representation of a ScriptedItemId
*
* @return Byte representation
*/
@Override
public byte[] toByteArray() {
return bytes;
}
/**
* Checks if the specified object is equal to the current id.
*
* @param o the object to be compared
* @return true, iff the specified id is equal to this id
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
byte[] firstBytes = this.bytes;
byte[] secondBytes = ((UniqueId) o).bytes;
for (int i = 0; i < UniqueId.ID_SIZE; i++) {
if (firstBytes[i] != secondBytes[i]) {
return false;
}
}
return true;
}
/**
* Returns the hash code of this ScriptedItemId.
* <p>
* Note: Implementation taken from org.bson.types.ObjectId
*
* @return hash code
*/
@Override
public int hashCode() {
int result = getTimeStamp();
result = 31 * result + getMachineIdentifier();
result = 31 * result + (int) getProcessIdentifier();
result = 31 * result + getCounter();
return result;
}
/**
* Performs a byte-wise comparison of this and the specified ScriptedItemId.
*
* @param other the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*/
@Override
public int compareTo(UniqueId other) {
for (int i = 0; i < UniqueId.ID_SIZE; i++) {
if (this.bytes[i] != other.bytes[i]) {
return ((this.bytes[i] & 0xff) < (other.bytes[i] & 0xff)) ? -1 : 1;
}
}
return 0;
}
/**
* Returns hex string representation of a ScriptedItemId.
* <p>
* Note: Implementation taken from org.bson.types.ObjectId
*
* @return ScriptedItemId string representation.
*/
@Override
public String toString() {
char[] chars = new char[24];
int i = 0;
for (byte b : bytes) {
chars[i++] = HEX_CHARS[b >> 4 & 0xF];
chars[i++] = HEX_CHARS[b & 0xF];
}
return String.valueOf(chars);
}
//------------------------------------------------------------------------------------------------
// private little helpers
//------------------------------------------------------------------------------------------------
/**
* Returns the timestamp component of the id.
*
* @return the timestamp
*/
private int getTimeStamp() {
return makeInt(bytes[0], bytes[1], bytes[2], bytes[3]);
}
/**
* Returns the machine identifier component of the id.
*
* @return the machine identifier
*/
private int getMachineIdentifier() {
return makeInt((byte) 0, bytes[4], bytes[5], bytes[6]);
}
/**
* Returns the process identifier component of the id.
*
* @return the process identifier
*/
private short getProcessIdentifier() {
return (short) makeInt((byte) 0, (byte) 0, bytes[7], bytes[8]);
}
/**
* Returns the counter component of the id.
*
* @return the counter
*/
private int getCounter() {
return makeInt((byte) 0, bytes[9], bytes[10], bytes[11]);
}
//------------------------------------------------------------------------------------------------
// static helper functions
//------------------------------------------------------------------------------------------------
/**
* Compares the given ScriptedItemIds and returns the smaller one. It both are equal, the first
* argument is returned.
*
* @param first first ScriptedItemId
* @param second second ScriptedItemId
* @return smaller ScriptedItemId or first if equal
*/
public static UniqueId min(UniqueId first, UniqueId second) {
int comparison = first.compareTo(second);
return comparison == 0 ? first : (comparison < 0 ? first : second);
}
/**
* Returns a primitive int represented by the given 4 bytes.
*
* @param b3 byte 3
* @param b2 byte 2
* @param b1 byte 1
* @param b0 byte 0
* @return int value
*/
private static int makeInt(final byte b3, final byte b2, final byte b1, final byte b0) {
return (b3 << 24) | ((b2 & 0xff) << 16) | ((b1 & 0xff) << 8) | ((b0 & 0xff));
}
}