com.datastax.driver.core.utils
Class UUIDs

java.lang.Object
  extended by com.datastax.driver.core.utils.UUIDs

public final class UUIDs
extends Object

Utility methods to work with UUID and most specifically with time-based ones (version 1).


Constructor Summary
UUIDs()
           
 
Method Summary
static UUID endOf(long timestamp)
          Creates a "fake" time-based UUID that sorts as the biggest possible version 1 UUID generated at the provided timestamp.
static UUID random()
          Creates a new random (version 4) UUID.
static UUID startOf(long timestamp)
          Creates a "fake" time-based UUID that sorts as the smallest possible version 1 UUID generated at the provided timestamp.
static UUID timeBased()
          Creates a new time-based (version 1) UUID.
static long unixTimestamp(UUID uuid)
          Return the unix timestamp contained by the provided time-based UUID.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UUIDs

public UUIDs()
Method Detail

random

public static UUID random()
Creates a new random (version 4) UUID.

This method is just a convenience for UUID.randomUUID().

Returns:
a newly generated, pseudo random, version 4 UUID.

timeBased

public static UUID timeBased()
Creates a new time-based (version 1) UUID.

UUID generated by this method are suitable for use with the timeuuid Cassandra type. In particular the generated UUID includes the timestamp of its generation.

Returns:
a new time-based UUID.

startOf

public static UUID startOf(long timestamp)
Creates a "fake" time-based UUID that sorts as the smallest possible version 1 UUID generated at the provided timestamp.

Such created UUID are useful in queries to select a time range of a timeuuid column.

The UUID created by this method are not unique and as such are not suitable for anything else than querying a specific time range. In particular, you should not insert such UUID.

Also, the timestamp to provide as parameter must be a unix timestamp (as returned by System.currentTimeMillis() or Date.getTime()), not a UUID 100-nanoseconds intervals since 15 October 1582. In other words, given a UUID uuid, you should never do startOf(uuid.timestamp()) but rather startOf(unixTimestamp(uuid.timestamp())).

Lastly, please note that Cassandra's timeuuid sorting is not compatible with UUID.compareTo(java.util.UUID) and hence the UUID created by this method are not necesssary lower bound for that latter method.

Parameters:
timestamp - the unix timestamp for which the created UUID must be a lower bound.
Returns:
the smallest (for Cassandra timeuuid sorting) UUID of timestamp.

endOf

public static UUID endOf(long timestamp)
Creates a "fake" time-based UUID that sorts as the biggest possible version 1 UUID generated at the provided timestamp.

Such created UUID are useful in queries to select a time range of a timeuuid column.

The UUID created by this method are not unique and as such are not suitable for anything else than querying a specific time range. In particular, you should not insert such UUID.

Also, the timestamp to provide as parameter must be a unix timestamp (as returned by System.currentTimeMillis() or Date.getTime()), not a UUID 100-nanoseconds intervals since 15 October 1582. In other words, given a UUID uuid, you should never do startOf(uuid.timestamp()) but rather startOf(unixTimestamp(uuid.timestamp())).

Lastly, please note that Cassandra's timeuuid sorting is not compatible with UUID.compareTo(java.util.UUID) and hence the UUID created by this method are not necesssary upper bound for that latter method.

Parameters:
timestamp - the unix timestamp for which the created UUID must be an upper bound.
Returns:
the biggest (for Cassandra timeuuid sorting) UUID of timestamp.

unixTimestamp

public static long unixTimestamp(UUID uuid)
Return the unix timestamp contained by the provided time-based UUID.

This method is not equivalent to 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 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 timestamp returned by System.currentTimeMillis(), Date.getTime(), etc.

Parameters:
uuid - the UUID to return the timestamp of.
Returns:
the unix timestamp of uuid.
Throws:
IllegalArgumentException - if uuid is not a version 1 UUID.


Copyright © 2013. All Rights Reserved.