private static synchronized UUIDTimer sharedTimer() { if (_sharedTimer == null) { try { _sharedTimer = new UUIDTimer(new java.util.Random(System.currentTimeMillis()), null); } catch (IOException e) { throw new IllegalArgumentException("Failed to create UUIDTimer with specified synchronizer: "+e.getMessage(), e); } } return _sharedTimer; } }
protected final void getAndSetTimestamp(byte[] uuidBytes) { long timestamp = getTimestamp(); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_SEQUENCE] = (byte) _clockSequence; uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_SEQUENCE+1] = (byte) (_clockSequence >> 8); // Time fields aren't nicely split across the UUID, so can't just // linearly dump the stamp: int clockHi = (int) (timestamp >>> 32); int clockLo = (int) timestamp; uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_HI] = (byte) (clockHi >>> 24); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_HI+1] = (byte) (clockHi >>> 16); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_MID] = (byte) (clockHi >>> 8); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_MID+1] = (byte) clockHi; uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_LO] = (byte) (clockLo >>> 24); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_LO+1] = (byte) (clockLo >>> 16); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_LO+2] = (byte) (clockLo >>> 8); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_LO+3] = (byte) clockLo; }
public UUIDTimer(Random rnd, TimestampSynchronizer sync) throws IOException { _random = rnd; _syncer = sync; initCounters(rnd); _lastSystemTimestamp = 0L; // This may get overwritten by the synchronizer _lastUsedTimestamp = 0L; /* Ok, now; synchronizer can tell us what is the first timestamp * value that definitely was NOT used by the previous incarnation. * This can serve as the last used time stamp, assuming it is not * less than value we are using now. */ if (sync != null) { long lastSaved = sync.initialize(); if (lastSaved > _lastUsedTimestamp) { _lastUsedTimestamp = lastSaved; } } /* Also, we need to make sure there are now no safe values (since * synchronizer is not yet requested to allocate any): */ _firstUnsafeTimestamp = 0L; // ie. will always trigger sync.update() }
initCounters(_random); slowDown(origTime, actDiff);
/** * @param addr Hardware address (802.1) to use for generating * spatially unique part of UUID. If system has more than one NIC, */ public TimeBasedGenerator(EthernetAddress ethAddr, UUIDTimer timer) { byte[] uuidBytes = new byte[16]; if (ethAddr == null) { ethAddr = EthernetAddress.constructMulticastAddress(); } // initialize baseline with MAC address info _ethernetAddress = ethAddr; _ethernetAddress.toByteArray(uuidBytes, 10); // and add clock sequence int clockSeq = timer.getClockSequence(); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_SEQUENCE] = (byte) clockSeq; uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_SEQUENCE+1] = (byte) (clockSeq >> 8); long l2 = UUIDUtil.gatherLong(uuidBytes, 8); _uuidL2 = UUIDUtil.initUUIDSecondLong(l2); _timer = timer; }
initCounters(_random); slowDown(origTime, actDiff);
/** * @param ethAddr Hardware address (802.1) to use for generating * spatially unique part of UUID. If system has more than one NIC, */ public TimeBasedGenerator(EthernetAddress ethAddr, UUIDTimer timer) { byte[] uuidBytes = new byte[16]; if (ethAddr == null) { ethAddr = EthernetAddress.constructMulticastAddress(); } // initialize baseline with MAC address info _ethernetAddress = ethAddr; _ethernetAddress.toByteArray(uuidBytes, 10); // and add clock sequence int clockSeq = timer.getClockSequence(); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_SEQUENCE] = (byte) (clockSeq >> 8); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_SEQUENCE+1] = (byte) clockSeq; long l2 = UUIDUtil.gatherLong(uuidBytes, 8); _uuidL2 = UUIDUtil.initUUIDSecondLong(l2); _timer = timer; }
/** * Factory method for constructing UUID generator that generates UUID using * variant 1 (time+location based), using specified Ethernet address * as the location part of UUID, and specified synchronizer (which may add * additional restrictions to guarantee system-wide uniqueness). * * @see com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer */ public static TimeBasedGenerator timeBasedGenerator(EthernetAddress ethernetAddress, TimestampSynchronizer sync) { UUIDTimer timer; try { timer = new UUIDTimer(new Random(System.currentTimeMillis()), sync); } catch (IOException e) { throw new IllegalArgumentException("Failed to create UUIDTimer with specified synchronizer: "+e.getMessage(), e); } return timeBasedGenerator(ethernetAddress, timer); }
@Override public UUID generate() { final long rawTimestamp = _timer.getTimestamp(); // Time field components are kind of shuffled, need to slice: int clockHi = (int) (rawTimestamp >>> 32); int clockLo = (int) rawTimestamp; // and dice int midhi = (clockHi << 16) | (clockHi >>> 16); // need to squeeze in type (4 MSBs in byte 6, clock hi) midhi &= ~0xF000; // remove high nibble of 6th byte midhi |= 0x1000; // type 1 long midhiL = (long) midhi; midhiL = ((midhiL << 32) >>> 32); // to get rid of sign extension // and reconstruct long l1 = (((long) clockLo) << 32) | midhiL; // last detail: must force 2 MSB to be '10' return new UUID(l1, _uuidL2); } }
public UUIDTimer(Random rnd, TimestampSynchronizer sync) throws IOException { _random = rnd; _syncer = sync; initCounters(rnd); _lastSystemTimestamp = 0L; // This may get overwritten by the synchronizer _lastUsedTimestamp = 0L; /* Ok, now; synchronizer can tell us what is the first timestamp * value that definitely was NOT used by the previous incarnation. * This can serve as the last used time stamp, assuming it is not * less than value we are using now. */ if (sync != null) { long lastSaved = sync.initialize(); if (lastSaved > _lastUsedTimestamp) { _lastUsedTimestamp = lastSaved; } } /* Also, we need to make sure there are now no safe values (since * synchronizer is not yet requested to allocate any): */ _firstUnsafeTimestamp = 0L; // ie. will always trigger sync.update() }
private static synchronized UUIDTimer sharedTimer() { if (_sharedTimer == null) { try { _sharedTimer = new UUIDTimer(new java.util.Random(System.currentTimeMillis()), null); } catch (IOException e) { throw new IllegalArgumentException("Failed to create UUIDTimer with specified synchronizer: "+e.getMessage(), e); } } return _sharedTimer; } }
protected final void getAndSetTimestamp(byte[] uuidBytes) { long timestamp = getTimestamp(); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_SEQUENCE] = (byte) _clockSequence; uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_SEQUENCE+1] = (byte) (_clockSequence >> 8); // Time fields aren't nicely split across the UUID, so can't just // linearly dump the stamp: int clockHi = (int) (timestamp >>> 32); int clockLo = (int) timestamp; uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_HI] = (byte) (clockHi >>> 24); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_HI+1] = (byte) (clockHi >>> 16); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_MID] = (byte) (clockHi >>> 8); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_MID+1] = (byte) clockHi; uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_LO] = (byte) (clockLo >>> 24); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_LO+1] = (byte) (clockLo >>> 16); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_LO+2] = (byte) (clockLo >>> 8); uuidBytes[UUIDUtil.BYTE_OFFSET_CLOCK_LO+3] = (byte) clockLo; }
/** * Factory method for constructing UUID generator that generates UUID using * variant 1 (time+location based), using specified Ethernet address * as the location part of UUID, and specified synchronizer (which may add * additional restrictions to guarantee system-wide uniqueness). * * @param ethernetAddress (optional) MAC address to use; if null, a transient * random address is generated. * * @see com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer */ public static TimeBasedGenerator timeBasedGenerator(EthernetAddress ethernetAddress, TimestampSynchronizer sync) { UUIDTimer timer; try { timer = new UUIDTimer(new Random(System.currentTimeMillis()), sync); } catch (IOException e) { throw new IllegalArgumentException("Failed to create UUIDTimer with specified synchronizer: "+e.getMessage(), e); } return timeBasedGenerator(ethernetAddress, timer); }
@Override public UUID generate() { final long rawTimestamp = _timer.getTimestamp(); // Time field components are kind of shuffled, need to slice: int clockHi = (int) (rawTimestamp >>> 32); int clockLo = (int) rawTimestamp; // and dice int midhi = (clockHi << 16) | (clockHi >>> 16); // need to squeeze in type (4 MSBs in byte 6, clock hi) midhi &= ~0xF000; // remove high nibble of 6th byte midhi |= 0x1000; // type 1 long midhiL = (long) midhi; midhiL = ((midhiL << 32) >>> 32); // to get rid of sign extension // and reconstruct long l1 = (((long) clockLo) << 32) | midhiL; // last detail: must force 2 MSB to be '10' return new UUID(l1, _uuidL2); } }