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: /*
009: Version 1:
010: 60 bits of time
011: 48 bits of nodeId
012: 12 bits of clock sequence
013: 2 bits variant
014: 4 bits version
015:
016: 0 1 2 3
017: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
018: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
019: | time_low | 8 hex digits
020: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
021: | time_mid | time_hi_and_version | 4-4
022: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
023: |clk_seq_hi_res | clk_seq_low | node (0-1) | 4-
024: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
025: | node (2-5) | 12
026: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
027: */
028:
029: // http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
030: /* java.util.UUID
031: 0xFFFFFFFF00000000 time_low
032: 0x00000000FFFF0000 time_mid
033: 0x000000000000F000 version
034: 0x0000000000000FFF time_hi
035:
036: The least significant long consists of the following unsigned fields:
037:
038: 0xC000000000000000 variant
039: 0x3FFF000000000000 clock_seq
040: 0x0000FFFFFFFFFFFF node
041: */
042:
043: /** Timebased UUIDs.
044: *
045: * @author Andy Seaborne
046: * @version $Id: UUID_V1.java,v 1.7 2008/01/02 12:06:07 andy_seaborne Exp $
047: */
048:
049: public class UUID_V1 extends JenaUUID {
050: // Constants
051: public static final int version = 1; // Version 1: time-based.
052: public static final int variant = JenaUUID.Var_Std; // DCE varient
053:
054: // The only state-per-object
055: long bitsMostSignificant;
056: long bitsLeastSignificant;
057:
058: UUID_V1(long mostSigBits, long leastSigBits) {
059: if (!check(mostSigBits, leastSigBits)) {
060: check(mostSigBits, leastSigBits);
061: throw new IllegalArgumentException("Funny bits");
062: }
063: bitsMostSignificant = mostSigBits;
064: bitsLeastSignificant = leastSigBits;
065: }
066:
067: public long getMostSignificantBits() {
068: return bitsMostSignificant;
069: }
070:
071: public long getLeastSignificantBits() {
072: return bitsLeastSignificant;
073: }
074:
075: private boolean check(long mostSigBits, long leastSigBits) {
076: int _variant = _getVariant(mostSigBits, leastSigBits);
077: int _version = _getVersion(mostSigBits, leastSigBits);
078:
079: if (_variant != variant)
080: return false;
081: if (_version != version)
082: return false;
083: return true;
084: }
085:
086: public String toString() {
087: return UUID_V1_Gen.unparse(this );
088: }
089:
090: // Time low - which includes the incremental count.
091: public int hashCode() {
092: return (int) Bits.unpack(bitsMostSignificant, 32, 64);
093: }
094:
095: public boolean equals(Object other) {
096: if (!(other instanceof UUID_V1))
097: return false;
098: UUID_V1 x = (UUID_V1) other;
099: return this .bitsMostSignificant == x.bitsMostSignificant
100: && this .bitsLeastSignificant == x.bitsLeastSignificant;
101: }
102:
103: // Accessors
104:
105: long getTimeHigh() {
106: return Bits.unpack(bitsMostSignificant, 0, 12);
107: } // ( uuid.bitsUpper & UUID_V1_Gen.maskTimeHigh ) ;
108:
109: long getTimeMid() {
110: return Bits.unpack(bitsMostSignificant, 16, 32);
111: } // ( uuid.bitsUpper & UUID_V1_Gen.maskTimeMid ) >>> 16 ;
112:
113: long getTimeLow() {
114: return Bits.unpack(bitsMostSignificant, 32, 64);
115: } // ( uuid.bitsUpper & UUID_V1_Gen.maskTimeLow ) >>> 32;
116:
117: public long getTimestamp() {
118: return getTimeLow() | getTimeMid() << 32 | getTimeHigh() << 48;
119: }
120:
121: public long getClockSequence() {
122: return Bits.unpack(bitsLeastSignificant, 48, 62);
123: }
124:
125: public long getNode() {
126: return Bits.unpack(bitsLeastSignificant, 0, 48);
127: }
128:
129: public int getVersion() {
130: return super ._getVersion(bitsMostSignificant,
131: bitsLeastSignificant);
132: }
133:
134: public int getVariant() {
135: return super ._getVariant(bitsMostSignificant,
136: bitsLeastSignificant);
137: }
138: }
139:
140: /*
141: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
142: * All rights reserved.
143: *
144: * Redistribution and use in source and binary forms, with or without
145: * modification, are permitted provided that the following conditions
146: * are met:
147: * 1. Redistributions of source code must retain the above copyright
148: * notice, this list of conditions and the following disclaimer.
149: * 2. Redistributions in binary form must reproduce the above copyright
150: * notice, this list of conditions and the following disclaimer in the
151: * documentation and/or other materials provided with the distribution.
152: * 3. The name of the author may not be used to endorse or promote products
153: * derived from this software without specific prior written permission.
154: *
155: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
156: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
158: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
159: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
160: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
161: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
162: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
163: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
164: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
165: */
|