Skip to content

WhackDevelopment/java-snowflakeid

Repository files navigation


Java SnowflakeIds


Implementation of Java-SnowflakeId

Usage


Import Snowflake

Add Repository in Gradle

// Repository - Groovy
maven {
    name = "whackdevelopment-snapshots"
    url = "https://repo.whackdevelopment.com/repository/maven-snapshots/"
}

// Repository - Kotlin DSL
maven("https://repo.whackdevelopment.com/repository/maven-snapshots/")

Gradle import

// Import - Groovy
implementation "com.whackdevelopment.snowflake:snowflakeid:1.0.0-SNAPSHOT"

// Import - Kotlin DSL
implementation("com.whackdevelopment.snowflake:snowflakeid:1.0.0-SNAPSHOT")

Add Repository in Maven

<repository>
    <id>whackdevelopment-snapshots</id>
    <url>https://repo.whackdevelopment.com/repository/maven-snapshots/</url>
</repository>

Maven import

<dependency>
    <groupId>com.whackdevelopment.snowflake</groupId>
    <artifactId>snowflakeid</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

Create a new SnowflakeGenerator

SnowflakeGenerator flakeGenerator=new SnowflakeGenerator(
        /*machineId*/ 1, // optional, define machine id (defaults to 1)
        /*timeOffset*/ 0 // optional, define a offset time (defaults to 0)
        );

Options

machineId: (Defaults to 1) A machine id or any random id. If you are generating id in distributed system, its highly advised to provide a proper machineId which is unique to different machines.

timeOffset: (Defaults to 0) Time offset will be subtracted from current time to get the first 42 bit of id. This help in generating smaller ids. ( not recommended )


Generate a Snowflake

Snowflake id1=flakeGenerator.generate(); // returns something like 112867124767768576
        Snowflake id2=flakeGenerator.generate(); // returns something like 112867124784545792
        String id1String=id1.toString();
        String id2String=id2.toString();

Create a new Snowflake from a Snowflake String

Snowflake id1Clone=new Snowflake(id1.toString());

About

generates unique 64-bit snowflake IDs inspired by Twitter and Discord IDs

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

  •  
  •