public static boolean isTimeBased( UUID uuid ) { if ( uuid == null ) { return false; } return uuid.version() == 1; }
public static boolean isTimeBased( UUID uuid ) { if ( uuid == null ) { return false; } return uuid.version() == 1; }
public static Boolean isTimeUuid(UUID uuid) { return uuid.version() == 1; }
/** * Verify the version is not null and is a type 1 version */ public static void verifyTimeUuid( final UUID uuid, final String fieldName ) { Preconditions.checkNotNull( uuid, "%s is required to be set for an update operation", fieldName ); Preconditions.checkArgument( uuid.version() == UUID_VERSION, "%s uuid must be version 1", fieldName ); }
private Date UUIDTimeStampToDate(UUID uuid) { long timeStamp = 0L; // The UUID is supposed to be time based so this should always be '1' // but this is just used for logging so we don't want to throw an error i it is misused. if (uuid.version() == 1) { // this is the difference between midnight October 15, 1582 UTC and midnight January 1, 1970 UTC as 100 nanosecond units long epochDiff = 122192928000000000L; // the UUID timestamp is in 100 nanosecond units. // convert that to milliseconds timeStamp = ((uuid.timestamp()-epochDiff)/10000); } return new Date(timeStamp); }
System.out.println( " uuid : " + uuid.toString() ); System.out.println( " variant : " + uuid.variant() ); System.out.println( " version : " + uuid.version() ); if ( uuid.variant() != 2 ) { throw new RuntimeException( "bad variant" );
@Test public void testUUIDUtils() { UUID uuid = UUIDUtils.newTimeUUID(); logger.info("" + uuid); logger.info("" + uuid.timestamp()); logger.info("" + UUIDUtils.getTimestampInMillis(uuid)); logger.info("" + UUIDUtils.getTimestampInMillis(UUIDUtils.newTimeUUID())); logger.info("" + System.currentTimeMillis()); logger.info("" + UUIDUtils.getTimestampInMicros(UUIDUtils.newTimeUUID())); logger.info("" + (System.currentTimeMillis() * 1000)); logger.info("" + UUIDUtils.MIN_TIME_UUID); logger.info("" + UUIDUtils.MIN_TIME_UUID.variant()); logger.info("" + UUIDUtils.MIN_TIME_UUID.version()); logger.info("" + UUIDUtils.MIN_TIME_UUID.clockSequence()); logger.info("" + UUIDUtils.MIN_TIME_UUID.timestamp()); logger.info("" + UUIDUtils.MAX_TIME_UUID); logger.info("" + UUIDUtils.MAX_TIME_UUID.variant()); logger.info("" + UUIDUtils.MAX_TIME_UUID.version()); logger.info("" + UUIDUtils.MAX_TIME_UUID.clockSequence()); logger.info("" + UUIDUtils.MAX_TIME_UUID.timestamp()); }
@Test public void testUsage() { Session session = openSession(); session.beginTransaction(); Node node = new Node(); session.save( node ); assertNotNull( node.getId() ); assertEquals( 2, node.getId().variant() ); assertEquals( 1, node.getId().version() ); session.getTransaction().commit(); session.close(); session = openSession(); session.beginTransaction(); session.delete( node ); session.getTransaction().commit(); session.close(); } }
final int variant = uuid.variant(); assertEquals("Incorrect variant. Expected 2 got " + variant, 2, variant); final int version = uuid.version(); assertEquals("Incorrect version. Expected 1 got " + version, 1, version); final long node = uuid.node();
if ( !isNew || node.getId().getUuid().version() != 1 ) { return false;
/** * Return the Unix timestamp contained by the provided time-based UUID. * * <p>This method is not equivalent to {@link UUID#timestamp()}. More precisely, a version 1 UUID * stores a timestamp that represents the number of 100-nanoseconds intervals since midnight, 15 * October 1582 and that is what {@link UUID#timestamp()} returns. This method however converts * that timestamp to the equivalent Unix timestamp in milliseconds, i.e. a timestamp representing * a number of milliseconds since midnight, January 1, 1970 UTC. In particular, the timestamps * returned by this method are comparable to the timestamps returned by {@link * System#currentTimeMillis}, {@link java.util.Date#getTime}, etc. * * @param uuid the UUID to return the timestamp of. * @return the Unix timestamp of {@code uuid}. * @throws IllegalArgumentException if {@code uuid} is not a version 1 UUID. */ public static long unixTimestamp(UUID uuid) { if (uuid.version() != 1) throw new IllegalArgumentException( String.format( "Can only retrieve the unix timestamp for version 1 uuid (provided version %d)", uuid.version())); long timestamp = uuid.timestamp(); return (timestamp / 10000) + START_EPOCH; }
@Override public String format(UUID value) { if (value == null) return "NULL"; if (value.version() != 1) throw new InvalidTypeException( String.format("%s is not a Type 1 (time-based) UUID", value)); return super.format(value); }
@Override public ByteBuffer serialize(UUID value, ProtocolVersion protocolVersion) { if (value == null) return null; if (value.version() != 1) throw new InvalidTypeException( String.format("%s is not a Type 1 (time-based) UUID", value)); return super.serialize(value, protocolVersion); } }
@Test(groups = "unit") public void conformanceTest() { // The UUIDs class does some computation at class initialization, which // may screw up our assumption below that UUIDs.timeBased() takes less // than 10ms, so force class loading now. UUIDs.random(); long now = System.currentTimeMillis(); UUID uuid = UUIDs.timeBased(); assertEquals(uuid.version(), 1); assertEquals(uuid.variant(), 2); long tstamp = UUIDs.unixTimestamp(uuid); // Check now and the uuid timestamp are within 10 millisseconds. assert now <= tstamp && now >= tstamp - 10 : String.format("now = %d, tstamp = %d", now, tstamp); }
public void testU5_4() { UUID u1 = UUID.randomUUID() ; JenaUUID u2 = JenaUUID.parse(u1.toString()) ; assertEquals(u1.version(), u2.getVersion()) ; assertEquals(u1.variant(), u2.getVariant()) ; assertEquals(u1.getMostSignificantBits(), u2.getMostSignificantBits()) ; assertEquals(u1.getLeastSignificantBits(), u2.getLeastSignificantBits()) ; assertEquals(u1.toString(), u2.toString()) ; }
public void testU5_3() { UUID_V4 u1 = factory4.generateV4() ; UUID u2 = UUID.fromString(u1.asString()) ; assertEquals(u1.getVariant(), u2.variant()) ; assertEquals(u1.getVersion(), u2.version()) ; assertEquals(u1.getMostSignificantBits(), u2.getMostSignificantBits()) ; assertEquals(u1.getLeastSignificantBits(), u2.getLeastSignificantBits()) ; }
public void testU5_5() { JenaUUID u1 = factory1.generate() ; UUID u2 = new UUID(u1.getMostSignificantBits(), u1.getLeastSignificantBits()) ; assertEquals(u1.getVersion(), u2.version()) ; assertEquals(u1.getVariant(), u2.variant()) ; assertEquals(u1.getMostSignificantBits(), u2.getMostSignificantBits()) ; assertEquals(u1.getLeastSignificantBits(), u2.getLeastSignificantBits()) ; assertEquals(u1.toString(), u2.toString()) ; }
public void testU5_6() { JenaUUID u1 = factory4.generate() ; UUID u2 = new UUID(u1.getMostSignificantBits(), u1.getLeastSignificantBits()) ; assertEquals(u1.getVersion(), u2.version()) ; assertEquals(u1.getVariant(), u2.variant()) ; assertEquals(u1.getMostSignificantBits(), u2.getMostSignificantBits()) ; assertEquals(u1.getLeastSignificantBits(), u2.getLeastSignificantBits()) ; assertEquals(u1.toString(), u2.toString()) ; }
public void testU5_2() { UUID_V1 u1 = factory1.generateV1() ; UUID u2 = UUID.fromString(u1.asString()) ; assertEquals(u1.getClockSequence(), u2.clockSequence()) ; assertEquals(u1.getTimestamp(), u2.timestamp()) ; assertEquals(u1.getNode(), u2.node()) ; assertEquals(u1.getVariant(), u2.variant()) ; assertEquals(u1.getVersion(), u2.version()) ; assertEquals(u1.getMostSignificantBits(), u2.getMostSignificantBits()) ; assertEquals(u1.getLeastSignificantBits(), u2.getLeastSignificantBits()) ; }