Awesome
Timeflake4j
Java implementation of Timeflake.
Timeflake is a 128-bit, roughly-ordered, URL-safe UUID. Inspired by Twitter's Snowflake, Instagram's ID and Firebase's PushID.
How to use
<dependency>
<groupId>am.ik.timeflake</groupId>
<artifactId>timeflake4j</artifactId>
<version>1.3.0</version>
</dependency>
Example
Generate a snowflake
final Timeflake timeflake = Timeflake.generate();
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698 (java.math.BigInteger)
System.out.println(timeflake.toUuid()); // 01772f27-10f3-091a-ea3f-28e7f6f7296a (java.util.UUID)
System.out.println(timeflake.toInstant()); // 2021-01-23T12:10:25.395Z (java.time.Instant)
System.out.println(timeflake.base62()); // 2lSUuPgi2bGXfZde730O2 (String)
TheadLocalRandom
is used to generate a random number by default. You can change Random
instance as follows:
final Timeflake timeflake = Timeflake.generate(new SecureRandom());
Create a snowflake
from an UUID
instance
final Timeflake timeflake = Timeflake.valueOf(UUID.fromString("01772f27-10f3-091a-ea3f-28e7f6f7296a"));
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
from a base62 encoded string
final Timeflake timeflake = Timeflake.valueOf("2lSUuPgi2bGXfZde730O2");
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
from a big integer value
final Timeflake timeflake = Timeflake.valueOf(new BigInteger("1948067690345842174618850429941262698"));
System.out.println(timeflake.value()); // 1948067690345842174618850429941262698
from a 48-bit timestamp and 80-bit random part
final Instant timestamp = Instant.now();
final BigInteger random = new BigInteger(80, ThreadLocalRandom.current());
final Timeflake timeflake = Timeflake.create(timestamp, random);
Note on security
See the original warning.
Note on privacy
See the original warning.
Required
- Java 8+
License
Licensed under the MIT License