Skip to content

Commit 85b5b9a

Browse files
committed
initial commit
1 parent 21993bf commit 85b5b9a

File tree

13 files changed

+514
-1
lines changed

13 files changed

+514
-1
lines changed

.github/dependabot.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "maven" # See documentation for possible values
9+
directory: "/" # Location of package manifests
10+
schedule:
11+
interval: "daily"

.github/workflows/maven.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
4+
name: Java CI with Maven
5+
6+
on: [ push, pull_request ]
7+
8+
jobs:
9+
build:
10+
name: Build for JDK ${{ matrix.java }}
11+
if: ${{ github.event_name == 'push' || github.event.pull_request.head.repo.full_name != 'zrdj/java-identifiers' }}
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
java: [ 11, 17, 21 ]
16+
steps:
17+
- uses: actions/checkout@v2
18+
- uses: actions/cache@v1
19+
with:
20+
path: ~/.m2/repository
21+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
22+
restore-keys: |
23+
${{ runner.os }}-maven-
24+
- name: Set up JDK ${{ matrix.java }}
25+
uses: actions/setup-java@v2
26+
with:
27+
java-version: ${{ matrix.java }}
28+
distribution: 'temurin'
29+
cache: maven
30+
- name: Build with Maven
31+
run: mvn -B -U compile test --file pom.xml
32+
env:
33+
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}

.gitignore

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/**
8+
*.iws
9+
*.iml
10+
*.ipr
11+
12+
### Eclipse ###
13+
.apt_generated
14+
.classpath
15+
.factorypath
16+
.project
17+
.settings
18+
.springBeans
19+
.sts4-cache
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
build/
28+
!**/src/main/**/build/
29+
!**/src/test/**/build/
30+
31+
### VS Code ###
32+
.vscode/
33+
34+
### Mac OS ###
35+
.DS_Store

