Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.client.hp.hpl.jena.shared.uuid; //import java.util.* ; import com.client.hp.hpl.jena.shared.uuid.Bits; import com.client.hp.hpl.jena.shared.uuid.JenaUUID; import com.client.hp.hpl.jena.shared.uuid.LibUUID; import com.client.hp.hpl.jena.shared.uuid.UUIDFactory; import com.client.hp.hpl.jena.shared.uuid.UUID_V4; import com.client.hp.hpl.jena.shared.uuid.JenaUUID.FormatException; import com.google.gwt.user.client.Random; /** Generator for for random number based UUIDs (version 2, variant 4) * @author Andy Seaborne * @version $Id: UUID_V4_Gen.java,v 1.1 2009-06-29 08:55:40 castagna Exp $ */ public class UUID_V4_Gen implements UUIDFactory { // Implementation should be compatible with JXTA UUIDs static final int versionHere = 4; // Version 4: random number static final int variantHere = JenaUUID.Var_Std; int random = 0; public UUID_V4_Gen() { } @Override public JenaUUID generate() { return generateV4(); } public UUID_V4 generateV4() { init(); Double mostSigBits = Random.nextDouble(); Double leastSigBits = Random.nextDouble(); long convertedMostSigBits = mostSigBits.longValue(); long convertedLeastSigBits = leastSigBits.longValue(); convertedMostSigBits = Bits.pack(convertedMostSigBits, versionHere, 12, 16); convertedLeastSigBits = Bits.pack(convertedLeastSigBits, variantHere, 62, 64); return new UUID_V4(convertedMostSigBits, convertedLeastSigBits); } @Override public JenaUUID parse(String s) { return parseV4(s); } public UUID_V4 parseV4(String s) { s = s.toLowerCase(); if (s.length() != 36) throw new FormatException("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); UUID_V4 u = parse$(s); if (u.getVersion() != versionHere) throw new FormatException( "Wrong version (Expected: " + versionHere + "Got: " + u.getVersion() + "): " + s); if (u.getVariant() != variantHere) throw new FormatException( "Wrong version (Expected: " + variantHere + "Got: " + u.getVariant() + "): " + s); return u; } static UUID_V4 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 long mostSigBits = Bits.unpack(s, 0, 8); // Skip - mostSigBits = mostSigBits << 16 | Bits.unpack(s, 9, 13); // Skip - mostSigBits = mostSigBits << 16 | Bits.unpack(s, 14, 18); long leastSigBits = Bits.unpack(s, 19, 23); leastSigBits = leastSigBits << 48 | Bits.unpack(s, 24, 36); 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 == 0) reset(); } @Override public void reset() { random = LibUUID.makeRandom(); } }