Example usage for java.util UUID version

List of usage examples for java.util UUID version

Introduction

In this page you can find the example usage for java.util UUID version.

Prototype

public int version() 

Source Link

Document

The version number associated with this UUID .

Usage

From source file:Main.java

public static void main(String[] args) {
    UUID uid = UUID.randomUUID();

    // checking version value
    System.out.println("Version value: " + uid.version());
}

From source file:net.geertvos.theater.core.util.UUIDGen.java

/**
 * Returns a milliseconds-since-epoch value for a type-1 UUID.
 * //w ww. j a v a2s. com
 * @param uuid a type-1 (time-based) UUID
 * @return the number of milliseconds since the unix epoch
 * @throws IllegalArgumentException if the UUID is not version 1
 */
public static long getAdjustedTimestamp(UUID uuid) {
    if (uuid.version() != 1)
        throw new IllegalArgumentException("incompatible with uuid version: " + uuid.version());
    return (uuid.timestamp() / 10000) - START_EPOCH;
}

From source file:kina.rdd.CassandraRDDUtils.java

/**
 * Returns an instance of the Cassandra validator that matches the provided object.
 *
 * @param obj the object to use to resolve the cassandra marshaller.
 * @param <T> the generic object type.
 * @return an instance of the Cassandra validator that matches the provided object.
 * @throws kina.exceptions.GenericException if no validator can be found for the specified object.
 *//*  w w w .jav  a  2 s.  com*/
public static <T> AbstractType<?> marshallerInstance(T obj) {
    AbstractType<?> abstractType = MAP_JAVA_TYPE_TO_ABSTRACT_TYPE.get(obj.getClass());

    if (obj instanceof UUID) {
        UUID uuid = (UUID) obj;

        if (uuid.version() == 1) {
            abstractType = TimeUUIDType.instance;

        } else {
            abstractType = UUIDType.instance;
        }
    }

    if (abstractType == null) {
        throw new GenericException("parameter class " + obj.getClass().getCanonicalName() + " does not have a"
                + " Cassandra marshaller");
    }

    return abstractType;
}

From source file:com.easemob.dataexport.utils.UUIDUtils.java

public static boolean isTimeBased(UUID uuid) {
    if (uuid == null) {
        return false;
    }/*from  ww  w.j  a  va  2  s .  com*/
    return uuid.version() == 1;
}

From source file:com.stratio.deep.cassandra.util.CassandraUtils.java

/**
 * Returns an instance of the Cassandra validator that matches the provided object.
 *
 * @param obj the object to use to resolve the cassandra marshaller.
 * @param <T> the generic object type.
 * @return an instance of the Cassandra validator that matches the provided object.
 * @throws com.stratio.deep.commons.exception.DeepGenericException if no validator can be found for the specified object.
 *//*w ww . j  a va2 s  .  com*/
public static <T> AbstractType<?> marshallerInstance(T obj) {

    AbstractType<?> abstractType = null;

    if (obj != null) {
        abstractType = MAP_JAVA_TYPE_TO_ABSTRACT_TYPE.get(obj.getClass());

        if (obj instanceof UUID) {
            UUID uuid = (UUID) obj;

            if (uuid.version() == 1) {
                abstractType = TimeUUIDType.instance;

            } else {
                abstractType = UUIDType.instance;
            }
        }

        if (abstractType == null) {
            //LIST Case
            if (List.class.isAssignableFrom(obj.getClass())) {

                List list = (List) obj;
                if (!list.isEmpty()) {
                    abstractType = ListType.getInstance(marshallerInstance(list.get(0)));
                }

            }
            // SET Case
            else if (Set.class.isAssignableFrom(obj.getClass())) {
                Set set = (Set) obj;
                if (!set.isEmpty()) {
                    java.util.Iterator i = set.iterator();
                    Object o = i.next();
                    abstractType = SetType.getInstance(marshallerInstance(o));
                }
            }
            // MAP Case
            else if (Map.class.isAssignableFrom(obj.getClass())) {
                Set set = ((Map) obj).keySet();
                if (!set.isEmpty()) {
                    java.util.Iterator i = set.iterator();
                    Object o = i.next();
                    abstractType = MapType.getInstance(marshallerInstance(o),
                            marshallerInstance(((Map) obj).get(o)));

                }

            }
        }

    }

    if (abstractType == null) {
        throw new DeepGenericException("parameter class " + obj.getClass().getCanonicalName()
                + " does not have a" + " Cassandra marshaller");
    }

    return abstractType;
}

From source file:org.apache.cassandra.db.marshal.TimeUUIDType.java

public String getString(ByteBuffer bytes) {
    if (bytes.remaining() == 0) {
        return "";
    }/*from  w w w. j  ava2 s  .  c  om*/
    if (bytes.remaining() != 16) {
        throw new MarshalException("UUIDs must be exactly 16 bytes");
    }
    UUID uuid = UUIDGen.getUUID(bytes);
    if (uuid.version() != 1) {
        throw new MarshalException("TimeUUID only makes sense with version 1 UUIDs");
    }
    return uuid.toString();
}

From source file:org.apache.cassandra.db.marshal.TimeUUIDType.java

public ByteBuffer fromString(String source) throws MarshalException {
    // Return an empty ByteBuffer for an empty string.
    if (source.isEmpty())
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;

    ByteBuffer idBytes = null;/*from www  .jav a2s.com*/

    // ffffffff-ffff-ffff-ffff-ffffffffff
    if (regexPattern.matcher(source).matches()) {
        UUID uuid = null;
        try {
            uuid = UUID.fromString(source);
            idBytes = decompose(uuid);
        } catch (IllegalArgumentException e) {
            throw new MarshalException(String.format("unable to make UUID from '%s'", source), e);
        }

        if (uuid.version() != 1)
            throw new MarshalException("TimeUUID supports only version 1 UUIDs");
    } else if (source.toLowerCase().equals("now")) {
        idBytes = ByteBuffer
                .wrap(UUIDGen.decompose(UUIDGen.makeType1UUIDFromHost(FBUtilities.getLocalAddress())));
    }
    // Milliseconds since epoch?
    else if (source.matches("^\\d+$")) {
        try {
            idBytes = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes(Long.parseLong(source)));
        } catch (NumberFormatException e) {
            throw new MarshalException(String.format("unable to make version 1 UUID from '%s'", source), e);
        }
    }
    // Last chance, attempt to parse as date-time string
    else {
        try {
            long timestamp = DateUtils.parseDate(source, iso8601Patterns).getTime();
            idBytes = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes(timestamp));
        } catch (ParseException e1) {
            throw new MarshalException(String.format("unable to coerce '%s' to version 1 UUID", source), e1);
        }
    }

    return idBytes;
}

From source file:org.usergrid.utils.UUIDUtils.java

/**
 * @param uuid//www.ja  v  a  2  s. c o  m
 * @return
 */
public static boolean isTimeBased(UUID uuid) {
    if (uuid == null) {
        return false;
    }
    return uuid.version() == 1;
}