- java.lang.Object
-
- com.github.f4b6a3.uuid.codec.base.BaseNCodec
-
- Direct Known Subclasses:
Base16Codec,Base32Codec,Base58BtcCodec,Base62Codec,Base64Codec,Base64UrlCodec
public abstract class BaseNCodec extends Object implements UuidCodec<String>
Abstract class that contains the basic functionality for base-n codecs of this package.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceBaseNCodec.CustomDividerA division function that returns quotient and remainder.
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseNCodec(BaseN base)protectedBaseNCodec(BaseN base, BaseNCodec.CustomDivider divider)protectedBaseNCodec(BaseN base, BaseNEncoder encoder, BaseNDecoder decoder)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description UUIDdecode(String string)Get a UUID from an encoded string.Stringencode(UUID uuid)Get an encoded string from a UUID.BaseNgetBase()Get the base-n encoding object.static BaseNCodecnewInstance(int radix)Static factory that returns a new instance ofBaseNCodecusing the specified radix.static BaseNCodecnewInstance(int radix, BaseNCodec.CustomDivider divider)Static factory that returns a new instance ofBaseNCodecusing the specified radix and aBaseNCodec.CustomDivider.static BaseNCodecnewInstance(BaseN base)Static factory that returns a new instance ofBaseNCodecusing the specifiedBaseN.static BaseNCodecnewInstance(BaseN base, BaseNCodec.CustomDivider divider)Static factory that returns a new instance ofBaseNCodecusing the specifiedBaseNand aBaseNCodec.CustomDivider.static BaseNCodecnewInstance(String alphabet)Static factory that returns a new instance ofBaseNCodecusing the specified alphabet.static BaseNCodecnewInstance(String alphabet, BaseNCodec.CustomDivider divider)Static factory that returns a new instance ofBaseNCodecusing the specified alphabet and aBaseNCodec.CustomDivider.
-
-
-
Constructor Detail
-
BaseNCodec
protected BaseNCodec(BaseN base)
- Parameters:
base- an object that represents the base-n encoding
-
BaseNCodec
protected BaseNCodec(BaseN base, BaseNCodec.CustomDivider divider)
- Parameters:
base- an object that represents the base-n encodingdivider- a division function that returns quotient and remainder
-
BaseNCodec
protected BaseNCodec(BaseN base, BaseNEncoder encoder, BaseNDecoder decoder)
- Parameters:
base- an object that represents the base-n encodingencoder- a functional encoderdecoder- a functional decoder
-
-
Method Detail
-
newInstance
public static BaseNCodec newInstance(BaseN base)
Static factory that returns a new instance ofBaseNCodecusing the specifiedBaseN.This method can be used if none of the existing concrete codecs of this package class is desired.
The
BaseNCodecobjects provided by this method encode UUIDs using remainder operation (modulus), a common approach to encode integers.If you need a
BaseNthat is not available in this package, use the static factoriesnewInstance(String)ornewInstance(int).- Parameters:
base- an object that represents the base-n encoding- Returns:
- a
BaseNCodec
-
newInstance
public static BaseNCodec newInstance(int radix)
Static factory that returns a new instance ofBaseNCodecusing the specified radix.This method can be used if none of the existing concrete codecs of this package class is desired.
The
BaseNCodecobjects provided by this method encode UUIDs using remainder operator (modulus), a common approach to encode integers.The example below shows how to create a
BaseNCodecfor an hypothetical base-40 encoding that contains only letters. You only need to pass a number 40. TheBaseNCodecinstantiates aBaseNobject internally. SeeBaseN.String radix = 40; BaseNCodec codec = BaseNCodec.newInstance(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- Returns:
- a
BaseNCodec
-
newInstance
public static BaseNCodec newInstance(String alphabet)
Static factory that returns a new instance ofBaseNCodecusing the specified alphabet.This method can be used if none of the existing concrete codecs of this package class is desired.
The
BaseNCodecobjects provided by this method encode UUIDs using remainder operator (modulus), a common approach to encode integers.The example below shows how to create a
BaseNCodecfor an hypothetical base-26 encoding that contains only letters. You only need to pass a string with 26 characters. TheBaseNCodecinstantiates aBaseNobject internally. SeeBaseN.String alphabet = "abcdefghijklmnopqrstuvwxyz"; BaseNCodec codec = BaseNCodec.newInstance(alphabet);Alphabet strings similar to "a-f0-9" are expanded to "abcdef0123456789". The same example using the string "a-z" instead of "abcdefghijklmnopqrstuvwxyz":
String alphabet = "a-z"; BaseNCodec codec = BaseNCodec.newInstance(alphabet);- Parameters:
alphabet- the alphabet to be used- Returns:
- a
BaseNCodec
-
newInstance
public static BaseNCodec newInstance(BaseN base, BaseNCodec.CustomDivider divider)
Static factory that returns a new instance ofBaseNCodecusing the specifiedBaseNand aBaseNCodec.CustomDivider.- Parameters:
base- an object that represents the base-n encodingdivider- a division function that returns quotient and remainder- Returns:
- a
BaseNCodec
-
newInstance
public static BaseNCodec newInstance(int radix, BaseNCodec.CustomDivider divider)
Static factory that returns a new instance ofBaseNCodecusing the specified radix and aBaseNCodec.CustomDivider.- Parameters:
radix- the radix to be useddivider- a division function that returns quotient and remainder- Returns:
- a
BaseNCodec
-
newInstance
public static BaseNCodec newInstance(String alphabet, BaseNCodec.CustomDivider divider)
Static factory that returns a new instance ofBaseNCodecusing the specified alphabet and aBaseNCodec.CustomDivider.- Parameters:
alphabet- the alphabet to be useddivider- a division function that returns quotient and remainder- Returns:
- a
BaseNCodec
-
getBase
public BaseN getBase()
Get the base-n encoding object.- Returns:
- a base-n encoding object
-
encode
public String encode(UUID uuid)
Get an encoded string from a UUID.- Specified by:
encodein interfaceUuidCodec<String>- Parameters:
uuid- a UUID- Returns:
- an encoded string
- Throws:
InvalidUuidException- if the argument is invalid
-
-