Skip to content

Instantly share code, notes, and snippets.

@formix
Last active February 26, 2018 18:09
Show Gist options
  • Select an option

  • Save formix/d9521fd49cbeee305e2a to your computer and use it in GitHub Desktop.

Select an option

Save formix/d9521fd49cbeee305e2a to your computer and use it in GitHub Desktop.

Revisions

  1. Jean-Philippe Gravel revised this gist Feb 26, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SUID.java
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ private SUID() {
    * milliseconds since epoch (Jan. 1, 1970) and using a 16 bits counter. The
    * counter is initialized at a random number. This generator can create up
    * to 65536 different id per millisecond, which translate to 1 id every
    * 16 nanoseconds.
    * 16 microseconds.
    *
    * @return a new id.
    */
  2. Jean-Philippe Gravel revised this gist Jul 31, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion SUID.java
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,8 @@ private SUID() {
    * Creates a unique 64 bits ID by aggregating the current time in
    * milliseconds since epoch (Jan. 1, 1970) and using a 16 bits counter. The
    * counter is initialized at a random number. This generator can create up
    * to 65536 different id per millisecond.
    * to 65536 different id per millisecond, which translate to 1 id every
    * 16 nanoseconds.
    *
    * @return a new id.
    */
  3. Jean-Philippe Gravel revised this gist Apr 7, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions SUID.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    /****************************************************************************
    * Copyright 2009-2015 Jean-Philippe Gravel, P. Eng. CSDP
    *
    * Modified 4/7/16 - Jean-Philippe Gravel (@formix) - Moved random number
    * generator into the method "if" statement.
    *
    * Modified 6/8/15 - Osric Wilkinson (@Moosemorals) - No point messing with
    * random seeds. Just use SecureRandom.
    *
  4. Jean-Philippe Gravel revised this gist Apr 7, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions SUID.java
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,6 @@
    final class SUID {

    private static final int SHORT_MAX = 65536;
    private static Random rnd = new SecureRandom();
    private static int counter = -1;

    private SUID() {
    @@ -45,10 +44,11 @@ private SUID() {
    * @return a new id.
    */
    public static synchronized long nextId() {
    long now = System.currentTimeMillis();
    if (counter == -1) {
    Random rnd = new SecureRandom();
    counter = rnd.nextInt(SHORT_MAX);
    }
    long now = System.currentTimeMillis();
    long id = (now << 16) | counter;
    counter = (counter + 1) % SHORT_MAX;
    return id;
  5. Jean-Philippe Gravel revised this gist Aug 21, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions SUID.java
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    /****************************************************************************
    * Copyright 2009-2015 Jean-Philippe Gravel, P. Eng. CSDP
    *
    * Modified 6/8/15 - Osric Wilkinson - No point messing with random seeds.
    * Just use SecureRandom.
    * Modified 6/8/15 - Osric Wilkinson (@Moosemorals) - No point messing with
    * random seeds. Just use SecureRandom.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  6. Jean-Philippe Gravel revised this gist Aug 21, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions SUID.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    /****************************************************************************
    * Copyright 2009-2015 Jean-Philippe Gravel, P. Eng. CSDP
    *
    * Modified 6/8/15 - Osric Wilkinson - No point messing with random seeds.
    * Just use SecureRandom.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    @@ -16,6 +19,7 @@
    package org.formix.utils;

    import java.util.Random;
    import java.util.SecureRandom;

    /**
    * Sufficiently Unique Identifier
    @@ -26,7 +30,7 @@
    final class SUID {

    private static final int SHORT_MAX = 65536;

    private static Random rnd = new SecureRandom();
    private static int counter = -1;

    private SUID() {
    @@ -43,12 +47,10 @@ private SUID() {
    public static synchronized long nextId() {
    long now = System.currentTimeMillis();
    if (counter == -1) {
    long seed = now ^ Thread.currentThread().getId();
    Random rnd = new Random(Long.hashCode(seed));
    counter = rnd.nextInt(SHORT_MAX);
    }
    long id = (now << 16) | counter;
    counter = (counter + 1) % SHORT_MAX;
    return id;
    }
    }
    }
  7. Jean-Philippe Gravel revised this gist Jul 1, 2015. 1 changed file with 10 additions and 9 deletions.
    19 changes: 10 additions & 9 deletions SUID.java
    Original file line number Diff line number Diff line change
    @@ -26,13 +26,8 @@
    final class SUID {

    private static final int SHORT_MAX = 65536;

    private static int counter;

    static {
    Random rnd = new Random();
    counter = rnd.nextInt(SHORT_MAX);
    }

    private static int counter = -1;

    private SUID() {
    }
    @@ -41,12 +36,18 @@ private SUID() {
    * Creates a unique 64 bits ID by aggregating the current time in
    * milliseconds since epoch (Jan. 1, 1970) and using a 16 bits counter. The
    * counter is initialized at a random number. This generator can create up
    * to 65536 different ids per millisecond.
    * to 65536 different id per millisecond.
    *
    * @return a new id.
    */
    public static synchronized long nextId() {
    long id = (System.currentTimeMillis() << 16) | counter;
    long now = System.currentTimeMillis();
    if (counter == -1) {
    long seed = now ^ Thread.currentThread().getId();
    Random rnd = new Random(Long.hashCode(seed));
    counter = rnd.nextInt(SHORT_MAX);
    }
    long id = (now << 16) | counter;
    counter = (counter + 1) % SHORT_MAX;
    return id;
    }
  8. Jean-Philippe Gravel revised this gist Jul 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SUID.java
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ private SUID() {
    * Creates a unique 64 bits ID by aggregating the current time in
    * milliseconds since epoch (Jan. 1, 1970) and using a 16 bits counter. The
    * counter is initialized at a random number. This generator can create up
    * to 65536 different id per millisecond.
    * to 65536 different ids per millisecond.
    *
    * @return a new id.
    */
  9. Jean-Philippe Gravel revised this gist Jul 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SUID.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /****************************************************************************
    * Copyright 2009-2014 Jean-Philippe Gravel, P. Eng. CSDP
    * Copyright 2009-2015 Jean-Philippe Gravel, P. Eng. CSDP
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  10. Jean-Philippe Gravel created this gist Jul 1, 2015.
    53 changes: 53 additions & 0 deletions SUID.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    /****************************************************************************
    * Copyright 2009-2014 Jean-Philippe Gravel, P. Eng. CSDP
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    ***************************************************************************/
    package org.formix.utils;

    import java.util.Random;

    /**
    * Sufficiently Unique Identifier
    *
    * @author formix
    *
    */
    final class SUID {

    private static final int SHORT_MAX = 65536;

    private static int counter;

    static {
    Random rnd = new Random();
    counter = rnd.nextInt(SHORT_MAX);
    }

    private SUID() {
    }

    /**
    * Creates a unique 64 bits ID by aggregating the current time in
    * milliseconds since epoch (Jan. 1, 1970) and using a 16 bits counter. The
    * counter is initialized at a random number. This generator can create up
    * to 65536 different id per millisecond.
    *
    * @return a new id.
    */
    public static synchronized long nextId() {
    long id = (System.currentTimeMillis() << 16) | counter;
    counter = (counter + 1) % SHORT_MAX;
    return id;
    }
    }