Skip to content

Commit

Permalink
Merge pull request #101 from pateljay15/master
Browse files Browse the repository at this point in the history
Improve Code Quality
  • Loading branch information
fabiolimace authored Mar 31, 2024
2 parents 3d68f61 + 16d45bf commit e30f093
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 213 deletions.
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
Expand Down
110 changes: 0 additions & 110 deletions src/main/java/com/github/f4b6a3/uuid/alt/GUID.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
121 changes: 121 additions & 0 deletions src/main/java/com/github/f4b6a3/uuid/alt/Parser.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
30 changes: 2 additions & 28 deletions src/main/java/com/github/f4b6a3/uuid/codec/StringCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -66,7 +67,7 @@ public class StringCodec implements UuidCodec<String> {
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.
Expand Down Expand Up @@ -270,31 +271,4 @@ protected static char[] toCharArray(String string) {

return chars;
}

/**
* Returns the java major version number.
*
* @see <a href= "https://www.java.com/releases/">JDK Releases</a>
* @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;
}
}
}
15 changes: 11 additions & 4 deletions src/main/java/com/github/f4b6a3/uuid/codec/base/BaseN.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit e30f093

Please sign in to comment.