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:
010: import com.hp.hpl.jena.shared.uuid.JenaUUID.FormatException;
011:
012: /** Generator for for random number based UUIDs (version 2, variant 4)
013: * @author Andy Seaborne
014: * @version $Id: UUID_V4_Gen.java,v 1.3 2008/01/02 12:06:07 andy_seaborne Exp $
015: */
016: public class UUID_V4_Gen implements UUIDFactory {
017: // Implementation should be compatible with JXTA UUIDs
018:
019: static final int versionHere = 4; // Version 4: random number
020: static final int variantHere = JenaUUID.Var_Std;
021:
022: Random random = null;
023:
024: public UUID_V4_Gen() {
025: }
026:
027: public JenaUUID generate() {
028: return generateV4();
029: }
030:
031: public UUID_V4 generateV4() {
032: init();
033: long mostSigBits = random.nextLong();
034: long leastSigBits = random.nextLong();
035: mostSigBits = Bits.pack(mostSigBits, versionHere, 12, 16);
036: leastSigBits = Bits.pack(leastSigBits, variantHere, 62, 64);
037: return new UUID_V4(mostSigBits, leastSigBits);
038: }
039:
040: public JenaUUID parse(String s) {
041: return parseV4(s);
042: }
043:
044: public UUID_V4 parseV4(String s) {
045: s = s.toLowerCase();
046:
047: if (s.length() != 36)
048: throw new FormatException(
049: "UUID string is not 36 chars long: it's "
050: + s.length() + " [" + s + "]");
051:
052: if (s.charAt(8) != '-' && s.charAt(13) != '-'
053: && s.charAt(18) != '-' && s.charAt(23) != '-')
054: throw new FormatException(
055: "String does not have dashes in the right places: "
056: + s);
057:
058: UUID_V4 u = parse$(s);
059: if (u.getVersion() != versionHere)
060: throw new FormatException("Wrong version (Expected: "
061: + versionHere + "Got: " + u.getVersion() + "): "
062: + s);
063: if (u.getVariant() != variantHere)
064: throw new FormatException("Wrong version (Expected: "
065: + variantHere + "Got: " + u.getVariant() + "): "
066: + s);
067: return u;
068: }
069:
070: static UUID_V4 parse$(String s) {
071: // The UUID broken up into parts.
072: // 00000000-0000-0000-0000-000000000000
073: // ^ ^ ^ ^ ^
074: // Byte: 0 4 6 8 10
075: // Char: 0 9 14 19 24 including hyphens
076: long mostSigBits = Bits.unpack(s, 0, 8);
077: // Skip -
078: mostSigBits = mostSigBits << 16 | Bits.unpack(s, 9, 13);
079: // Skip -
080: mostSigBits = mostSigBits << 16 | Bits.unpack(s, 14, 18);
081:
082: long leastSigBits = Bits.unpack(s, 19, 23);
083: leastSigBits = leastSigBits << 48 | Bits.unpack(s, 24, 36);
084: return new UUID_V4(mostSigBits, leastSigBits);
085: }
086:
087: public static String unparse(UUID_V4 uuid) {
088: StringBuffer sb = new StringBuffer(36);
089: JenaUUID.toHex(sb, Bits
090: .unpack(uuid.bitsMostSignificant, 32, 64), 4);
091: sb.append('-');
092: JenaUUID.toHex(sb, Bits
093: .unpack(uuid.bitsMostSignificant, 16, 32), 2);
094: sb.append('-');
095: JenaUUID.toHex(sb,
096: Bits.unpack(uuid.bitsMostSignificant, 0, 16), 2);
097: sb.append('-');
098: JenaUUID.toHex(sb, Bits.unpack(uuid.bitsLeastSignificant, 48,
099: 64), 2);
100: sb.append('-');
101: JenaUUID.toHex(sb, Bits
102: .unpack(uuid.bitsLeastSignificant, 0, 48), 6);
103: return sb.toString();
104: }
105:
106: private void init() {
107: if (random == null)
108: reset();
109: }
110:
111: public void reset() {
112: random = LibUUID.makeRandom();
113: }
114: }
115:
116: /*
117: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
118: * All rights reserved.
119: *
120: * Redistribution and use in source and binary forms, with or without
121: * modification, are permitted provided that the following conditions
122: * are met:
123: * 1. Redistributions of source code must retain the above copyright
124: * notice, this list of conditions and the following disclaimer.
125: * 2. Redistributions in binary form must reproduce the above copyright
126: * notice, this list of conditions and the following disclaimer in the
127: * documentation and/or other materials provided with the distribution.
128: * 3. The name of the author may not be used to endorse or promote products
129: * derived from this software without specific prior written permission.
130: *
131: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
132: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
133: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
134: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
135: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
136: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
137: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
138: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
139: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
140: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
141: */
|