1 21 22 package org.apache.derby.impl.services.uuid; 23 24 import org.apache.derby.iapi.services.monitor.Monitor; 25 import org.apache.derby.catalog.UUID; 26 import org.apache.derby.iapi.services.uuid.UUIDFactory; 27 28 57 58 public final class BasicUUIDFactory 59 implements UUIDFactory 60 { 61 64 65 private long majorId; private long timemillis; 67 68 public BasicUUIDFactory() { 69 Object env = Monitor.getMonitor().getEnvironment(); 70 if (env != null) { 71 String s = env.toString(); 72 if (s != null) 73 env = s; 74 75 majorId = ((long) env.hashCode()); 76 77 78 } else { 79 majorId = Runtime.getRuntime().freeMemory(); 80 } 81 82 majorId &= 0x0000ffffffffffffL; 83 resetCounters(); 84 } 85 86 87 111 private static final long MODULUS = ( 1L << 32 ); 112 private static final long MULTIPLIER = ( ( 1L << 14 ) + 1 ); 113 private static final long STEP = ( ( 1L << 27 ) + 1 ); 114 private static final long INITIAL_VALUE = ( 2551218188L ); 115 116 private long currentValue; 117 118 121 122 126 public synchronized UUID createUUID() 127 { 128 long cv = currentValue = ( ( MULTIPLIER * currentValue ) + STEP ) % MODULUS; 129 if ( cv == INITIAL_VALUE ) { bumpMajor(); } 130 int sequence = (int) cv; 131 132 return new BasicUUID(majorId, timemillis, sequence); 133 } 134 135 139 public UUID recreateUUID(String uuidstring) 140 { 141 return new BasicUUID(uuidstring); 142 } 143 144 147 public UUID recreateUUID(byte[] b) 148 { 149 return new BasicUUID(b); 150 } 151 152 private void bumpMajor() { 153 154 majorId = (majorId + 1L) & 0x0000ffffffffffffL; 156 if (majorId == 0L) 157 resetCounters(); 158 159 } 160 private void resetCounters() 161 { 162 timemillis = System.currentTimeMillis(); 163 currentValue = INITIAL_VALUE; 164 } 165 } 166 167 | Popular Tags |