public final class Hashids extends Object
Implementation of the Hashids algorithm:
An instance with the default interoperable configurations is available through the default constructor
.
Configured instances can be created via Hashids.Builder
to set a salt
, define a
·minimum hash length
or use a custom alphabet
. Optional features
can be enabled via the features(HashidsFeature)
method. Please note that most features will break the
interoperability with the origin algorithm implementation!
Instances of this class are thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
Hashids.Builder
|
Modifier and Type | Field and Description |
---|---|
static char[] |
DEFAULT_ALPHABET
The default alphabet for the hash generation.
|
static long |
MAX_INTEROP_NUMBER_SIZE
The maximum number size to ensure interoperability with the origin algorithm implementation hashids
.js.
|
Constructor and Description |
---|
Hashids()
Constructs a new instance without a salt, no minimum hash length, the
default alphabet and no enabled
features . |
Modifier and Type | Method and Description |
---|---|
long[] |
decode(String hash)
Decodes the given hash into its numeric representation based on this instance configuration.
|
String |
decodeHex(String hash)
Decodes the given hash into its hexadecimal representation based on this instance configuration.
|
Optional<Long> |
decodeOne(String hash)
Decodes the given valid hash into its single numeric representation based on this instance configuration.
|
String |
encode(long... numbers)
Encodes the given positive numbers based on this instance configuration.
|
String |
encodeHex(String hexNumbers)
Encodes the given numbers in hexadecimal format based on this instance configuration.
|
boolean |
equals(Object otherObject) |
static String |
getInteropVersion()
Returns the version of the original algorithm implementation hashids
.js with which the public API is interoperable.
|
static String |
getVersion()
Returns the version of the public API.
|
String |
toString() |
public static final long MAX_INTEROP_NUMBER_SIZE
This limit is based on the JavaScript Number.MAX_SAFE_INTEGER constant which represents the maximum safe integer.
The limit can be canceled by enabling the NO_MAX_INTEROP_NUMBER_SIZE
feature. Please
note that this will break the interoperability with the origin algorithm implementation!
public static final char[] DEFAULT_ALPHABET
A customized alphabet can be set via the instance builder alphabet
method.
public Hashids()
default alphabet
and no enabled
features
.public String encode(long... numbers)
The given numbers size must not be larger than the maximum interoperability size
unless the
NO_MAX_INTEROP_NUMBER_SIZE
feature is enabled. Please
note that this will break the interoperability with the origin algorithm implementation!
numbers
- the positive numbers to be encodedIllegalArgumentException
- if the EXCEPTION_HANDLING
feature is enabled and the total length of
numbers is zero, any numbers size is invalid or larger than the maximum interoperability size
NullPointerException
- if the EXCEPTION_HANDLING
feature is enabled and the given numbers are null
public String encodeHex(String hexNumbers)
The given hexadecimal numbers must not be prefixed (0x
or 0X
) unless the
ALLOW_HEXADECIMAL_NUMBER_PREFIX
feature is enabled. Please
note that this will break the interoperability with the origin algorithm implementation!
hexNumbers
- the numbers in hexadecimal format to be encodedIllegalArgumentException
- if the EXCEPTION_HANDLING
feature is enabled and any of the numbers is invalidNullPointerException
- if the EXCEPTION_HANDLING
feature is enabled and the given numbers are null
public long[] decode(String hash)
hash
- the hash to be decodedIllegalArgumentException
- if the EXCEPTION_HANDLING
feature is enabled and the hash is invalidNullPointerException
- if the EXCEPTION_HANDLING
feature is enabled and the given hash is null
public String decodeHex(String hash)
hash
- the hash to be decodedNullPointerException
- if the EXCEPTION_HANDLING
feature is enabled and the given hash is null
public Optional<Long> decodeOne(String hash)
Simplifies the use-case where the amount of resulting numbers is known before to handle the return value as single value instead of an array.
The given hash must resolve into a one number only, otherwise see the decode
method.
hash
- the valid hash to be decodedIllegalArgumentException
- if the EXCEPTION_HANDLING
feature is enabled and the hash is invalidNullPointerException
- if the EXCEPTION_HANDLING
feature is enabled and the given hash is null
public static String getVersion()
public static String getInteropVersion()
Copyright © 2017 Arctic Ice Studio