001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.shared.uuid;
007:
008: import java.util.*;
009: import java.security.*;
010:
011: /** Random number based UUIDs
012: * @author Andy Seaborne
013: * @version $Id: UUID_V4.java,v 1.7 2008/01/02 12:06:07 andy_seaborne Exp $
014: */
015: public class UUID_V4 extends JenaUUID {
016: // Implementation should be compatible with JXTA UUIDs
017: // Constants
018: public static final int version = 4; // Version 4: random number
019: public static final int variant = JenaUUID.Var_Std;
020:
021: static Random random = null;
022:
023: long bitsMostSignificant = 0; // Bytes 0 to 7
024: long bitsLeastSignificant = 0; // Bytes 8 to 15
025:
026: UUID_V4(long mostSigBits, long leastSigBits) {
027: if (!check(mostSigBits, leastSigBits))
028: throw new IllegalArgumentException("Funny bits");
029: this .bitsMostSignificant = mostSigBits;
030: this .bitsLeastSignificant = leastSigBits;
031: }
032:
033: public long getMostSignificantBits() {
034: return bitsMostSignificant;
035: }
036:
037: public long getLeastSignificantBits() {
038: return bitsLeastSignificant;
039: }
040:
041: private boolean check(long mostSigBits, long leastSigBits) {
042: int _variant = _getVariant(mostSigBits, leastSigBits);
043: int _version = _getVersion(mostSigBits, leastSigBits);
044:
045: if (_variant != variant)
046: return false;
047: if (_version != version)
048: return false;
049: return true;
050: }
051:
052: public String toString() {
053: return UUID_V4_Gen.unparse(this );
054: }
055:
056: public int hashCode() {
057: return (int) Bits.unpack(bitsMostSignificant, 32, 64);
058: }
059:
060: public boolean equals(Object other) {
061: if (!(other instanceof UUID_V4))
062: return false;
063: UUID_V4 u = (UUID_V4) other;
064: return this .bitsMostSignificant == u.bitsMostSignificant
065: && this .bitsLeastSignificant == u.bitsLeastSignificant;
066: }
067:
068: static boolean initialized = false;
069:
070: static public void init() {
071: if (!initialized) {
072: reset();
073: initialized = true;
074: }
075: }
076:
077: static public void uninit() {
078: initialized = false;
079: }
080:
081: public static void reset() {
082: random = new SecureRandom(); // SecureRandom.getInstance("SHA1PRNG");
083:
084: byte[] seed = LibUUID.makeSeed();
085:
086: if (random == null) {
087: // dreadful.
088: random = new Random();
089: long l = 0;
090: for (int i = 0; i < 8; i++)
091: l = (l << 8) | (seed[i] & 0xff);
092: random = new Random();
093: random.setSeed(l);
094: }
095: }
096:
097: public int getVersion() {
098: return _getVersion(bitsMostSignificant, bitsLeastSignificant);
099: }
100:
101: public int getVariant() {
102: return _getVariant(bitsMostSignificant, bitsLeastSignificant);
103: }
104: }
105:
106: /*
107: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
108: * All rights reserved.
109: *
110: * Redistribution and use in source and binary forms, with or without
111: * modification, are permitted provided that the following conditions
112: * are met:
113: * 1. Redistributions of source code must retain the above copyright
114: * notice, this list of conditions and the following disclaimer.
115: * 2. Redistributions in binary form must reproduce the above copyright
116: * notice, this list of conditions and the following disclaimer in the
117: * documentation and/or other materials provided with the distribution.
118: * 3. The name of the author may not be used to endorse or promote products
119: * derived from this software without specific prior written permission.
120: *
121: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
122: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
123: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
124: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
125: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
126: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
127: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
128: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
129: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
130: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
131: */
|