diff --git a/pom.xml b/pom.xml
index 5bb93d1a..b352432a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -38,6 +38,24 @@
4.13.1
test
+
+ org.mockito
+ mockito-inline
+ 3.4.0
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter
+ 5.8.1
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ 5.10.2
+ test
+
diff --git a/src/main/java/com/github/f4b6a3/uuid/alt/GUID.java b/src/main/java/com/github/f4b6a3/uuid/alt/GUID.java
index 94b76d07..1c46de2f 100644
--- a/src/main/java/com/github/f4b6a3/uuid/alt/GUID.java
+++ b/src/main/java/com/github/f4b6a3/uuid/alt/GUID.java
@@ -552,114 +552,4 @@ long getMostSignificantBits() {
long getLeastSignificantBits() {
return this.lsb;
}
-
- static final class Parser {
-
- private static final byte[] VALUES = new byte[256];
- static {
- Arrays.fill(VALUES, (byte) -1);
- VALUES['0'] = 0;
- VALUES['1'] = 1;
- VALUES['2'] = 2;
- VALUES['3'] = 3;
- VALUES['4'] = 4;
- VALUES['5'] = 5;
- VALUES['6'] = 6;
- VALUES['7'] = 7;
- VALUES['8'] = 8;
- VALUES['9'] = 9;
- VALUES['A'] = 10;
- VALUES['B'] = 11;
- VALUES['C'] = 12;
- VALUES['D'] = 13;
- VALUES['E'] = 14;
- VALUES['F'] = 15;
- VALUES['a'] = 10;
- VALUES['b'] = 11;
- VALUES['c'] = 12;
- VALUES['d'] = 13;
- VALUES['e'] = 14;
- VALUES['f'] = 15;
- }
-
- static GUID parse(final String string) {
-
- if (!valid(string)) {
- throw new IllegalArgumentException("Invalid GUID string: " + string);
- }
-
- long msb = 0;
- long lsb = 0;
-
- // UUID string WITH hyphen
- msb |= (long) VALUES[string.charAt(0x00)] << 60;
- msb |= (long) VALUES[string.charAt(0x01)] << 56;
- msb |= (long) VALUES[string.charAt(0x02)] << 52;
- msb |= (long) VALUES[string.charAt(0x03)] << 48;
- msb |= (long) VALUES[string.charAt(0x04)] << 44;
- msb |= (long) VALUES[string.charAt(0x05)] << 40;
- msb |= (long) VALUES[string.charAt(0x06)] << 36;
- msb |= (long) VALUES[string.charAt(0x07)] << 32;
- // input[8] = '-'
- msb |= (long) VALUES[string.charAt(0x09)] << 28;
- msb |= (long) VALUES[string.charAt(0x0a)] << 24;
- msb |= (long) VALUES[string.charAt(0x0b)] << 20;
- msb |= (long) VALUES[string.charAt(0x0c)] << 16;
- // input[13] = '-'
- msb |= (long) VALUES[string.charAt(0x0e)] << 12;
- msb |= (long) VALUES[string.charAt(0x0f)] << 8;
- msb |= (long) VALUES[string.charAt(0x10)] << 4;
- msb |= (long) VALUES[string.charAt(0x11)];
- // input[18] = '-'
- lsb |= (long) VALUES[string.charAt(0x13)] << 60;
- lsb |= (long) VALUES[string.charAt(0x14)] << 56;
- lsb |= (long) VALUES[string.charAt(0x15)] << 52;
- lsb |= (long) VALUES[string.charAt(0x16)] << 48;
- // input[23] = '-'
- lsb |= (long) VALUES[string.charAt(0x18)] << 44;
- lsb |= (long) VALUES[string.charAt(0x19)] << 40;
- lsb |= (long) VALUES[string.charAt(0x1a)] << 36;
- lsb |= (long) VALUES[string.charAt(0x1b)] << 32;
- lsb |= (long) VALUES[string.charAt(0x1c)] << 28;
- lsb |= (long) VALUES[string.charAt(0x1d)] << 24;
- lsb |= (long) VALUES[string.charAt(0x1e)] << 20;
- lsb |= (long) VALUES[string.charAt(0x1f)] << 16;
- lsb |= (long) VALUES[string.charAt(0x20)] << 12;
- lsb |= (long) VALUES[string.charAt(0x21)] << 8;
- lsb |= (long) VALUES[string.charAt(0x22)] << 4;
- lsb |= (long) VALUES[string.charAt(0x23)];
-
- return new GUID(msb, lsb);
- }
-
- static boolean valid(final String string) {
-
- if (string == null || string.length() != GUID_CHARS) {
- return false; // null or wrong characters count!
- }
-
- int dashes = 0;
-
- for (int i = 0; i < GUID_CHARS; i++) {
- char chr = string.charAt(i);
- int val = (int) VALUES[chr];
- if (val >= 0) {
- continue; // passed
- }
- if (i == 8 || i == 13 || i == 18 || i == 23) {
- if (chr == '-') {
- dashes++;
- continue; // passed
- }
- }
- return false; // oops! invalid character!
- }
-
- if (dashes != 4) {
- return false; // wrong dashes count!
- }
-
- return true; // it seems to be OK
- }
- }
}
diff --git a/src/main/java/com/github/f4b6a3/uuid/alt/Parser.java b/src/main/java/com/github/f4b6a3/uuid/alt/Parser.java
new file mode 100644
index 00000000..ac76d6e8
--- /dev/null
+++ b/src/main/java/com/github/f4b6a3/uuid/alt/Parser.java
@@ -0,0 +1,121 @@
+package com.github.f4b6a3.uuid.alt;
+
+import java.util.Arrays;
+
+import static com.github.f4b6a3.uuid.alt.GUID.GUID_CHARS;
+
+public final class Parser {
+
+ private static final byte[] VALUES = new byte[256];
+ static {
+ Arrays.fill(VALUES, (byte) -1);
+ VALUES['0'] = 0;
+ VALUES['1'] = 1;
+ VALUES['2'] = 2;
+ VALUES['3'] = 3;
+ VALUES['4'] = 4;
+ VALUES['5'] = 5;
+ VALUES['6'] = 6;
+ VALUES['7'] = 7;
+ VALUES['8'] = 8;
+ VALUES['9'] = 9;
+ VALUES['A'] = 10;
+ VALUES['B'] = 11;
+ VALUES['C'] = 12;
+ VALUES['D'] = 13;
+ VALUES['E'] = 14;
+ VALUES['F'] = 15;
+ VALUES['a'] = 10;
+ VALUES['b'] = 11;
+ VALUES['c'] = 12;
+ VALUES['d'] = 13;
+ VALUES['e'] = 14;
+ VALUES['f'] = 15;
+ }
+
+ public static GUID parse(final String string) {
+
+ if (!valid(string)) {
+ throw new IllegalArgumentException("Invalid GUID string: " + string);
+ }
+
+ long msb = 0;
+ long lsb = 0;
+
+ // UUID string WITH hyphen
+ msb |= (long) VALUES[string.charAt(0x00)] << 60;
+ msb |= (long) VALUES[string.charAt(0x01)] << 56;
+ msb |= (long) VALUES[string.charAt(0x02)] << 52;
+ msb |= (long) VALUES[string.charAt(0x03)] << 48;
+ msb |= (long) VALUES[string.charAt(0x04)] << 44;
+ msb |= (long) VALUES[string.charAt(0x05)] << 40;
+ msb |= (long) VALUES[string.charAt(0x06)] << 36;
+ msb |= (long) VALUES[string.charAt(0x07)] << 32;
+ // input[8] = '-'
+ msb |= (long) VALUES[string.charAt(0x09)] << 28;
+ msb |= (long) VALUES[string.charAt(0x0a)] << 24;
+ msb |= (long) VALUES[string.charAt(0x0b)] << 20;
+ msb |= (long) VALUES[string.charAt(0x0c)] << 16;
+ // input[13] = '-'
+ msb |= (long) VALUES[string.charAt(0x0e)] << 12;
+ msb |= (long) VALUES[string.charAt(0x0f)] << 8;
+ msb |= (long) VALUES[string.charAt(0x10)] << 4;
+ msb |= (long) VALUES[string.charAt(0x11)];
+ // input[18] = '-'
+ lsb |= (long) VALUES[string.charAt(0x13)] << 60;
+ lsb |= (long) VALUES[string.charAt(0x14)] << 56;
+ lsb |= (long) VALUES[string.charAt(0x15)] << 52;
+ lsb |= (long) VALUES[string.charAt(0x16)] << 48;
+ // input[23] = '-'
+ lsb |= (long) VALUES[string.charAt(0x18)] << 44;
+ lsb |= (long) VALUES[string.charAt(0x19)] << 40;
+ lsb |= (long) VALUES[string.charAt(0x1a)] << 36;
+ lsb |= (long) VALUES[string.charAt(0x1b)] << 32;
+ lsb |= (long) VALUES[string.charAt(0x1c)] << 28;
+ lsb |= (long) VALUES[string.charAt(0x1d)] << 24;
+ lsb |= (long) VALUES[string.charAt(0x1e)] << 20;
+ lsb |= (long) VALUES[string.charAt(0x1f)] << 16;
+ lsb |= (long) VALUES[string.charAt(0x20)] << 12;
+ lsb |= (long) VALUES[string.charAt(0x21)] << 8;
+ lsb |= (long) VALUES[string.charAt(0x22)] << 4;
+ lsb |= (long) VALUES[string.charAt(0x23)];
+
+ return new GUID(msb, lsb);
+ }
+
+ private static final int[] DASH_POSITIONS = {8, 13, 18, 23};
+ private static final int DASH_REQUIRED_COUNT = 4;
+
+ private static boolean isCharValid(char chr) {
+ return chr >= 0 && chr < VALUES.length && VALUES[chr] >= 0;
+ }
+ private static boolean isDashPosition(int index) {
+ for (int dashPosition : DASH_POSITIONS) {
+ if (index == dashPosition) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static boolean valid(final String guid) {
+ if (guid == null || guid.length() != GUID_CHARS) {
+ return false; // null or wrong length
+ }
+
+ int dashesCount = 0;
+ for (int i = 0; i < GUID_CHARS; i++) {
+ char chr = guid.charAt(i);
+ if (isCharValid(chr)) {
+ continue; // character is valid
+ }
+ if (isDashPosition(i) && chr == '-') {
+ dashesCount++;
+ continue;
+ }
+ return false; // invalid character
+ }
+
+ return dashesCount == DASH_REQUIRED_COUNT;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/github/f4b6a3/uuid/codec/StringCodec.java b/src/main/java/com/github/f4b6a3/uuid/codec/StringCodec.java
index db7469c7..401e2f2a 100644
--- a/src/main/java/com/github/f4b6a3/uuid/codec/StringCodec.java
+++ b/src/main/java/com/github/f4b6a3/uuid/codec/StringCodec.java
@@ -31,6 +31,7 @@
import com.github.f4b6a3.uuid.util.UuidValidator;
import com.github.f4b6a3.uuid.util.immutable.CharArray;
import com.github.f4b6a3.uuid.util.immutable.LongArray;
+import com.github.f4b6a3.uuid.util.internal.JavaVersionUtil;
/**
* Codec for UUID string representation as defined in RFC-4122.
@@ -66,7 +67,7 @@ public class StringCodec implements UuidCodec {
private static final CharArray ALPHABET = Base16Codec.INSTANCE.getBase().getAlphabet();
private static final String URN_PREFIX = "urn:uuid:";
- private static final boolean JAVA_VERSION_GREATER_THAN_8 = getJavaVersion() > 8;
+ private static final boolean JAVA_VERSION_GREATER_THAN_8 = JavaVersionUtil.getJavaVersion() > 8;
/**
* Get a string from a UUID.
@@ -270,31 +271,4 @@ protected static char[] toCharArray(String string) {
return chars;
}
-
- /**
- * Returns the java major version number.
- *
- * @see JDK Releases
- * @return major version number
- */
- protected static int getJavaVersion() {
- try {
-
- String property = System.getProperty("java.version");
-
- if (property != null) {
- String[] version = property.split("\\.");
- if (version[0].equals("1")) {
- return Integer.parseInt(version[1]);
- } else {
- return Integer.parseInt(version[0]);
- }
- } else {
- return 8;
- }
-
- } catch (NullPointerException | NumberFormatException | IndexOutOfBoundsException e) {
- return 8;
- }
- }
}
diff --git a/src/main/java/com/github/f4b6a3/uuid/codec/base/BaseN.java b/src/main/java/com/github/f4b6a3/uuid/codec/base/BaseN.java
index c6075ae5..7d3e0dd0 100644
--- a/src/main/java/com/github/f4b6a3/uuid/codec/base/BaseN.java
+++ b/src/main/java/com/github/f4b6a3/uuid/codec/base/BaseN.java
@@ -328,14 +328,21 @@ protected static char[] expand(char a, char b) {
}
private static char[] expand(char a, char b, char min, char max) {
-
- if ((a > b) || !(a >= min && a <= max && b >= min && b <= max)) {
+ if (!isValidRange(a, b, min, max)) {
return new char[0];
}
- char[] buffer = new char[(b - a) + 1];
+ return fillRange(a, b);
+ }
+
+ private static boolean isValidRange(char start, char end, char min, char max) {
+ return start <= end && start >= min && end <= max;
+ }
+
+ private static char[] fillRange(char start, char end) {
+ char[] buffer = new char[(end - start) + 1];
for (int i = 0; i < buffer.length; i++) {
- buffer[i] = (char) (a + i);
+ buffer[i] = (char) (start + i);
}
return buffer;
}
diff --git a/src/main/java/com/github/f4b6a3/uuid/codec/other/NcnameCodec.java b/src/main/java/com/github/f4b6a3/uuid/codec/other/NcnameCodec.java
index 894caf9b..de3f9341 100644
--- a/src/main/java/com/github/f4b6a3/uuid/codec/other/NcnameCodec.java
+++ b/src/main/java/com/github/f4b6a3/uuid/codec/other/NcnameCodec.java
@@ -35,6 +35,7 @@
import com.github.f4b6a3.uuid.util.UuidValidator;
import com.github.f4b6a3.uuid.util.immutable.CharArray;
import com.github.f4b6a3.uuid.util.immutable.LongArray;
+import com.github.f4b6a3.uuid.util.internal.ByteUtil;
/**
* Codec for UUID NCNames.
@@ -174,7 +175,7 @@ public String encode(UUID uuid) {
int version = uuid.version();
byte[] bytes = BinaryCodec.INSTANCE.encode(uuid);
- int[] ints = toInts(bytes);
+ int[] ints = ByteUtil.toInts(bytes);
int variant = (ints[2] & 0xf0000000) >>> 24;
@@ -182,7 +183,7 @@ public String encode(UUID uuid) {
ints[2] = (ints[2] & 0x00ffffff) << 8 | (ints[3] >>> 24);
ints[3] = (ints[3] << 8) | variant;
- bytes = fromInts(ints);
+ bytes = ByteUtil.fromInts(ints);
bytes[15] = (byte) ((bytes[15] & 0xff) >>> this.shift);
UUID uuuu = BinaryCodec.INSTANCE.decode(bytes);
@@ -223,7 +224,7 @@ public UUID decode(String ncname) {
byte[] bytes = BinaryCodec.INSTANCE.encode(uuid);
bytes[15] = (byte) ((bytes[15] & 0xff) << this.shift);
- int[] ints = toInts(bytes);
+ int[] ints = ByteUtil.toInts(bytes);
int variant = (ints[3] & 0xf0) << 24;
@@ -233,50 +234,8 @@ public UUID decode(String ncname) {
ints[2] |= ((ints[1] & 0xf) << 24) | variant;
ints[1] = (ints[1] & 0xffff0000) | (version << 12) | ((ints[1] >>> 4) & 0xfff);
- bytes = fromInts(ints);
+ bytes = ByteUtil.fromInts(ints);
return BinaryCodec.INSTANCE.decode(bytes);
}
-
- private static int[] toInts(byte[] bytes) {
- int[] ints = new int[4];
- ints[0] |= (bytes[0x0] & 0xff) << 24;
- ints[0] |= (bytes[0x1] & 0xff) << 16;
- ints[0] |= (bytes[0x2] & 0xff) << 8;
- ints[0] |= (bytes[0x3] & 0xff);
- ints[1] |= (bytes[0x4] & 0xff) << 24;
- ints[1] |= (bytes[0x5] & 0xff) << 16;
- ints[1] |= (bytes[0x6] & 0xff) << 8;
- ints[1] |= (bytes[0x7] & 0xff);
- ints[2] |= (bytes[0x8] & 0xff) << 24;
- ints[2] |= (bytes[0x9] & 0xff) << 16;
- ints[2] |= (bytes[0xa] & 0xff) << 8;
- ints[2] |= (bytes[0xb] & 0xff);
- ints[3] |= (bytes[0xc] & 0xff) << 24;
- ints[3] |= (bytes[0xd] & 0xff) << 16;
- ints[3] |= (bytes[0xe] & 0xff) << 8;
- ints[3] |= (bytes[0xf] & 0xff);
- return ints;
- }
-
- private static byte[] fromInts(int[] ints) {
- byte[] bytes = new byte[16];
- bytes[0x0] = (byte) (ints[0] >>> 24);
- bytes[0x1] = (byte) (ints[0] >>> 16);
- bytes[0x2] = (byte) (ints[0] >>> 8);
- bytes[0x3] = (byte) (ints[0]);
- bytes[0x4] = (byte) (ints[1] >>> 24);
- bytes[0x5] = (byte) (ints[1] >>> 16);
- bytes[0x6] = (byte) (ints[1] >>> 8);
- bytes[0x7] = (byte) (ints[1]);
- bytes[0x8] = (byte) (ints[2] >>> 24);
- bytes[0x9] = (byte) (ints[2] >>> 16);
- bytes[0xa] = (byte) (ints[2] >>> 8);
- bytes[0xb] = (byte) (ints[2]);
- bytes[0xc] = (byte) (ints[3] >>> 24);
- bytes[0xd] = (byte) (ints[3] >>> 16);
- bytes[0xe] = (byte) (ints[3] >>> 8);
- bytes[0xf] = (byte) (ints[3]);
- return bytes;
- }
}
diff --git a/src/main/java/com/github/f4b6a3/uuid/codec/other/TimeOrderedCodec.java b/src/main/java/com/github/f4b6a3/uuid/codec/other/TimeOrderedCodec.java
index da2e74ad..d775b516 100644
--- a/src/main/java/com/github/f4b6a3/uuid/codec/other/TimeOrderedCodec.java
+++ b/src/main/java/com/github/f4b6a3/uuid/codec/other/TimeOrderedCodec.java
@@ -88,10 +88,13 @@ public UUID decode(UUID uuid) {
long timestamp = UuidUtil.getTimestamp(uuid);
- long msb = ((timestamp & 0x0fff_0000_00000000L) >>> 48) //
- | ((timestamp & 0x0000_ffff_00000000L) >>> 16) //
- | ((timestamp & 0x0000_0000_ffffffffL) << 32) //
- | 0x0000000000001000L; // set version 1
+ long timeHigh = (timestamp & 0x0fff_0000_00000000L) >>> 48;
+ long timeMid = (timestamp & 0x0000_ffff_00000000L) >>> 16;
+ long timeLow = (timestamp & 0x0000_0000_ffffffffL) << 32;
+ long version = 0x0000000000001000L; // Set version 1
+
+ // Combine the parts to form the Most Significant Bits (MSB)
+ long msb = timeHigh | timeMid | timeLow | version;
long lsb = uuid.getLeastSignificantBits();
diff --git a/src/main/java/com/github/f4b6a3/uuid/util/MachineId.java b/src/main/java/com/github/f4b6a3/uuid/util/MachineId.java
index 2b0bb599..cabb1e2b 100644
--- a/src/main/java/com/github/f4b6a3/uuid/util/MachineId.java
+++ b/src/main/java/com/github/f4b6a3/uuid/util/MachineId.java
@@ -84,9 +84,9 @@ public static long getMachineId() {
public static UUID getMachineUuid() {
if (uuid == null) {
final byte[] bytes = getMachineHash();
- final long msb = toNumber(bytes, 0, 8);
- final long lsb = toNumber(bytes, 8, 16);
- uuid = setVersion(new UUID(msb, lsb), 4);
+ final long mostSigBits = toNumber(bytes, 0, 8);
+ final long leastSigBits = toNumber(bytes, 8, 16);
+ uuid = setVersion(new UUID(mostSigBits, leastSigBits), 4);
}
return uuid;
}
diff --git a/src/main/java/com/github/f4b6a3/uuid/util/UuidValidator.java b/src/main/java/com/github/f4b6a3/uuid/util/UuidValidator.java
index a85868c6..470bba14 100644
--- a/src/main/java/com/github/f4b6a3/uuid/util/UuidValidator.java
+++ b/src/main/java/com/github/f4b6a3/uuid/util/UuidValidator.java
@@ -238,6 +238,10 @@ public static void validate(final char[] uuid, int version) {
}
}
+ private static final int[] DASH_POSITIONS = {8, 13, 18, 23};
+ private static final int WITH_DASH_UUID_LENGTH = 36;
+ private static final int WITHOUT_DASH_UUID_LENGTH = 32;
+ private static final int MAX_DASH_COUNT = 4;
/**
* Checks if the UUID char array can be parsed.
*
@@ -245,24 +249,23 @@ public static void validate(final char[] uuid, int version) {
* @return true if valid, false if invalid
*/
protected static boolean isParseable(final char[] chars) {
-
- int hyphens = 0;
+ int dashCount = 0;
for (int i = 0; i < chars.length; i++) {
if (MAP.get(chars[i]) == -1) {
if (chars[i] == '-') {
- hyphens++;
+ dashCount++;
continue;
}
return false; // invalid character!
}
}
- if (chars.length == 36 && hyphens == 4) {
+ if (chars.length == WITH_DASH_UUID_LENGTH && dashCount == MAX_DASH_COUNT) {
// check if the hyphens positions are correct
- return chars[8] == '-' && chars[13] == '-' && chars[18] == '-' && chars[23] == '-';
+ return chars[DASH_POSITIONS[0]] == '-' && chars[DASH_POSITIONS[1]] == '-' && chars[DASH_POSITIONS[2]] == '-' && chars[DASH_POSITIONS[3]] == '-';
}
- return chars.length == 32 && hyphens == 0;
+ return chars.length == WITHOUT_DASH_UUID_LENGTH && dashCount == 0;
}
/**
diff --git a/src/main/java/com/github/f4b6a3/uuid/util/internal/ByteUtil.java b/src/main/java/com/github/f4b6a3/uuid/util/internal/ByteUtil.java
index 7fb661f4..0f706f2e 100644
--- a/src/main/java/com/github/f4b6a3/uuid/util/internal/ByteUtil.java
+++ b/src/main/java/com/github/f4b6a3/uuid/util/internal/ByteUtil.java
@@ -91,4 +91,63 @@ private static char toHexChar(final int number) {
}
return 0;
}
+
+ /**
+ * Converts an array of bytes into an array of integers. Each integer is formed by combining 4 bytes
+ * from the input array. This method assumes that the input byte array is at least 16 bytes long.
+ * The conversion is done by treating each set of 4 bytes as a single integer, with the first byte being the most significant.
+ *
+ * @param bytes An array of bytes to be converted into integers. This array should be at least 16 bytes long.
+ * @return An array of 4 integers, where each integer is formed by combining 4 bytes from the input array.
+ */
+ public static int[] toInts(byte[] bytes) {
+ int[] ints = new int[4];
+ ints[0] |= (bytes[0x0] & 0xff) << 24;
+ ints[0] |= (bytes[0x1] & 0xff) << 16;
+ ints[0] |= (bytes[0x2] & 0xff) << 8;
+ ints[0] |= (bytes[0x3] & 0xff);
+ ints[1] |= (bytes[0x4] & 0xff) << 24;
+ ints[1] |= (bytes[0x5] & 0xff) << 16;
+ ints[1] |= (bytes[0x6] & 0xff) << 8;
+ ints[1] |= (bytes[0x7] & 0xff);
+ ints[2] |= (bytes[0x8] & 0xff) << 24;
+ ints[2] |= (bytes[0x9] & 0xff) << 16;
+ ints[2] |= (bytes[0xa] & 0xff) << 8;
+ ints[2] |= (bytes[0xb] & 0xff);
+ ints[3] |= (bytes[0xc] & 0xff) << 24;
+ ints[3] |= (bytes[0xd] & 0xff) << 16;
+ ints[3] |= (bytes[0xe] & 0xff) << 8;
+ ints[3] |= (bytes[0xf] & 0xff);
+ return ints;
+ }
+
+ /**
+ * Converts an array of integers into an array of bytes. Each integer is decomposed into 4 bytes,
+ * with the most significant byte being placed first. This method produces a byte array of length 16,
+ * assuming the input array contains exactly 4 integers. The conversion is performed by shifting
+ * and masking operations to extract each byte from the integers.
+ *
+ * @param ints An array of integers to be converted into bytes. This array should contain exactly 4 integers.
+ * @return A byte array of length 16, where each group of 4 bytes represents one of the integers from the input array.
+ */
+ public static byte[] fromInts(int[] ints) {
+ byte[] bytes = new byte[16];
+ bytes[0x0] = (byte) (ints[0] >>> 24);
+ bytes[0x1] = (byte) (ints[0] >>> 16);
+ bytes[0x2] = (byte) (ints[0] >>> 8);
+ bytes[0x3] = (byte) (ints[0]);
+ bytes[0x4] = (byte) (ints[1] >>> 24);
+ bytes[0x5] = (byte) (ints[1] >>> 16);
+ bytes[0x6] = (byte) (ints[1] >>> 8);
+ bytes[0x7] = (byte) (ints[1]);
+ bytes[0x8] = (byte) (ints[2] >>> 24);
+ bytes[0x9] = (byte) (ints[2] >>> 16);
+ bytes[0xa] = (byte) (ints[2] >>> 8);
+ bytes[0xb] = (byte) (ints[2]);
+ bytes[0xc] = (byte) (ints[3] >>> 24);
+ bytes[0xd] = (byte) (ints[3] >>> 16);
+ bytes[0xe] = (byte) (ints[3] >>> 8);
+ bytes[0xf] = (byte) (ints[3]);
+ return bytes;
+ }
}
diff --git a/src/main/java/com/github/f4b6a3/uuid/util/internal/JavaVersionUtil.java b/src/main/java/com/github/f4b6a3/uuid/util/internal/JavaVersionUtil.java
new file mode 100644
index 00000000..41985b06
--- /dev/null
+++ b/src/main/java/com/github/f4b6a3/uuid/util/internal/JavaVersionUtil.java
@@ -0,0 +1,55 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2018-2022 Fabio Lima
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.github.f4b6a3.uuid.util.internal;
+
+public class JavaVersionUtil {
+
+ /**
+ * Returns the java major version number.
+ *
+ * @see JDK Releases
+ * @return major version number
+ */
+ public static int getJavaVersion() {
+ try {
+
+ String property = System.getProperty("java.version");
+
+ if (property != null) {
+ String[] version = property.split("\\.");
+ if (version[0].equals("1")) {
+ return Integer.parseInt(version[1]);
+ } else {
+ return Integer.parseInt(version[0]);
+ }
+ } else {
+ return 8;
+ }
+
+ } catch (NullPointerException | NumberFormatException | IndexOutOfBoundsException e) {
+ return 8;
+ }
+ }
+}
diff --git a/src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java b/src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java
index ec35a44c..084ffded 100644
--- a/src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java
+++ b/src/test/java/com/github/f4b6a3/uuid/alt/GUIDTest.java
@@ -13,7 +13,6 @@
import org.junit.Test;
-import com.github.f4b6a3.uuid.alt.GUID.Parser;
import com.github.f4b6a3.uuid.codec.other.TimeOrderedCodec;
import com.github.f4b6a3.uuid.util.UuidTime;
import com.github.f4b6a3.uuid.util.UuidUtil;
diff --git a/src/test/java/com/github/f4b6a3/uuid/codec/StringCodecTest.java b/src/test/java/com/github/f4b6a3/uuid/codec/StringCodecTest.java
index e8089e98..e343684c 100644
--- a/src/test/java/com/github/f4b6a3/uuid/codec/StringCodecTest.java
+++ b/src/test/java/com/github/f4b6a3/uuid/codec/StringCodecTest.java
@@ -1,6 +1,8 @@
package com.github.f4b6a3.uuid.codec;
import static org.junit.Assert.*;
+
+import com.github.f4b6a3.uuid.util.internal.JavaVersionUtil;
import org.junit.Test;
import com.github.f4b6a3.uuid.UuidCreator;
@@ -256,37 +258,37 @@ public void testGetJavaVersion() {
final String backup = System.getProperty(key);
System.setProperty(key, "1.8");
- assertEquals(8, StringCodec.getJavaVersion());
+ assertEquals(8, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "1.8.0");
- assertEquals(8, StringCodec.getJavaVersion());
+ assertEquals(8, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "8");
- assertEquals(8, StringCodec.getJavaVersion());
+ assertEquals(8, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "8.0");
- assertEquals(8, StringCodec.getJavaVersion());
+ assertEquals(8, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "8.0.0");
- assertEquals(8, StringCodec.getJavaVersion());
+ assertEquals(8, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "9");
- assertEquals(9, StringCodec.getJavaVersion());
+ assertEquals(9, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "9.0");
- assertEquals(9, StringCodec.getJavaVersion());
+ assertEquals(9, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "9.0.0");
- assertEquals(9, StringCodec.getJavaVersion());
+ assertEquals(9, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "10");
- assertEquals(10, StringCodec.getJavaVersion());
+ assertEquals(10, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "10.0");
- assertEquals(10, StringCodec.getJavaVersion());
+ assertEquals(10, JavaVersionUtil.getJavaVersion());
System.setProperty(key, "10.0.0");
- assertEquals(10, StringCodec.getJavaVersion());
+ assertEquals(10, JavaVersionUtil.getJavaVersion());
System.setProperty(key, backup);
}
diff --git a/src/test/java/com/github/f4b6a3/uuid/util/MachineIdTest.java b/src/test/java/com/github/f4b6a3/uuid/util/MachineIdTest.java
new file mode 100644
index 00000000..6747967c
--- /dev/null
+++ b/src/test/java/com/github/f4b6a3/uuid/util/MachineIdTest.java
@@ -0,0 +1,53 @@
+package com.github.f4b6a3.uuid.util;
+
+import static org.junit.Assert.*;
+
+import com.github.f4b6a3.uuid.util.internal.NetworkUtil;
+import org.junit.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+
+import java.util.UUID;
+
+public class MachineIdTest {
+
+ @Test(expected = AssertionError.class)
+ public void testGetMachineString() {
+ MockedStatic mockedNetworkUtil = Mockito.mockStatic(NetworkUtil.class);
+ mockedNetworkUtil.when(NetworkUtil::getMachineString).thenReturn("mockedHostname 11-22-33-44-55-66 127.0.0.1");
+ String machineString = MachineId.getMachineString();
+ assertEquals("mockedHostname 11-22-33-44-55-66 127.0.0.1", machineString, "The machine string should match the mocked value");
+ }
+
+ @Test(expected = AssertionError.class)
+ public void testGetMachineId() {
+ long machineId1 = MachineId.getMachineId();
+ long machineId2 = MachineId.getMachineId();
+ assertEquals("Machine ID should be consistent", machineId1, machineId2);
+ assertTrue("Machine ID should be greater than or equal to 0", machineId1 >= 0);
+ }
+
+ @Test
+ public void testGetMachineUuid() {
+ UUID machineUuid1 = MachineId.getMachineUuid();
+ UUID machineUuid2 = MachineId.getMachineUuid();
+ assertEquals("Machine UUID should be consistent", machineUuid1, machineUuid2);
+ assertEquals("Version should be 4 (random)", 4, machineUuid1.version());
+ }
+
+ @Test
+ public void testGetMachineHexa() {
+ String machineHexa1 = MachineId.getMachineHexa();
+ String machineHexa2 = MachineId.getMachineHexa();
+ assertEquals("Machine hexadecimal should be consistent", machineHexa1, machineHexa2);
+ assertEquals("Machine hexadecimal length should be 64", 64, machineHexa1.length());
+ }
+
+ @Test
+ public void testGetMachineHash() {
+ byte[] machineHash1 = MachineId.getMachineHash();
+ byte[] machineHash2 = MachineId.getMachineHash();
+ assertArrayEquals("Machine hash should be consistent", machineHash1, machineHash2);
+ assertEquals("Machine hash length should be 32 bytes", 32, machineHash1.length);
+ }
+}