- java.lang.Object
-
- com.github.f4b6a3.uuid.codec.base.BaseN
-
public final class BaseN extends Object
Class that represents the base-n encodings.
-
-
Field Summary
Fields Modifier and Type Field Description protected static StringALPHABET_36protected static StringALPHABET_64protected static intRADIX_MAXprotected static intRADIX_MIN
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static char[]expand(char a, char b)Expands a character sequence similar to 0-9, a-z and A-Z.protected static Stringexpand(String string)Expands character sequences similar to 0-9, a-z and A-Z.CharArraygetAlphabet()Returns the alphabet of the base-n.intgetLength()Returns the length of encoded UUIDs.LongArraygetMap()Returns the map of the base-n.chargetPadding()Return the padding character.intgetRadix()Returns the radix of the base-n.booleanisSensitive()Informs if the base-n is case-sensitive.booleanisValid(String uuid)Checks if the UUID string is valid.voidvalidate(String uuid)Checks if the UUID string is a valid.
-
-
-
Field Detail
-
RADIX_MIN
protected static final int RADIX_MIN
- See Also:
- Constant Field Values
-
RADIX_MAX
protected static final int RADIX_MAX
- See Also:
- Constant Field Values
-
ALPHABET_36
protected static final String ALPHABET_36
- See Also:
- Constant Field Values
-
ALPHABET_64
protected static final String ALPHABET_64
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BaseN
public BaseN(int radix)
Public constructor for the base-n object.The radix is the alphabet size.
The supported alphabet sizes are from 2 to 64.
If there are mixed cases in the alphabet, the base-n is case SENSITIVE.
The encoded string length is equal to `CEIL(128 / LOG2(n))`, where n is the radix. The encoded string is padded to fit the expected length.
The padding character is the first character of the string. For example, the padding character for the alphabet "abcdef0123456" is 'a'.
The example below shows how to create a
BaseNfor an hypothetical base-26 encoding that contains only letters. You only need to pass a number 40.String radix = 40; BaseN base = new BaseN(radix);If radix is greater than 36, the alphabet generated is a subset of the character sequence "0-9A-Za-z-_". Otherwise it is a subset of "0-9a-z". In the example above the resulting alphabet is "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcd" (0-9A-Za-d).
- Parameters:
radix- the radix to be used
-
BaseN
public BaseN(String alphabet)
Public constructor for the base-n object.The radix is the alphabet size.
The supported alphabet sizes are from 2 to 64.
If there are mixed cases in the alphabet, the base-n is case SENSITIVE.
The encoded string length is equal to `CEIL(128 / LOG2(n))`, where n is the radix. The encoded string is padded to fit the expected length.
The padding character is the first character of the string. For example, the padding character for the alphabet "abcdef0123456" is 'a'.
The example below shows how to create a
BaseNfor an hypothetical base-26 encoding that contains only letters. You only need to pass a string with 26 characters.
Alphabet strings similar to "a-f0-9" are expanded to "abcdef0123456789". The same example using the string "a-z" instead of "abcdefghijklmnopqrstuvwxyz":String alphabet = "abcdefghijklmnopqrstuvwxyz"; BaseN base = new BaseN(alphabet);String alphabet = "a-z"; BaseN base = new BaseN(alphabet);- Parameters:
alphabet- the alphabet to be used
-
-
Method Detail
-
getRadix
public int getRadix()
Returns the radix of the base-n.- Returns:
- the radix
-
getLength
public int getLength()
Returns the length of encoded UUIDs.- Returns:
- the length
-
getPadding
public char getPadding()
Return the padding character.- Returns:
- a character
-
isSensitive
public boolean isSensitive()
Informs if the base-n is case-sensitive.- Returns:
- true if it is case-sensitive
-
getAlphabet
public CharArray getAlphabet()
Returns the alphabet of the base-n.- Returns:
- the alphabet
-
getMap
public LongArray getMap()
Returns the map of the base-n.- Returns:
- a map
-
isValid
public boolean isValid(String uuid)
Checks if the UUID string is valid.- Parameters:
uuid- a UUID string- Returns:
- true if valid, false if invalid
-
validate
public void validate(String uuid)
Checks if the UUID string is a valid.- Parameters:
uuid- a UUID string- Throws:
InvalidUuidException- if the argument is invalid
-
expand
protected static String expand(String string)
Expands character sequences similar to 0-9, a-z and A-Z.- Parameters:
string- a string to be expanded- Returns:
- a string
-
expand
protected static char[] expand(char a, char b)Expands a character sequence similar to 0-9, a-z and A-Z.- Parameters:
a- the first character of the sequenceb- the last character of the sequence- Returns:
- an expanded sequence of characters
-
-