1111 */
1212public with sharing class ULID {
1313 /**
14- * This character set is the complete list of allowed characters in
14+ * @description This character set is the complete list of allowed characters in
1515 * a ULID string. It intentionally does not include characters that
1616 * may be ambiguously read, such as i, l, o, and u characters.
1717 */
18- private static final List <String > CHARACTERSET = new List <String >{
18+ private static final List <String > CHARACTER_SET = new List <String >{
1919 ' 0' ,
2020 ' 1' ,
2121 ' 2' ,
@@ -50,23 +50,22 @@ public with sharing class ULID {
5050 ' Z'
5151 };
5252 // Calculate this once per transaction to avoid unnecessary math
53- private static final Long CHARACTERSETSIZE = CHARACTERSET .size ();
54- // This is equal to 2^48-1 and represents the max timestamp
55- // allowed in a ULID string
53+ private static final Long CHARACTER_SET_SIZE = CHARACTER_SET .size ();
54+ // This is equal to 2^48-1 and represents the max timestamp allowed in a ULID string
5655 // private static final Long MAXTIME = 281474976710655L;
5756 // This is the number of digits to encode the timestamp into.
58- private static final Long TIMELENGTH = 10 ;
57+ private static final Long TIME_LENGTH = 10 ;
5958 // This is the number of digits of random character to generate
60- private static final Integer RANDOMLENGTH = 16 ;
59+ private static final Integer RANDOM_LENGTH = 16 ;
6160
6261 /**
6362 * @description Generates a ULID string according to spec.
6463 * https://github.com/ulid/spec
6564 * @return `String`
6665 */
6766 public static String generate () {
68- return encodeTimestamp (DateTime .now ().getTime (), TIMELENGTH ) +
69- generateRandomString (RANDOMLENGTH );
67+ return encodeTimestamp (Datetime .now ().getTime (), TIME_LENGTH ) +
68+ generateRandomString (RANDOM_LENGTH );
7069 }
7170
7271 /**
@@ -76,13 +75,14 @@ public with sharing class ULID {
7675 * @param timeLength how many characters of the timestamp to encode
7776 * @return `String`
7877 */
78+ @SuppressWarnings('PMD.UnusedLocalVariable')
7979 private static String encodeTimestamp (Long dtSeed , Long timeLength ) {
8080 Long modulo ;
8181 String retString = ' ' ;
8282 for (Long l = timeLength ; timeLength > 0 ; timeLength -- ) {
83- modulo = Math .mod (dtSeed , CHARACTERSETSIZE );
84- retString = CHARACTERSET [modulo .intValue ()] + retString ;
85- dtSeed = (dtSeed - modulo ) / CHARACTERSETSIZE ;
83+ modulo = Math .mod (dtSeed , CHARACTER_SET_SIZE );
84+ retString = CHARACTER_SET [modulo .intValue ()] + retString ;
85+ dtSeed = (dtSeed - modulo ) / CHARACTER_SET_SIZE ;
8686 }
8787 return retString ;
8888 }
@@ -108,7 +108,7 @@ public with sharing class ULID {
108108 * @return `String`
109109 */
110110 private static String fetchRandomCharacterFromCharacterSet () {
111- Long rand = Math .mod (Math .abs (Crypto .getRandomLong ()), CHARACTERSETSIZE );
112- return CHARACTERSET [rand .intValue ()];
111+ Long rand = Math .mod (Math .abs (Crypto .getRandomLong ()), CHARACTER_SET_SIZE );
112+ return CHARACTER_SET [rand .intValue ()];
113113 }
114114}
0 commit comments