README.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
1+
[![License](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)]()
2+
[![](https://jitpack.io/v/ZrdJ/java-identifiers.svg)](https://jitpack.io/#ZrdJ/java-identifiers)
3+
![GitHub Workflow Status (branch)](https://github.com/zrdj/java-identifiers/actions/workflows/maven.yml/badge.svg)
4+
15
# java-identifiers
2-
Wrapper for using identifiers like UUID, Ulid, Ksid and Tsid
6+
7+
## Maven
8+
9+
Add the Jitpack repository to your build file
10+
11+
```xml
12+
13+
<repositories>
14+
<repository>
15+
<id>jitpack.io</id>
16+
<url>https://jitpack.io</url>
17+
</repository>
18+
</repositories>
19+
```
20+
21+
Release artifact
22+
23+
```xml
24+
25+
<dependency>
26+
<groupId>com.github.zrdj</groupId>
27+
<artifactId>java-identifiers</artifactId>
28+
<version>0.1.0</version>
29+
</dependency>
30+
```
31+
32+
## Usage
33+
34+
### Identifiers and Codecs
35+
36+
Use the static methods of `Identifiers.*` to create your desired IDs of UUID's, Ulid's, Tsid's or Ksuid's and convert
37+
them from or into a String using the methods of `Codecs.*` for choosing the desired codec.
38+
39+
```java
40+
public static void main(String[] args) {
41+
final UUID id = Identifiers.UUIDv7();
42+
final Codec<UUID> base32 = Codecs.UUIDBase32();
43+
System.out.println("id as base32: " + base32.encode(id));
44+
}
45+
```

pom.xml

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github.zrdj</groupId>
8+
<artifactId>java-identifiers</artifactId>
9+
<version>0.1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>3.12.1</version>
23+
<configuration>
24+
<source>${maven.compiler.source}</source>
25+
<target>${maven.compiler.target}</target>
26+
</configuration>
27+
</plugin>
28+
<!-- Sources -->
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-source-plugin</artifactId>
32+
<version>3.3.0</version>
33+
<executions>
34+
<execution>
35+
<id>attach-sources</id>
36+
<goals>
37+
<goal>jar-no-fork</goal>
38+
</goals>
39+
</execution>
40+
</executions>
41+
</plugin>
42+
43+
<!-- Javadoc -->
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-javadoc-plugin</artifactId>
47+
<version>3.6.3</version>
48+
<executions>
49+
<execution>
50+
<id>attach-javadocs</id>
51+
<goals>
52+
<goal>jar</goal>
53+
</goals>
54+
</execution>
55+
</executions>
56+
</plugin>
57+
58+
</plugins>
59+
</build>
60+
61+
<dependencies>
62+
<dependency>
63+
<groupId>com.github.f4b6a3</groupId>
64+
<artifactId>uuid-creator</artifactId>
65+
<version>5.3.7</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.github.f4b6a3</groupId>
69+
<artifactId>ulid-creator</artifactId>
70+
<version>5.2.3</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>com.github.f4b6a3</groupId>
74+
<artifactId>tsid-creator</artifactId>
75+
<version>5.2.6</version>
76+
</dependency>
77+
<dependency>
78+
<groupId>com.github.f4b6a3</groupId>
79+
<artifactId>ksuid-creator</artifactId>
80+
<version>4.1.1</version>
81+
</dependency>
82+
</dependencies>
83+
84+
85+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.zrdj.java.identifiers;
2+
3+
public interface Codec<ID> {
4+
String encode(ID identifier);
5+
6+
ID decode(String type);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.zrdj.java.identifiers;
2+
3+
import com.github.f4b6a3.ksuid.Ksuid;
4+
import com.github.f4b6a3.tsid.Tsid;
5+
import com.github.f4b6a3.ulid.Ulid;
6+
import com.github.zrdj.java.identifiers.codecs.KsuidCodec;
7+
import com.github.zrdj.java.identifiers.codecs.TsidCodec;
8+
import com.github.zrdj.java.identifiers.codecs.UUIDCodec;
9+
import com.github.zrdj.java.identifiers.codecs.UlidCodec;
10+
11+
import java.util.UUID;
12+
13+
public interface Codecs {
14+
static Codec<UUID> UUIDBase32() {
15+
return UUIDCodec.Base32;
16+
}
17+
18+
static Codec<Ulid> UlidBase32() {
19+
return UlidCodec.Base32Crockford;
20+
}
21+
22+
static Codec<Tsid> TsidBase32() {
23+
return TsidCodec.Base32Crockford;
24+
}
25+
26+
static Codec<Ksuid> KsuidBase62() {
27+
return KsuidCodec.Base62;
28+
}
29+
30+
static Codec<UUID> UUIDBase16() {
31+
return UUIDCodec.Base16;
32+
}
33+
34+
static Codec<UUID> UUIDBase62() {
35+
return UUIDCodec.Base62;
36+
}
37+
38+
static Codec<UUID> UUIDBase64() {
39+
return UUIDCodec.Base64;
40+
}
41+
42+
static Codec<UUID> UUIDBase64Url() {
43+
return UUIDCodec.Base64Url;
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.github.zrdj.java.identifiers;
2+
3+
import com.github.f4b6a3.ksuid.Ksuid;
4+
import com.github.f4b6a3.ksuid.KsuidCreator;
5+
import com.github.f4b6a3.tsid.Tsid;
6+
import com.github.f4b6a3.tsid.TsidCreator;
7+
import com.github.f4b6a3.ulid.Ulid;
8+
import com.github.f4b6a3.ulid.UlidCreator;
9+
import com.github.f4b6a3.uuid.UuidCreator;
10+
import com.github.f4b6a3.uuid.enums.UuidLocalDomain;
11+
import com.github.f4b6a3.uuid.enums.UuidNamespace;
12+
13+
import java.util.UUID;
14+
15+
public interface Identifiers {
16+
static Ksuid Ksuid() {
17+
return KsuidCreator.getKsuid();
18+
}
19+
20+
static Tsid Tsid() {
21+
return TsidCreator.getTsid();
22+
}
23+
24+
static Ulid Ulid() {
25+
return UlidCreator.getUlid();
26+
}
27+
28+
static UUID UUIDv1() {
29+
return UuidCreator.getTimeBased();
30+
}
31+
32+
static UUID UUIDv2(final UuidLocalDomain domain, final int identifier) {
33+
return UuidCreator.getDceSecurity(domain, identifier);
34+
}
35+
36+
static UUID UUIDv3(final UuidNamespace namespace, final String name) {
37+
return UuidCreator.getNameBasedMd5(namespace, name);
38+
}
39+
40+
static UUID UUIDv4() {
41+
return UuidCreator.getRandomBased();
42+
}
43+
44+
static UUID UUIDv5(final UuidNamespace namespace, final String name) {
45+
return UuidCreator.getNameBasedSha1(namespace, name);
46+
}
47+
48+
static UUID UUIDv6() {
49+
return UuidCreator.getTimeOrdered();
50+
}
51+
52+
static UUID UUIDv7() {
53+
return UuidCreator.getTimeOrderedEpoch();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.zrdj.java.identifiers.codecs;
2+
3+
import com.github.f4b6a3.ksuid.Ksuid;
4+
import com.github.zrdj.java.identifiers.Codec;
5+
6+
public enum KsuidCodec implements Codec<Ksuid> {
7+
Base62(new Codec<>() {
8+
@Override
9+
public String encode(Ksuid identifier) {
10+
return identifier.toString();
11+
}
12+
13+
@Override
14+
public Ksuid decode(String text) {
15+
return Ksuid.from(text);
16+
}
17+
}),
18+
;
19+
private final Codec<Ksuid> _codec;
20+
21+
KsuidCodec(final Codec<Ksuid> codec) {
22+
_codec = codec;
23+
}
24+
25+
@Override
26+
public String encode(final Ksuid uuid) {
27+
return _codec.encode(uuid);
28+
}
29+
30+
@Override
31+
public Ksuid decode(final String value) {
32+
return _codec.decode(value);
33+
}
34+
35+
public boolean isValid(final String value) {
36+
if (value == null || value.trim().isEmpty()) {
37+
return false;
38+
}
39+
try {
40+
decode(value);
41+
return true;
42+
} catch (Exception ignored) {
43+
return false;
44+
}
45+
}
46+
47+
}

0 commit comments

Comments
 (0)