Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from LinuxCounter/43_ulid
[#43] ULID support and example usage in JPA entity
- Loading branch information
Showing
13 changed files
with
646 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
82 changes: 82 additions & 0 deletions
82
common/ulid/src/main/java/io/github/linuxcounter/common/ulid/ULID.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.