/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package permafrost.tundra.id;

/**
 * A Java implementation of ULID, as per https://github.com/alizain/ulid.
 */
public class ULID {
    /**
     * Table of characters used to base32-encode the ULID time and random components.
     */
    private static final char[] ENCODE_TABLE = {
        '0','1','2','3','4','5','6','7','8','9',
        'A','B','C','D','E','F','G','H','J','K',
        'M','N','P','Q','R','S','T','V','W','X',
        'Y','Z'
    };
    /**
     * The number of characters in the ULID devoted to the time component.
     */
    private static final int TIME_LENGTH = 10;
    /**
     * The number of characters in the ULID devoted ot the random component.
     */
    private static final int RANDOM_LENGTH = 16;
    /**
     * The buffer into which the ULID is generated.
     */
    private StringBuilder buffer = new StringBuilder();

    /**
     * Constructs a new ULID.
     */
    private ULID() {
        encodeRandom();
        encodeTime();
    }

    /**
     * Encode the current time into this ULID's string buffer.
     */
    private void encodeTime() {
        long time = System.currentTimeMillis();

        for (int i = 0; i < TIME_LENGTH; i++) {
            int mod = (int)(time % ENCODE_TABLE.length);
            time = (time - mod) / ENCODE_TABLE.length;
            buffer.append(ENCODE_TABLE[mod]);
        }
    }

    /**
     * Encodes a randomness component into this ULID's string buffer.
     */
    private void encodeRandom() {
        for (int i = 0; i < RANDOM_LENGTH; i++) {
            int rand = (int)(Math.floor(ENCODE_TABLE.length * Math.random()));
            buffer.append(ENCODE_TABLE[rand]);
        }
    }

    /**
     * Returns the string representation of this ULID.
     *
     * @return The string representation of this ULID.
     */
    @Override
    public String toString() {
        return buffer.reverse().toString();
    }

    /**
     * Returns a newly generated ULID.
     *
     * @return A newly generated ULID.
     */
    public static String generate() {
        return new ULID().toString();
    }
}

@@ -1876,16 +1876,17 @@
 H = 16;%0A
+%0A
     /**%0A
@@ -1896,41 +1896,69 @@
   * 
-The buffer into which the ULID is
+Returns a newly generated ULID.%0A     *%0A     * @return A newly
  gen
@@ -1963,16 +1963,21 @@
 enerated
+ ULID
 .%0A     *
@@ -1983,22 +1983,56 @@
 */%0A    p
-rivate
+ublic static String generate() %7B%0A       
  StringB
@@ -2072,118 +2072,106 @@
 ();%0A
-%0A
     
-/**%0A     * Constructs a new ULID.%0A     */%0A    private ULID() %7B%0A        encodeRandom();%0A        encodeTime
+    encodeRandom(buffer);%0A        encodeTime(buffer);%0A        return buffer.reverse().toString
 ();%0A
@@ -2260,32 +2260,39 @@
  */%0A    private 
+static 
 void encodeTime(
@@ -2291,16 +2291,36 @@
 odeTime(
+StringBuilder buffer
 ) %7B%0A    
@@ -2328,17 +2328,24 @@
    long 
-t
+currentT
 ime = Sy
@@ -2447,17 +2447,24 @@
 = (int)(
-t
+currentT
 ime %25 EN
@@ -2499,17 +2499,31 @@
     
-time = (t
+currentTime = (currentT
 ime 
@@ -2713,16 +2713,23 @@
 private 
+static 
 void enc
@@ -2738,16 +2738,36 @@
 eRandom(
+StringBuilder buffer
 ) %7B%0A    
@@ -2832,16 +2832,24 @@
 int rand
+omNumber
  = (int)
@@ -2946,434 +2946,29 @@
 rand
-%5D);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Returns the string representation of this ULID.%0A     *%0A     * @return The string representation of this ULID.%0A     */%0A    @Override%0A    public String toString() %7B%0A        return buffer.reverse().toString(
+omNumber%5D
 );%0A    
-%7D%0A%0A
     
-/**%0A     * Returns a newly generated ULID.%0A     *%0A     * @return A newly generated ULID.%0A     */%0A    public static String generate() %7B%0A        return new ULID().toString();
+%7D
 %0A   

Refactor ULID implementation to require less object creation and hence garbage collection