001
014
015 package com.liferay.portal.tools.samplesqlbuilder;
016
017 import com.liferay.portal.kernel.util.StringBundler;
018 import com.liferay.portal.kernel.util.StringPool;
019
020 import java.util.concurrent.atomic.AtomicLong;
021
022
028 public class SequentialUUID {
029
030 public static String generate() {
031 long count = _counter.getAndIncrement();
032
033 long high = (count >> 48) & 0xffff;
034 long low = count & 0xffffffffffffL;
035
036 StringBundler sb = new StringBundler(4);
037
038 sb.append(_UUID_PREFIX);
039 sb.append(_toHexString(high, 4));
040 sb.append(StringPool.MINUS);
041 sb.append(_toHexString(low, 8));
042
043 return sb.toString();
044 }
045
046 public static SequentialUUID getSequentialUUID() {
047 return _instance;
048 }
049
050 private static String _toHexString(long number, int digits) {
051 char[] buffer = new char[digits];
052
053 for (int i = 0; i < digits; i++) {
054 buffer[i] = '0';
055 }
056
057 int index = digits;
058
059 do {
060 buffer[--index] = _HEX_DIGITS[(int) (number & 15)];
061
062 number >>>= 4;
063 }
064 while (number != 0);
065
066 return new String(buffer);
067 }
068
069 private static final char[] _HEX_DIGITS = {
070 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
071 'e', 'f'
072 };
073
074 private static final String _UUID_PREFIX = "00000000-0000-0000-";
075
076 private static SequentialUUID _instance = new SequentialUUID();
077
078 private static AtomicLong _counter = new AtomicLong();
079
080 }