/
AbstractUUIDGenerator.java
84 lines (73 loc) · 1.88 KB
/
AbstractUUIDGenerator.java
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
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.id;
import java.net.InetAddress;
import org.hibernate.id.factory.spi.StandardGenerator;
import org.hibernate.internal.util.BytesHelper;
/**
* The base class for identifier generators that use a UUID algorithm. This
* class implements the algorithm, subclasses define the identifier
* format.
*
* @see UUIDHexGenerator
* @author Gavin King
*
* @deprecated since {@link UUIDHexGenerator} is deprecated
*/
@Deprecated(since = "6")
public abstract class AbstractUUIDGenerator implements IdentifierGenerator, StandardGenerator {
private static final int IP;
static {
int ipadd;
try {
ipadd = BytesHelper.toInt( InetAddress.getLocalHost().getAddress() );
}
catch (Exception e) {
ipadd = 0;
}
IP = ipadd;
}
private static short counter = (short) 0;
private static final int JVM = (int) ( System.currentTimeMillis() >>> 8 );
public AbstractUUIDGenerator() {
}
/**
* Unique across JVMs on this machine (unless they load this class
* in the same quarter-second, which is very unlikely)
*/
protected int getJVM() {
return JVM;
}
/**
* Unique in a millisecond for this JVM instance (unless there
* are more than {@value Short#MAX_VALUE} instances created in
* a millisecond)
*/
protected short getCount() {
synchronized(AbstractUUIDGenerator.class) {
if ( counter < 0 ) {
counter=0;
}
return counter++;
}
}
/**
* Unique in a local network
*/
protected int getIP() {
return IP;
}
/**
* Unique down to millisecond
*/
protected short getHiTime() {
return (short) ( System.currentTimeMillis() >>> 32 );
}
protected int getLoTime() {
return (int) System.currentTimeMillis();
}
}