Author: andy
Date: Mon Apr 21 20:26:12 2014
New Revision: 1588976
URL: http://svn.apache.org/r1588976
Log:
Tidy Jena' UUID generation.
For version 1 UUIDs, use the first hardware MAC address that can be found, now
that Java
allows access to the hardware address.
Modified:
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/JenaUUID.java
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/LibUUID.java
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUIDFactory.java
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1.java
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1_Gen.java
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4.java
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4_Gen.java
Modified:
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/JenaUUID.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/JenaUUID.java?rev=1588976&r1=1588975&r2=1588976&view=diff
==============================================================================
---
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/JenaUUID.java
(original)
+++
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/JenaUUID.java
Mon Apr 21 20:26:12 2014
@@ -20,13 +20,13 @@
* http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
*/
-package com.hp.hpl.jena.shared.uuid;
+package com.hp.hpl.jena.shared.uuid ;
import java.util.Locale ;
-import java.util.UUID;
+import java.util.UUID ;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
// TO DO
// + Comments and renaming.
@@ -38,75 +38,123 @@ import org.slf4j.LoggerFactory;
public abstract class JenaUUID
{
-
+
// Java6: get MAC address:
- // http://www.kodejava.org/examples/250.html
-
- static final int HEX = 16 ;
- static final int Var_NCS = 0 ;
- static final int Var_Std = 2 ; // Same as DCE
- static final int Var_DCE = 2 ;
- static final int Var_MS_GUID = 6 ;
+ // http://www.kodejava.org/examples/250.html
+
+ static final int HEX = 16 ;
+ static final int Var_NCS = 0 ;
+ static final int Var_Std = 2 ; // Same as DCE
+ static final int Var_DCE = 2 ;
+ static final int Var_MS_GUID = 6 ;
static final int Var_Reserved = 7 ;
-
- abstract public int getVersion() ;
+
+ abstract public int getVersion() ;
+
abstract public int getVariant() ;
-
+
abstract public long getMostSignificantBits() ;
+
abstract public long getLeastSignificantBits() ;
-
- protected int _getVersion(long mostSigBits, long leastSigBits)
- {
-// int variant = (int)((UUID_V1_Gen.maskVariant & leastSigBits)>>>62) ;
-// int version = (int)((UUID_V1_Gen.maskVersion & mostSigBits)>>>12) ;
+
+ protected int _getVersion(long mostSigBits, long leastSigBits) {
+ // int variant = (int)((UUID_V1_Gen.maskVariant & leastSigBits)>>>62) ;
+ // int version = (int)((UUID_V1_Gen.maskVersion & mostSigBits)>>>12) ;
int version = (int)Bits.unpack(mostSigBits, 12, 16) ;
return version ;
- }
-
- protected int _getVariant(long mostSigBits, long leastSigBits)
- {
+ }
+
+ protected int _getVariant(long mostSigBits, long leastSigBits) {
int variant = (int)Bits.unpack(leastSigBits, 62, 64) ;
return variant ;
- }
-
- protected JenaUUID()
- {}
-
+ }
+
+ protected JenaUUID() {}
+
/** Format as a string - no URI scheme **/
- public String asString() { return toString() ; }
+ public String asString() {
+ return toString() ;
+ }
+
/** Format as a URI - that is uuid:ABCD */
- public String asURI() { return "uuid:"+toString() ; }
+ public String asURI() {
+ return "uuid:" + toString() ;
+ }
+
/** Format as a URN - that is urn:uuid:ABCD */
- public String asURN() { return "urn:uuid:"+toString() ; }
+ public String asURN() {
+ return "urn:uuid:" + toString() ;
+ }
- /** Return a {@link java.util.UUID} for this Jena-generated UUID */
- public UUID asUUID()
- {
+ /** Return a {@link java.util.UUID} for this Jena-generated UUID */
+ public UUID asUUID() {
return new UUID(getMostSignificantBits(), getLeastSignificantBits()) ;
}
- // ----------------------------------------------------
- // Factory
+ @Override
+ public String toString() {
+ return toString(this) ;
+ }
- static UUIDFactory factory = new UUID_V1_Gen() ;
- public static void setFactory(UUIDFactory factory) { JenaUUID.factory =
factory ; }
- public static UUIDFactory getFactory() { return factory ; }
+ // Time low - which includes the incremental count.
+ @Override
+ public int hashCode() { return (int) Bits.unpack(getMostSignificantBits(),
32, 64) ; }
+ @Override
+ public boolean equals(Object other)
+ {
+ if ( this == other ) return true ;
+ if ( other == null ) return false ;
+ if ( ! ( other instanceof JenaUUID ) )
+ return false ;
+ JenaUUID x = (JenaUUID)other ;
+ return this.getMostSignificantBits() == x.getMostSignificantBits() &&
+ this.getLeastSignificantBits() == x.getLeastSignificantBits() ;
+ }
+
+
+
+
+ // ----------------------------------------------------
+ // Factory
+
+ static UUIDFactory factory = new UUID_V1_Gen() ;
+
+ public static void setFactory(UUIDFactory factory) {
+ JenaUUID.factory = factory ;
+ }
+
+ public static UUIDFactory getFactory() {
+ return factory ;
+ }
+
/** Create a UUID */
- public static JenaUUID generate() { return factory.generate() ; }
- public static void reset() { factory.reset() ; }
-
+ public static JenaUUID generate() {
+ return factory.generate() ;
+ }
+
+ public static void reset() {
+ factory.reset() ;
+ }
+
/** The nil UUID */
- public static JenaUUID nil() { return UUID_nil.getNil() ; }
- public static String strNil() { return UUID_nil.getNilString() ; }
- public boolean isNil() { return this.equals(nil()) ; } // Or
this == UUID_nil.nil because it's a singleton.
+ public static JenaUUID nil() {
+ return UUID_nil.getNil() ;
+ }
+
+ public static String strNil() {
+ return UUID_nil.getNilString() ;
+ }
+
+ public boolean isNil() {
+ return this.equals(nil()) ;
+ } // Or this == UUID_nil.nil because it's a singleton.
/** Recreate a UUID from string */
- public static JenaUUID parse(String s)
- {
- if ( s.equals(strNil()) )
+ public static JenaUUID parse(String s) {
+ if ( s.equals(strNil()) )
return nil() ;
-
+
// Canonical: this works in conjunction with .equals
s = s.toLowerCase(Locale.ENGLISH) ;
@@ -116,79 +164,93 @@ public abstract class JenaUUID
s = s.substring(5) ;
if ( s.length() != 36 )
- throw new FormatException("UUID string is not 36 chars long: it's
"+s.length()+" ["+s+"]") ;
+ throw new UUIDFormatException("UUID string is not 36 chars long:
it's " + s.length() + " [" + s + "]") ;
- if ( s.charAt(8) != '-' || s.charAt(13) != '-' || s.charAt(18) != '-'
|| s.charAt(23) != '-' )
- throw new FormatException("String does not have dashes in the
right places: "+s) ;
+ if ( s.charAt(8) != '-' || s.charAt(13) != '-' || s.charAt(18) != '-'
|| s.charAt(23) != '-' )
+ throw new UUIDFormatException("String does not have dashes in the
right places: " + s) ;
- // 00000000-0000-0000-0000-000000000000
- // ^ ^ ^ ^ ^
- // Byte: 0 4 6 8 10
- // Char: 0 9 14 19 24 including hyphens
-
+ // The UUID broken up into parts.
+ // 00000000-0000-0000-0000-000000000000
+ // ^ ^ ^ ^ ^
+ // Byte: 0 4 6 8 10
+ // Char: 0 9 14 19 24 including hyphens
int x = (int)Bits.unpack(s, 19, 23) ;
- int variant = (x>>>14) ;
+ int variant = (x >>> 14) ;
int version = (int)Bits.unpack(s, 14, 15) ;
-
- if ( variant == Var_Std )
- {
- switch (version)
- {
- case UUID_V1.version: return UUID_V1_Gen.parse$(s) ;
- case UUID_V4.version: return UUID_V4_Gen.parse$(s) ;
+
+ if ( variant == Var_Std ) {
+ switch (version) {
+ case UUID_V1.version :
+ return UUID_V1_Gen.parse$(s) ;
+ case UUID_V4.version :
+ return UUID_V4_Gen.parse$(s) ;
}
- LoggerFactory.getLogger(JenaUUID.class).warn(s+" : Unsupported
version: "+version) ;
- throw new UnsupportedOperationException("String specifies
unsupported UUID version: "+version) ;
+ LoggerFactory.getLogger(JenaUUID.class).warn(s + " : Unsupported
version: " + version) ;
+ throw new UnsupportedOperationException("String specifies
unsupported UUID version: " + version) ;
}
-
+
Logger log = LoggerFactory.getLogger(JenaUUID.class) ;
-
- switch (variant)
- {
- case Var_NCS: // NCS
- log.warn(s+" : Oh look! An NCS UUID ID. Call the museum.") ;
+
+ switch (variant) {
+ case Var_NCS : // NCS
+ log.warn(s + " : Oh look! An NCS UUID ID. Call the museum.") ;
break ;
- case Var_DCE: // DCE - should have been caught earlier.
- log.warn(s+" : Oh look! A DCE UUID ID - but we should have
already handled this") ;
+ case Var_DCE : // DCE - should have been caught earlier.
+ log.warn(s + " : Oh look! A DCE UUID ID - but we should have
already handled this") ;
break ;
- case Var_MS_GUID:
- log.warn(s+" : Microsoft UUID ID.") ;
+ case Var_MS_GUID :
+ log.warn(s + " : Microsoft UUID ID.") ;
break ;
- case Var_Reserved:
- log.warn(s+" : Reserved variant") ;
+ case Var_Reserved :
+ log.warn(s + " : Reserved variant") ;
break ;
- default:
- log.warn(s+" : Unknown variant: "+variant) ;
+ default :
+ log.warn(s + " : Unknown variant: " + variant) ;
break ;
}
- throw new UnsupportedOperationException("String specifies
unsupported UUID variant: "+variant) ;
+ throw new UnsupportedOperationException("String specifies unsupported
UUID variant: " + variant) ;
}
- // ----------------------------------------------------
- // Worker functions
+ public static String toString(JenaUUID uuid) {
+ return toString(uuid.getMostSignificantBits(),
uuid.getLeastSignificantBits()) ;
+ }
- static void toHex(StringBuffer sBuff, long value, int lenBytes)
- {
+ /** Format using two longs - assumed valid for an UUID of some kind */
+ public static String toString(long mostSignificantBits, long
leastSignificantBits) {
+ StringBuffer sb = new StringBuffer(36) ;
+ JenaUUID.toHex(sb, Bits.unpack(mostSignificantBits, 32, 64), 4) ;
+ sb.append('-') ;
+ JenaUUID.toHex(sb, Bits.unpack(mostSignificantBits, 16, 32), 2) ;
+ sb.append('-') ;
+ JenaUUID.toHex(sb, Bits.unpack(mostSignificantBits, 0, 16), 2) ;
+ sb.append('-') ;
+ JenaUUID.toHex(sb, Bits.unpack(leastSignificantBits, 48, 64), 2) ;
+ sb.append('-') ;
+ JenaUUID.toHex(sb, Bits.unpack(leastSignificantBits, 0, 48), 6) ;
+ return sb.toString() ;
+ }
+
+ // ----------------------------------------------------
+ // Worker functions
+
+ static void toHex(StringBuffer sBuff, long value, int lenBytes) {
// Insert in high-low order, by nibble
- for ( int i = 2*lenBytes-1 ; i >= 0 ; i-- )
- {
- int shift = 4*i ;
- int x = (int)(value>>>shift & 0xF) ;
+ for (int i = 2 * lenBytes - 1; i >= 0; i--) {
+ int shift = 4 * i ;
+ int x = (int)(value >>> shift & 0xF) ;
sBuff.append(Character.forDigit(x, 16)) ;
}
}
-
- static public class FormatException extends RuntimeException
- {
- public FormatException()
- {
- super();
- }
-
- public FormatException(String msg)
- {
- super(msg);
- }
- }
+
+ static public class UUIDFormatException extends RuntimeException
+ {
+ public UUIDFormatException() {
+ super() ;
+ }
+
+ public UUIDFormatException(String msg) {
+ super(msg) ;
+ }
+ }
}
Modified:
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/LibUUID.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/LibUUID.java?rev=1588976&r1=1588975&r2=1588976&view=diff
==============================================================================
--- jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/LibUUID.java
(original)
+++ jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/LibUUID.java
Mon Apr 21 20:26:12 2014
@@ -73,5 +73,4 @@ class LibUUID
seedInput.append(Long.toString(new Object().hashCode())) ;
return seedInput.toString().getBytes() ;
}
-
}
Modified:
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUIDFactory.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUIDFactory.java?rev=1588976&r1=1588975&r2=1588976&view=diff
==============================================================================
---
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUIDFactory.java
(original)
+++
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUIDFactory.java
Mon Apr 21 20:26:12 2014
@@ -21,6 +21,6 @@ package com.hp.hpl.jena.shared.uuid;
public interface UUIDFactory
{
public JenaUUID generate() ;
- public JenaUUID parse(String s) throws JenaUUID.FormatException ;
+ public JenaUUID parse(String s) throws JenaUUID.UUIDFormatException ;
public void reset() ;
}
Modified:
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1.java?rev=1588976&r1=1588975&r2=1588976&view=diff
==============================================================================
--- jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1.java
(original)
+++ jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1.java
Mon Apr 21 20:26:12 2014
@@ -93,23 +93,6 @@ public class UUID_V1 extends JenaUUID
return true ;
}
- @Override
- public String toString()
- { return UUID_V1_Gen.unparse(this) ; }
-
- // Time low - which includes the incremental count.
- @Override
- public int hashCode() { return (int) Bits.unpack(bitsMostSignificant, 32,
64) ; }
-
- @Override
- public boolean equals(Object other)
- {
- if ( ! ( other instanceof UUID_V1 ) )
- return false ;
- UUID_V1 x = (UUID_V1)other ;
- return this.bitsMostSignificant == x.bitsMostSignificant &&
this.bitsLeastSignificant == x.bitsLeastSignificant ;
- }
-
// Accessors
long getTimeHigh() { return Bits.unpack(bitsMostSignificant, 0, 12) ; }
// ( uuid.bitsUpper & UUID_V1_Gen.maskTimeHigh ) ;
Modified:
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1_Gen.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1_Gen.java?rev=1588976&r1=1588975&r2=1588976&view=diff
==============================================================================
---
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1_Gen.java
(original)
+++
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V1_Gen.java
Mon Apr 21 20:26:12 2014
@@ -16,23 +16,28 @@
* limitations under the License.
*/
-package com.hp.hpl.jena.shared.uuid;
+package com.hp.hpl.jena.shared.uuid ;
+import java.net.NetworkInterface ;
+import java.util.Enumeration ;
import java.util.Locale ;
-import com.hp.hpl.jena.shared.uuid.JenaUUID.FormatException ;
+import com.hp.hpl.jena.shared.uuid.JenaUUID.UUIDFormatException ;
/* RFC 4122 "A Universally Unique IDentifier (UUID) URN Namespace"
ftp://ftp.rfc-editor.org/in-notes/rfc4122.txt
Originally: http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
-
-Version 1, variant 2: timebased:
+ http://en.wikipedia.org/wiki/Universally_unique_identifier
+
+Version 1: hardware address and timebased:
60 bits of time
48 bits of nodeId
12 bits of clock sequence
2 bits variant
4 bits version
+
+
laid out as:
0 1 2 3
@@ -69,7 +74,7 @@ Note on variant: despite the javadoc doc
top 3 bits of octet 8. But the low bit is a "don't care" in this variant
and is used by the clock.
-*/
+ */
/* Java6: get MAC address:
NetworkInterface ni = NetworkInterface.getByInetAddress(address)
@@ -78,7 +83,6 @@ NetworkInterface ni = NetworkInterface.g
/** Generator for timebased UUIDs (version 1, variant 2)
*/
-
public class UUID_V1_Gen implements UUIDFactory
{
// Constants
@@ -99,7 +103,7 @@ public class UUID_V1_Gen implements UUID
static final long UUIDS_PER_TICK = 100 ;
long uuids_this_tick = UUIDS_PER_TICK+1 ; // Force reset first time.
- // Genrator initial state
+ // Generator initial state
int clockSeq = 0 ;
private static final int CLOCK_BITS = 8 ;
long node = 0 ;
@@ -108,15 +112,14 @@ public class UUID_V1_Gen implements UUID
private long lastTime = 0 ;
private long DELAY = 10 ; // Milliseconds
-
- public UUID_V1_Gen()
- { reset() ; }
+ public UUID_V1_Gen() {
+ reset() ;
+ }
/** (Re)set the network id (a random number) and the timstamp */
-
+
@Override
- public void reset()
- {
+ public void reset() {
setInitialState() ;
setTime() ;
}
@@ -125,12 +128,9 @@ public class UUID_V1_Gen implements UUID
public JenaUUID generate()
{ return generateV1() ; }
- public UUID_V1 generateV1()
- {
-
+ public UUID_V1 generateV1() {
long timestamp = 0 ;
- synchronized(this)
- {
+ synchronized (this) {
if ( uuids_this_tick >= UUIDS_PER_TICK )
setTime() ;
timestamp = gregorianTime + uuids_this_tick ;
@@ -153,55 +153,54 @@ public class UUID_V1_Gen implements UUID
hexOctet = <hexDigit><hexDigit>
*/
-
@Override
- public JenaUUID parse(String s) throws FormatException
- {
+ public JenaUUID parse(String s) throws UUIDFormatException {
s = s.toLowerCase(Locale.ENGLISH) ;
if ( s.length() != 36 )
- throw new FormatException("UUID string is not 36 chars long: it's
"+s.length()+" ["+s+"]") ;
+ throw new UUIDFormatException("UUID string is not 36 chars long:
it's " + s.length() + " [" + s + "]") ;
- if ( s.charAt(8) != '-' && s.charAt(13) != '-' && s.charAt(18) != '-'
&& s.charAt(23) != '-' )
- throw new FormatException("String does not have dashes in the
right places: "+s) ;
+ if ( s.charAt(8) != '-' && s.charAt(13) != '-' && s.charAt(18) != '-'
&& s.charAt(23) != '-' )
+ throw new UUIDFormatException("String does not have dashes in the
right places: " + s) ;
UUID_V1 u = parse$(s) ;
if ( u.getVersion() != versionHere )
- throw new FormatException("Wrong version (Expected:
"+versionHere+"Got: "+u.getVersion()+"): "+s) ;
+ throw new UUIDFormatException("Wrong version (Expected: " +
versionHere + "Got: " + u.getVersion() + "): " + s) ;
if ( u.getVariant() != variantHere )
- throw new FormatException("Wrong version (Expected:
"+variantHere+"Got: "+u.getVariant()+"): "+s) ;
+ throw new UUIDFormatException("Wrong version (Expected: " +
variantHere + "Got: " + u.getVariant() + "): " + s) ;
return u ;
}
-
- static UUID_V1 parse$(String s)
- {
+
+ static UUID_V1 parse$(String s) {
// The UUID broken up into parts.
// 00000000-0000-0000-0000-000000000000
// ^ ^ ^ ^ ^
// Byte: 0 4 6 8 10
// Char: 0 9 14 19 24 including hyphens
int x = (int)Bits.unpack(s, 19, 23) ;
- int variant = (x>>>14) ;
+ int variant = (x >>> 14) ;
int clockSeq = x & 0x3FFF ;
long timeHigh = Bits.unpack(s, 15, 18) ;
- long timeMid = Bits.unpack(s, 9, 13) ;
- long timeLow = Bits.unpack(s, 0, 8) ;
+ long timeMid = Bits.unpack(s, 9, 13) ;
+ long timeLow = Bits.unpack(s, 0, 8) ;
long node = Bits.unpack(s, 24, 36) ;
int version = (int)Bits.unpack(s, 14, 15) ;
return generate(version, variant, timeHigh, timeMid, timeLow,
clockSeq, node) ;
}
- // Easier to have parse and unparse code close together
- public static String unparse(UUID_V1 uuid)
- {
- int _variant = uuid.getVariant() ; // (int)((UUID_V1_Gen.maskVariant
& uuid.bitsLower)>>>62) ;
- int _version = uuid.getVersion() ; // (int)((UUID_V1_Gen.maskVersion
& uuid.bitsUpper)>>>12) ;
+ // See LibUUID.toString(JenaUUID)
+ // The code here works on the specific fields and is kept for reference
only.
+ private static String unparse(UUID_V1 uuid) {
+ int _variant = uuid.getVariant() ; // (int)((UUID_V1_Gen.maskVariant &
+ // uuid.bitsLower)>>>62) ;
+ int _version = uuid.getVersion() ; // (int)((UUID_V1_Gen.maskVersion &
+ // uuid.bitsUpper)>>>12) ;
long timeHigh = uuid.getTimeHigh() ;
- long timeMid = uuid.getTimeMid() ;
- long timeLow = uuid.getTimeLow() ;
+ long timeMid = uuid.getTimeMid() ;
+ long timeLow = uuid.getTimeLow() ;
long node = uuid.getNode() ;
long clockSeq = uuid.getClockSequence() ;
@@ -219,87 +218,101 @@ public class UUID_V1_Gen implements UUID
return sBuff.toString() ;
}
- private UUID_V1 generate(long timestamp)
- {
+ private UUID_V1 generate(long timestamp) {
return generate(versionHere, variantHere, timestamp, clockSeq, node) ;
}
// Testing.
- public static UUID_V1 generate(int version, int variant, long timestamp,
long clockSeq, long node)
- {
- long timeHigh = timestamp>>>(60-12) ; // Top 12 bits of 60
bit number.
- long timeMid = (timestamp>>>32)&0xFFFFL ; // 16 bits, bits 32-47
- long timeLow = timestamp & 0xFFFFFFFFL ; // Low 32 bits
+ public static UUID_V1 generate(int version, int variant, long timestamp,
long clockSeq, long node) {
+ long timeHigh = timestamp >>> (60 - 12) ; // Top 12 bits of 60
bit number.
+ long timeMid = (timestamp >>> 32) & 0xFFFFL ; // 16 bits, bits 32-47
+ long timeLow = timestamp & 0xFFFFFFFFL ; // Low 32 bits
return generate(version, variant, timeHigh, timeMid, timeLow,
clockSeq, node) ;
}
- private static UUID_V1 generate(int version, int variant, long timeHigh,
long timeMid, long timeLow, long clockSeq, long node)
- {
- long mostSigBits = (timeLow<<32) | (timeMid<<16) | (versionHere<<12) |
timeHigh ;
- long leastSigBits = (long)variantHere<<62 | (clockSeq << 48) | node ;
+ private static UUID_V1 generate(int version, int variant, long timeHigh,
long timeMid, long timeLow, long clockSeq, long node) {
+ long mostSigBits = (timeLow << 32) | (timeMid << 16) | (versionHere <<
12) | timeHigh ;
+ long leastSigBits = (long)variantHere << 62 | (clockSeq << 48) | node ;
return new UUID_V1(mostSigBits, leastSigBits) ;
}
- private void setTime()
- {
+ private void setTime() {
long time = 0 ;
-
+
// Wait for a clock tick.
synchronized (this) {
if ( lastTime == 0 )
- lastTime = System.currentTimeMillis();
-
- boolean done = false;
+ lastTime = System.currentTimeMillis() ;
+
+ boolean done = false ;
while (!done) {
- time = System.currentTimeMillis();
- if (time < lastTime+DELAY) {
+ time = System.currentTimeMillis() ;
+ if ( time < lastTime + DELAY ) {
// pause for a while to wait for time to change
try {
- Thread.sleep(DELAY);
+ Thread.sleep(DELAY) ;
} catch (java.lang.InterruptedException e) {} // ignore
exception
- continue;
+ continue ;
} else {
- done = true;
+ done = true ;
}
}
}
-
- // We claim this tick just passed! 1 UUID per 100ns so ...
- //UUIDS_PER_TICK = (time-lastTime)*10 ;
- lastTime = time;
+
+ // We claim this tick just passed! 1 UUID per 100ns so ...
+ // UUIDS_PER_TICK = (time-lastTime)*10 ;
+ lastTime = time ;
uuids_this_tick = 0 ;
-
+
// Convert to the UUID base time (00:00:00.00, 15 October 1582)
// That's the date of the Gregorian calendar reforms
// See the text quoted for the number.
// Java base time is is January 1, 1970.
-
- gregorianTime = time*10 + 0x01B21DD213814000L ;
+
+ gregorianTime = time * 10 + 0x01B21DD213814000L ;
}
- private void setInitialState()
- {
-// try {
-// // And in Java7 : NetworkInterface.getByIndex(0)
-// // Java 6:
-// InetAddress address = InetAddress.getLocalHost();
-// NetworkInterface ni = NetworkInterface.getByInetAddress(address)
;
-// byte[] b = ni.getHardwareAddress() ;
-// } catch (Exception ex) {}
-
+ private void setInitialState() {
long random = LibUUID.makeRandom().nextLong() ;
+ clockSeq = 0 ;
+ if ( CLOCK_BITS != 0 )
+ clockSeq = (int)Bits.unpack(random, 48, (48 + CLOCK_BITS)) ;
+
+ // Get the MAC address of an interface.
+ // The loopback I/F does not have a MAC address.
+ try {
+ Enumeration<NetworkInterface> en =
NetworkInterface.getNetworkInterfaces() ;
+
+ byte[] hwAddr = null ;
+ while(en.hasMoreElements()) {
+ NetworkInterface ni = en.nextElement() ;
+ if ( ni == null || ni.isLoopback() || ni.isPointToPoint() ||
ni.isVirtual() )
+ continue ;
+ hwAddr = ni.getHardwareAddress() ;
+ if ( hwAddr != null )
+ break ;
+ }
+ if ( hwAddr != null && hwAddr.length > 4 ) { // Length is a sanity
check.
+ node = 0 ;
+ for ( byte bv : hwAddr ) {
+ node = node << 8 | bv ;
+ }
+ return ;
+ }
+ } catch (Exception ex) {
+
+ } // Failed in some way. Fallback.
- node = Bits.unpack(random, 0, 47); // Low 48bits, except groups
address bit
- node = Bits.set(node, 47) ; // Set group address bit
+
+ node = Bits.unpack(random, 0, 47) ; // Low 48bits, except groups
address bit
+ node = Bits.set(node, 47) ; // Set group address bit
+
// Can also set the clock sequence number to increase the randomness.
- // Use up to 13 bits for the clock (actually, it's 14 bits as
+ // Use up to 13 bits for the clock (actually, it's 14 bits as
// strays into the variant).
// We use less to get a characteristic "-80??-" in the string
-
- clockSeq = 0 ;
- if ( CLOCK_BITS != 0 )
- clockSeq = (int)Bits.unpack(random, 48, (48+CLOCK_BITS)) ;
+
}
}
Modified:
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4.java?rev=1588976&r1=1588975&r2=1588976&view=diff
==============================================================================
--- jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4.java
(original)
+++ jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4.java
Mon Apr 21 20:26:12 2014
@@ -58,28 +58,7 @@ public class UUID_V4 extends JenaUUID
return true ;
}
- @Override
- public String toString()
- {
- return UUID_V4_Gen.unparse(this) ;
- }
-
- @Override
- public int hashCode() { return (int) Bits.unpack(bitsMostSignificant, 32,
64) ; }
-
- @Override
- public boolean equals(Object other)
- {
- if ( ! (other instanceof UUID_V4 ) )
- return false ;
- UUID_V4 u = (UUID_V4)other ;
- return this.bitsMostSignificant == u.bitsMostSignificant &&
- this.bitsLeastSignificant == u.bitsLeastSignificant ;
- }
-
static boolean initialized = false ;
-
-
static public void init()
{
if ( !initialized )
Modified:
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4_Gen.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4_Gen.java?rev=1588976&r1=1588975&r2=1588976&view=diff
==============================================================================
---
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4_Gen.java
(original)
+++
jena/trunk/jena-core/src/main/java/com/hp/hpl/jena/shared/uuid/UUID_V4_Gen.java
Mon Apr 21 20:26:12 2014
@@ -20,7 +20,7 @@ package com.hp.hpl.jena.shared.uuid;
import java.util.* ;
-import com.hp.hpl.jena.shared.uuid.JenaUUID.FormatException;
+import com.hp.hpl.jena.shared.uuid.JenaUUID.UUIDFormatException;
/** Generator for for random number based UUIDs (version 2, variant 4)
@@ -60,16 +60,16 @@ public class UUID_V4_Gen implements UUID
s = s.toLowerCase(Locale.ENGLISH) ;
if ( s.length() != 36 )
- throw new FormatException("UUID string is not 36 chars long: it's
"+s.length()+" ["+s+"]") ;
+ throw new UUIDFormatException("UUID string is not 36 chars long:
it's "+s.length()+" ["+s+"]") ;
if ( s.charAt(8) != '-' && s.charAt(13) != '-' && s.charAt(18) != '-'
&& s.charAt(23) != '-' )
- throw new FormatException("String does not have dashes in the
right places: "+s) ;
+ throw new UUIDFormatException("String does not have dashes in the
right places: "+s) ;
UUID_V4 u = parse$(s) ;
if ( u.getVersion() != versionHere )
- throw new FormatException("Wrong version (Expected:
"+versionHere+"Got: "+u.getVersion()+"): "+s) ;
+ throw new UUIDFormatException("Wrong version (Expected:
"+versionHere+"Got: "+u.getVersion()+"): "+s) ;
if ( u.getVariant() != variantHere )
- throw new FormatException("Wrong version (Expected:
"+variantHere+"Got: "+u.getVariant()+"): "+s) ;
+ throw new UUIDFormatException("Wrong version (Expected:
"+variantHere+"Got: "+u.getVariant()+"): "+s) ;
return u ;
}
@@ -91,21 +91,6 @@ public class UUID_V4_Gen implements UUID
return new UUID_V4(mostSigBits, leastSigBits) ;
}
- public static String unparse(UUID_V4 uuid)
- {
- StringBuffer sb = new StringBuffer(36) ;
- JenaUUID.toHex(sb, Bits.unpack(uuid.bitsMostSignificant, 32, 64), 4) ;
- sb.append('-') ;
- JenaUUID.toHex(sb, Bits.unpack(uuid.bitsMostSignificant, 16, 32), 2) ;
- sb.append('-') ;
- JenaUUID.toHex(sb, Bits.unpack(uuid.bitsMostSignificant, 0, 16), 2) ;
- sb.append('-') ;
- JenaUUID.toHex(sb, Bits.unpack(uuid.bitsLeastSignificant, 48, 64), 2) ;
- sb.append('-') ;
- JenaUUID.toHex(sb, Bits.unpack(uuid.bitsLeastSignificant, 0, 48), 6) ;
- return sb.toString() ;
- }
-
private void init()
{
if ( random == null )