Skip to content

Commit

Permalink
Rename StringCodec and BinaryCodec
Browse files Browse the repository at this point in the history
Changes:

* BinaryCodec -> StandardBinaryCodec
* StringCodec -> StandardStringCodec
  • Loading branch information
fabiolimace committed Jul 6, 2024
1 parent 1fbbe80 commit 77f69b7
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 79 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/github/f4b6a3/uuid/UuidCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

import com.github.f4b6a3.uuid.codec.BinaryCodec;
import com.github.f4b6a3.uuid.codec.StringCodec;
import com.github.f4b6a3.uuid.codec.StandardBinaryCodec;
import com.github.f4b6a3.uuid.codec.StandardStringCodec;
import com.github.f4b6a3.uuid.enums.UuidLocalDomain;
import com.github.f4b6a3.uuid.enums.UuidNamespace;
import com.github.f4b6a3.uuid.exception.InvalidUuidException;
Expand Down Expand Up @@ -135,7 +135,7 @@ public static UUID getMax() {
* @throws InvalidUuidException if the argument is invalid
*/
public static byte[] toBytes(final UUID uuid) {
return BinaryCodec.INSTANCE.encode(uuid);
return StandardBinaryCodec.INSTANCE.encode(uuid);
}

/**
Expand All @@ -148,7 +148,7 @@ public static byte[] toBytes(final UUID uuid) {
* @throws InvalidUuidException if the argument is invalid
*/
public static UUID fromBytes(byte[] uuid) {
return BinaryCodec.INSTANCE.decode(uuid);
return StandardBinaryCodec.INSTANCE.decode(uuid);
}

/**
Expand All @@ -161,7 +161,7 @@ public static UUID fromBytes(byte[] uuid) {
* @throws InvalidUuidException if the argument is invalid
*/
public static String toString(UUID uuid) {
return StringCodec.INSTANCE.encode(uuid);
return StandardStringCodec.INSTANCE.encode(uuid);
}

/**
Expand All @@ -184,7 +184,7 @@ public static String toString(UUID uuid) {
* @throws InvalidUuidException if the argument is invalid
*/
public static UUID fromString(String uuid) {
return StringCodec.INSTANCE.decode(uuid);
return StandardStringCodec.INSTANCE.decode(uuid);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
* Codec for UUID binary encoding as defined in the RFC-4122.
* <p>
* The UUID is encoded as 16 octets (bytes).
* <p>
* Read: https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-format
*/
public class BinaryCodec implements UuidCodec<byte[]> {
public class StandardBinaryCodec implements UuidCodec<byte[]> {

/**
* A shared immutable instance.
*/
public static final BinaryCodec INSTANCE = new BinaryCodec();
public static final StandardBinaryCodec INSTANCE = new StandardBinaryCodec();

/**
* Get an array of bytes from a UUID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,37 @@
import com.github.f4b6a3.uuid.util.internal.JavaVersionUtil;

/**
* Codec for UUID string representation as defined in RFC-4122.
* Codec for UUID canonical string as defined in RFC-4122.
* <p>
* The string representation, also referred as canonical textual representation,
* is a string of 32 hexadecimal (base-16) digits, displayed in five groups
* In the canonical textual representation, the 16 bytes of a UUID are
* represented as 32 hexadecimal (base-16) digits, displayed in five groups
* separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32
* hexadecimal characters and 4 hyphens).
* <p>
* This codec decodes (parses) strings in these formats, with/without hyphens:
* This codec decodes (parses) strings in these formats:
* <ul>
* <li>00000000-0000-V000-0000-000000000000 (canonical string)
* <li>{00000000-0000-V000-0000-000000000000} (MS GUID string)
* <li>urn:uuid:00000000-0000-V000-0000-000000000000 (URN UUID string)
* <li>000000000000V0000000000000000000 (hexadecimal string)
* <li>00000000-0000-0000-0000-000000000000 (THE canonical string)
* <li>{00000000-0000-0000-0000-000000000000} (Microsoft string)
* <li>urn:uuid:00000000-0000-0000-0000-000000000000 (URN string)
* </ul>
* <p>
* The encoding and decoding processes can be much faster (7x) than
* {@link UUID#toString()} and {@link UUID#fromString(String)} in JDK 8.
* <p>
* If you prefer a string representation without hyphens, use
* {@link Base16Codec} instead of {@link StringCodec}. {@link Base16Codec} can
* be much faster (22x) than doing
* {@link Base16Codec} instead of {@link StandardStringCodec}.
* {@link Base16Codec} can be much faster (22x) than doing
* <code>uuid.toString().replaceAll("-", "")</code>.
* <p>
* Read: https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-format
*/
public class StringCodec implements UuidCodec<String> {
public class StandardStringCodec implements UuidCodec<String> {

/**
* A shared immutable instance.
*/
public static final StringCodec INSTANCE = new StringCodec();
public static final StandardStringCodec INSTANCE = new StandardStringCodec();

private static final int DASH_POSITION_1 = 8;
private static final int DASH_POSITION_2 = 13;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/f4b6a3/uuid/codec/UriCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public UUID decode(URI uri) {
if (!isUuidUri(uri)) {
throw InvalidUuidException.newInstance(uri);
}
return StringCodec.INSTANCE.decode(uri.toString());
return StandardStringCodec.INSTANCE.decode(uri.toString());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/f4b6a3/uuid/codec/UrnCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class UrnCodec implements UuidCodec<String> {
@Override
public String encode(UUID uuid) {
UuidValidator.validate(uuid);
return URN_PREFIX + StringCodec.INSTANCE.encode(uuid);
return URN_PREFIX + StandardStringCodec.INSTANCE.encode(uuid);
}

/**
Expand All @@ -81,7 +81,7 @@ public UUID decode(String urn) {
if (!isUuidUrn(urn)) {
throw InvalidUuidException.newInstance(urn);
}
return StringCodec.INSTANCE.decode(urn);
return StandardStringCodec.INSTANCE.decode(urn);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Arrays;
import java.util.UUID;

import com.github.f4b6a3.uuid.codec.BinaryCodec;
import com.github.f4b6a3.uuid.codec.StandardBinaryCodec;
import com.github.f4b6a3.uuid.codec.UuidCodec;
import com.github.f4b6a3.uuid.codec.base.Base32Codec;
import com.github.f4b6a3.uuid.codec.base.Base64UrlCodec;
Expand Down Expand Up @@ -175,7 +175,7 @@ public String encode(UUID uuid) {
UuidValidator.validate(uuid);

int version = uuid.version();
byte[] bytes = BinaryCodec.INSTANCE.encode(uuid);
byte[] bytes = StandardBinaryCodec.INSTANCE.encode(uuid);
int[] ints = ByteUtil.toInts(bytes);

int variant = (ints[2] & 0xf0000000) >>> 24;
Expand All @@ -187,7 +187,7 @@ public String encode(UUID uuid) {
bytes = ByteUtil.fromInts(ints);
bytes[15] = (byte) ((bytes[15] & 0xff) >>> this.shift);

UUID uuuu = BinaryCodec.INSTANCE.decode(bytes);
UUID uuuu = StandardBinaryCodec.INSTANCE.decode(bytes);
String encoded = this.codec.encode(uuuu).substring(0, this.length - 1);

// if base is 64, use upper case version, else use lower case
Expand Down Expand Up @@ -222,7 +222,7 @@ public UUID decode(String ncname) {
String substring = ncname.substring(1, ncname.length());
UUID uuid = this.codec.decode(substring + padding);

byte[] bytes = BinaryCodec.INSTANCE.encode(uuid);
byte[] bytes = StandardBinaryCodec.INSTANCE.encode(uuid);
bytes[15] = (byte) ((bytes[15] & 0xff) << this.shift);

int[] ints = ByteUtil.toInts(bytes);
Expand All @@ -237,6 +237,6 @@ public UUID decode(String ncname) {

bytes = ByteUtil.fromInts(ints);

return BinaryCodec.INSTANCE.decode(bytes);
return StandardBinaryCodec.INSTANCE.decode(bytes);
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/github/f4b6a3/uuid/factory/UuidFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.Objects;
import java.util.UUID;

import com.github.f4b6a3.uuid.codec.BinaryCodec;
import com.github.f4b6a3.uuid.codec.StringCodec;
import com.github.f4b6a3.uuid.codec.StandardBinaryCodec;
import com.github.f4b6a3.uuid.codec.StandardStringCodec;
import com.github.f4b6a3.uuid.enums.UuidLocalDomain;
import com.github.f4b6a3.uuid.enums.UuidNamespace;
import com.github.f4b6a3.uuid.enums.UuidVersion;
Expand Down Expand Up @@ -354,7 +354,7 @@ protected static byte[] namespaceBytes(UuidNamespace namespace) {
*/
protected static byte[] namespaceBytes(UUID namespace) {
if (namespace != null) {
return BinaryCodec.INSTANCE.encode(namespace);
return StandardBinaryCodec.INSTANCE.encode(namespace);
}
return null; // the name space can be null
}
Expand All @@ -368,7 +368,7 @@ protected static byte[] namespaceBytes(UUID namespace) {
*/
protected static byte[] namespaceBytes(String namespace) {
if (namespace != null) {
return BinaryCodec.INSTANCE.encode(StringCodec.INSTANCE.decode(namespace));
return StandardBinaryCodec.INSTANCE.encode(StandardStringCodec.INSTANCE.decode(namespace));
}
return null; // the name space can be null
}
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/github/f4b6a3/uuid/codec/BinaryCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BinaryCodecTest {
@Test
public void testEncode() {

BinaryCodec codec = new BinaryCodec();
StandardBinaryCodec codec = new StandardBinaryCodec();

for (int i = 0; i < DEFAULT_LOOP_LIMIT; i++) {
// UuidCreator.toBytes();
Expand Down Expand Up @@ -44,7 +44,7 @@ public void testEncode() {
@Test
public void testDecode() {

BinaryCodec codec = new BinaryCodec();
StandardBinaryCodec codec = new StandardBinaryCodec();

for (int i = 0; i < DEFAULT_LOOP_LIMIT; i++) {
// UuidCreator.fromBytes();
Expand Down Expand Up @@ -72,7 +72,7 @@ public void testDecode() {
@Test
public void testEncodeAndDecode() {

final BinaryCodec codec = new BinaryCodec();
final StandardBinaryCodec codec = new StandardBinaryCodec();

for (int i = 0; i < DEFAULT_LOOP_LIMIT; i++) {
UUID uuid = UUID.randomUUID();
Expand All @@ -84,7 +84,7 @@ public void testEncodeAndDecode() {
@Test
public void testEncodeInvalidUuidException() {

BinaryCodec codec = new BinaryCodec();
StandardBinaryCodec codec = new StandardBinaryCodec();

{
try {
Expand Down Expand Up @@ -118,7 +118,7 @@ public void testEncodeInvalidUuidException() {
@Test
public void testDecodeInvalidUuidException() {

BinaryCodec codec = new BinaryCodec();
StandardBinaryCodec codec = new StandardBinaryCodec();

{
try {
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/com/github/f4b6a3/uuid/codec/StringCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class StringCodecTest {
@Test
public void testEncode() {

StringCodec codec = new StringCodec();
StandardStringCodec codec = new StandardStringCodec();

for (int i = 0; i < DEFAULT_LOOP_LIMIT; i++) {
// UuidCreator.toString();
Expand All @@ -45,7 +45,7 @@ public void testEncode() {
@Test
public void testDecode() {

StringCodec codec = new StringCodec();
StandardStringCodec codec = new StandardStringCodec();

for (int i = 0; i < DEFAULT_LOOP_LIMIT; i++) {
// UuidCreator.fromString();
Expand Down Expand Up @@ -100,7 +100,7 @@ public void testDecode() {
@Test
public void testEncodeAndDecode() {

final StringCodec codec = new StringCodec();
final StandardStringCodec codec = new StandardStringCodec();

for (int i = 0; i < DEFAULT_LOOP_LIMIT; i++) {
UUID uuid = UUID.randomUUID();
Expand All @@ -112,7 +112,7 @@ public void testEncodeAndDecode() {
@Test
public void testEncodeInvalidUuidException() {

StringCodec codec = new StringCodec();
StandardStringCodec codec = new StandardStringCodec();

{
try {
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testEncodeInvalidUuidException() {
@Test
public void testDecodeInvalidUuidException() {

StringCodec codec = new StringCodec();
StandardStringCodec codec = new StandardStringCodec();

try {
String string = new UUID(0L, 0L).toString();
Expand Down Expand Up @@ -232,19 +232,19 @@ public void testModifyString() {
{
// 00000000-0000-0000-0000-000000000000
String string = UUID.randomUUID().toString();
assertEquals(string, StringCodec.modify(string));
assertEquals(string, StandardStringCodec.modify(string));
}

{
// urn:uuid:00000000-0000-0000-0000-000000000000
String string = UUID.randomUUID().toString();
assertEquals(string, StringCodec.modify(URN_PREFIX + string));
assertEquals(string, StandardStringCodec.modify(URN_PREFIX + string));
}

{
// {00000000-0000-0000-0000-000000000000}
String string = UUID.randomUUID().toString();
assertEquals(string, StringCodec.modify("{" + string + "}"));
assertEquals(string, StandardStringCodec.modify("{" + string + "}"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.junit.Assert.*;
import org.junit.Test;

import com.github.f4b6a3.uuid.codec.BinaryCodec;
import com.github.f4b6a3.uuid.codec.StandardBinaryCodec;
import com.github.f4b6a3.uuid.codec.UuidCodec;
import com.github.f4b6a3.uuid.exception.InvalidUuidException;

Expand All @@ -16,7 +16,7 @@ public class BaseNCodec1Test {

private static final int DEFAULT_LOOP_LIMIT = 100;

private static final UuidCodec<byte[]> CODEC_BYTES = new BinaryCodec();
private static final UuidCodec<byte[]> CODEC_BYTES = new StandardBinaryCodec();

// Alphabet used by BigInteger.toString(32);
protected static final char[] ALPHABET_JAVA = "0123456789abcdefghijklmnopqrstuv".toCharArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import org.junit.Test;

import com.github.f4b6a3.uuid.codec.BinaryCodec;
import com.github.f4b6a3.uuid.codec.StandardBinaryCodec;
import com.github.f4b6a3.uuid.codec.base.Base62Codec;
import com.github.f4b6a3.uuid.codec.base.BaseN;

Expand All @@ -23,7 +23,7 @@ public void testDecode() {
for (int i = 0; i < 1000; i++) {
String string = getRandomString(Base62Codec.INSTANCE.getBase());
UUID uuid = Base62Codec.INSTANCE.decode(string);
byte[] bytes = BinaryCodec.INSTANCE.encode(uuid);
byte[] bytes = StandardBinaryCodec.INSTANCE.encode(uuid);
assertEquals(Arrays.toString(decode(Base62Codec.INSTANCE.getBase(), string)), Arrays.toString(bytes));
}
}
Expand Down Expand Up @@ -68,13 +68,13 @@ public void testMultiply() {
}

protected static long[] toLongs(byte[] bytes) {
UUID uuid = BinaryCodec.INSTANCE.decode(bytes);
UUID uuid = StandardBinaryCodec.INSTANCE.decode(bytes);
return new long[] { uuid.getMostSignificantBits(), uuid.getLeastSignificantBits() };
}

protected static byte[] fromLongs(long[] longs) {
UUID uuid = new UUID(longs[0], longs[1]);
return BinaryCodec.INSTANCE.encode(uuid);
return StandardBinaryCodec.INSTANCE.encode(uuid);
}

private String getRandomString(BaseN base) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.junit.Test;

import com.github.f4b6a3.uuid.codec.BinaryCodec;
import com.github.f4b6a3.uuid.codec.StandardBinaryCodec;
import com.github.f4b6a3.uuid.codec.base.Base62Codec;
import com.github.f4b6a3.uuid.codec.base.BaseN;
import com.github.f4b6a3.uuid.codec.base.BaseNCodec.CustomDivider;
Expand All @@ -27,7 +27,7 @@ public void testEncode() {
for (int i = 0; i < 1000; i++) {
byte[] bytes = new byte[UUID_BYTES];
(new Random(seeder.nextLong())).nextBytes(bytes);
UUID uuid = BinaryCodec.INSTANCE.decode(bytes);
UUID uuid = StandardBinaryCodec.INSTANCE.decode(bytes);
String string = Base62Codec.INSTANCE.encode(uuid);
assertEquals(encode(Base62Codec.INSTANCE.getBase(), bytes), string);
}
Expand Down
Loading

0 comments on commit 77f69b7

Please sign in to comment.