Last active
February 26, 2018 18:09
-
-
Save formix/d9521fd49cbeee305e2a to your computer and use it in GitHub Desktop.
Revisions
-
Jean-Philippe Gravel revised this gist
Feb 26, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 microseconds. * * @return a new id. */ -
Jean-Philippe Gravel revised this gist
Jul 31, 2017 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, which translate to 1 id every * 16 nanoseconds. * * @return a new id. */ -
Jean-Philippe Gravel revised this gist
Apr 7, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. * -
Jean-Philippe Gravel revised this gist
Apr 7, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 int counter = -1; private SUID() { @@ -45,10 +44,11 @@ private SUID() { * @return a new id. */ public static synchronized long nextId() { 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; -
Jean-Philippe Gravel revised this gist
Aug 21, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 (@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. -
Jean-Philippe Gravel revised this gist
Aug 21, 2015 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) { counter = rnd.nextInt(SHORT_MAX); } long id = (now << 16) | counter; counter = (counter + 1) % SHORT_MAX; return id; } } -
Jean-Philippe Gravel revised this gist
Jul 1, 2015 . 1 changed file with 10 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = -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 id per millisecond. * * @return a new id. */ 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; } -
Jean-Philippe Gravel revised this gist
Jul 1, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ids per millisecond. * * @return a new id. */ -
Jean-Philippe Gravel revised this gist
Jul 1, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /**************************************************************************** * 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. -
Jean-Philippe Gravel created this gist
Jul 1, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } }