001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
023     * A simplified UUID generator for sample SQL generation that generates UUID in
024     * a sequential order. This should not be used for any other purposes.
025     *
026     * @author Shuyang Zhou
027     */
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    }