Skip to content

Commit

Permalink
Merge pull request #44 from LinuxCounter/43_ulid
Browse files Browse the repository at this point in the history
[#43] ULID support and example usage in JPA entity
  • Loading branch information
bmarwell committed Jan 6, 2022
2 parents 98ecbac + 831a3a4 commit dfc041e
Show file tree
Hide file tree
Showing 13 changed files with 646 additions and 0 deletions.
18 changes: 18 additions & 0 deletions common/ulid/pom.xml
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.linuxcounter</groupId>
<artifactId>linuxcounter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>linuxcounter-common-ulid</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>lico :: common :: ULID</name>

</project>
@@ -0,0 +1,82 @@
package io.github.linuxcounter.common.ulid;

import java.io.Serializable;
import java.util.Objects;

public class ULID implements Comparable<ULID>, Serializable {

private static final ULIDImpl generator = new ULIDImpl();

private final ULIDImpl.Value value;

private ULID(byte[] ulidBytes) {
this.value = ULIDImpl.fromBytes(ulidBytes);
}

private ULID(String ulidString) {
this.value = ULIDImpl.parseULID(ulidString);
}

public ULID(long mostSigBits, long leastSigBits) {
value = new ULIDImpl.Value(mostSigBits, leastSigBits);
}

public static ULID randomULID() {
return new ULID(generator.nextULID());
}

public static ULID fromString(String ulidString) {
return new ULID(ulidString);
}

/**
* Returns the least significant 64 bits of this UUID's 128 bit value.
*
* @return The least significant 64 bits of this UUID's 128 bit value
*/
public long getLeastSignificantBits() {
return value.getLeastSignificantBits();
}

/**
* Returns the most significant 64 bits of this UUID's 128 bit value.
*
* @return The most significant 64 bits of this UUID's 128 bit value
*/
public long getMostSignificantBits() {
return value.getMostSignificantBits();
}

public long timestamp() {
return value.timestamp();
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (other == null || getClass() != other.getClass()) {
return false;
}

ULID ulid = (ULID) other;

return value.equals(ulid.value);
}

@Override
public int hashCode() {
return Objects.hash(value);
}

public String toString() {
return value.toString();
}

@Override
public int compareTo(ULID other) {
return value.compareTo(other.value);
}
}

0 comments on commit dfc041e

Please sign in to comment.