UUIDHelper.java :  » Cassandra » jassandra » org » softao » jassandra » utils » Java Open Source

Java Open Source » Cassandra » jassandra 
jassandra » org » softao » jassandra » utils » UUIDHelper.java
package org.softao.jassandra.utils;

import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.util.UUID;

/**
 * 
 */
public final class UUIDHelper {
  /**
   * @return a new instance of time based UUID.
   */
  public static UUID createTimeBasedUUID() {
    org.safehaus.uuid.UUID timeBasedUUID = org.safehaus.uuid.UUIDGenerator
        .getInstance().generateTimeBasedUUID();
    return UUID.fromString(timeBasedUUID.toString());
  }

  /**
   * @param bytes
   * @return the UUID derived from <code>bytes</code>.
   */
  public static UUID toUUID(byte[] bytes) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    LongBuffer lb = bb.asLongBuffer();
    long high = lb.get();
    long low = lb.get();
    return new UUID(high, low);
  }

  /**
   * @param uuid
   * @return the bytes representation of UUID.
   */
  public static byte[] toBytes(UUID uuid) {
    ByteBuffer bb = ByteBuffer.allocate(16);
    LongBuffer lb = bb.asLongBuffer();
    lb.put(uuid.getMostSignificantBits());
    lb.put(uuid.getLeastSignificantBits());
    return bb.array();
  }

  private UUIDHelper() {
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.