Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
testower committed Jul 30, 2024
1 parent 1ec405a commit c3d26d3
Show file tree
Hide file tree
Showing 11 changed files with 592 additions and 402 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.entur.lamassu.model.id.DefaultIdValidator;
import org.entur.lamassu.model.id.IdBuilder;
import org.entur.lamassu.model.id.predicate.IdPredicateBuilder;
Expand Down
158 changes: 83 additions & 75 deletions src/main/java/org/entur/lamassu/model/id/DefaultIdValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,97 +19,105 @@
package org.entur.lamassu.model.id;

public class DefaultIdValidator implements IdValidator {
public static final char ID_SEPARATOR_CHAR = ':';
public static final int ID_CODESPACE_LENGTH = 3;
public static final int ID_MINIMUM_LENGTH = 6;

protected static final DefaultIdValidator instance = new DefaultIdValidator();
public static final char ID_SEPARATOR_CHAR = ':';
public static final int ID_CODESPACE_LENGTH = 3;
public static final int ID_MINIMUM_LENGTH = 6;

public static DefaultIdValidator getInstance() {
return instance;
}
protected static final DefaultIdValidator instance = new DefaultIdValidator();

public boolean validate(CharSequence string, int offset, int length) {
// minimum size is XXX:X:X
if (length < ID_MINIMUM_LENGTH) {
return false;
}
if (string.charAt(offset + ID_CODESPACE_LENGTH) != ':') {
return false;
}
int last = getLastSeperatorIndex(string, ID_CODESPACE_LENGTH + 1, length);
if (last == -1) {
return false;
}
return validateCodespace(string, 0, ID_CODESPACE_LENGTH) && validateType(string, ID_CODESPACE_LENGTH + 1, last)
&& validateValue(string, last + 1, string.length());
}
public static DefaultIdValidator getInstance() {
return instance;
}

protected static int getLastSeperatorIndex(CharSequence string, int startIndex, int endIndex) {
for (int i = endIndex - 1; i >= startIndex; i--) {
if (string.charAt(i) == ID_SEPARATOR_CHAR) {
return i;
}
}
return -1;
public boolean validate(CharSequence string, int offset, int length) {
// minimum size is XXX:X:X
if (length < ID_MINIMUM_LENGTH) {
return false;
}
if (string.charAt(offset + ID_CODESPACE_LENGTH) != ':') {
return false;
}
int last = getLastSeperatorIndex(string, ID_CODESPACE_LENGTH + 1, length);
if (last == -1) {
return false;
}
return (
validateCodespace(string, 0, ID_CODESPACE_LENGTH) &&
validateType(string, ID_CODESPACE_LENGTH + 1, last) &&
validateValue(string, last + 1, string.length())
);
}

public boolean validateCodespace(CharSequence codespace, int startIndex, int endIndex) {
// length 3
// A-Z
if (endIndex - startIndex == ID_CODESPACE_LENGTH) {
for (int i = startIndex; i < endIndex; i++) {
char c = codespace.charAt(i);
if (c < 'A' || c > 'Z') {
return false;
}
}
return true;
}
return false;
protected static int getLastSeperatorIndex(
CharSequence string,
int startIndex,
int endIndex
) {
for (int i = endIndex - 1; i >= startIndex; i--) {
if (string.charAt(i) == ID_SEPARATOR_CHAR) {
return i;
}
}
return -1;
}

public boolean validateType(CharSequence type, int startIndex, int endIndex) {
// not empty string
// A-Z
// a-z
if (endIndex > startIndex) {
for (int i = startIndex; i < endIndex; i++) {
char c = type.charAt(i);
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) {
return false;
}
}
return true;
public boolean validateCodespace(CharSequence codespace, int startIndex, int endIndex) {
// length 3
// A-Z
if (endIndex - startIndex == ID_CODESPACE_LENGTH) {
for (int i = startIndex; i < endIndex; i++) {
char c = codespace.charAt(i);
if (c < 'A' || c > 'Z') {
return false;
}
return false;
}
return true;
}
return false;
}

public boolean validateValue(CharSequence value, int startIndex, int endIndex) {
if (endIndex > startIndex) {
for (int i = startIndex; i < endIndex; i++) {
char c = value.charAt(i);
if (!isValueCharacter(c)) {
return false;
}
}
return true;
public boolean validateType(CharSequence type, int startIndex, int endIndex) {
// not empty string
// A-Z
// a-z
if (endIndex > startIndex) {
for (int i = startIndex; i < endIndex; i++) {
char c = type.charAt(i);
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) {
return false;
}
return false;
}
return true;
}
return false;
}

protected static boolean isValueCharacter(char c) {
if (c < 0x21 || c > 0x7E) {
return false; // Not in the ASCII printable range
public boolean validateValue(CharSequence value, int startIndex, int endIndex) {
if (endIndex > startIndex) {
for (int i = startIndex; i < endIndex; i++) {
char c = value.charAt(i);
if (!isValueCharacter(c)) {
return false;
}
}
return true;
}
return false;
}

if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
return true;
}
protected static boolean isValueCharacter(char c) {
if (c < 0x21 || c > 0x7E) {
return false; // Not in the ASCII printable range
}

return switch (c) {
case '.', '@', ':', '/', '_', '-' -> true;
default -> false;
};
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
return true;
}

return switch (c) {
case '.', '@', ':', '/', '_', '-' -> true;
default -> false;
};
}
}
84 changes: 46 additions & 38 deletions src/main/java/org/entur/lamassu/model/id/IdBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,62 @@
package org.entur.lamassu.model.id;

public class IdBuilder {
public static IdBuilder newInstance() {
return new IdBuilder();
}

protected static final String ID_SEPARATOR_CHAR = ":";
public static IdBuilder newInstance() {
return new IdBuilder();
}

private static final IdValidator defaultValidator = DefaultIdValidator.getInstance();
protected static final String ID_SEPARATOR_CHAR = ":";

private final IdValidator validator;
private static final IdValidator defaultValidator = DefaultIdValidator.getInstance();

protected String codespace;
protected String type;
protected String value;
private final IdValidator validator;

public IdBuilder() {
this(defaultValidator);
}
protected String codespace;
protected String type;
protected String value;

public IdBuilder(IdValidator validator) {
this.validator = validator;
}
public IdBuilder() {
this(defaultValidator);
}

public IdBuilder withCodespace(String codespace) {
this.codespace = codespace;
return this;
}
public IdBuilder(IdValidator validator) {
this.validator = validator;
}

public IdBuilder withType(String type) {
this.type = type;
return this;
}
public IdBuilder withCodespace(String codespace) {
this.codespace = codespace;
return this;
}

public IdBuilder withValue(String value) {
this.value = value;
public IdBuilder withType(String type) {
this.type = type;
return this;
}

return this;
}
public IdBuilder withValue(String value) {
this.value = value;

return this;
}

public String build() {
if (codespace == null || !validator.validateCodespace(codespace)) {
throw new IllegalStateException("Expected codespace (size 3 with characters A-Z), found " + codespace);
}
if (type == null || !validator.validateType(type)) {
throw new IllegalStateException("Expected type (nonempty with characters A-Z), found " + type);
}
if (value == null || !validator.validateValue(value)) {
throw new IllegalStateException("Expected value (nonempty with characters A-Z, a-z, ø, Ø, æ, Æ, å, Å, underscore, \\ and -), found " + value);
}
return String.join(ID_SEPARATOR_CHAR, codespace, type, value);
public String build() {
if (codespace == null || !validator.validateCodespace(codespace)) {
throw new IllegalStateException(
"Expected codespace (size 3 with characters A-Z), found " + codespace
);
}
if (type == null || !validator.validateType(type)) {
throw new IllegalStateException(
"Expected type (nonempty with characters A-Z), found " + type
);
}
if (value == null || !validator.validateValue(value)) {
throw new IllegalStateException(
"Expected value (nonempty with characters A-Z, a-z, ø, Ø, æ, Æ, å, Å, underscore, \\ and -), found " +
value
);
}
return String.join(ID_SEPARATOR_CHAR, codespace, type, value);
}
}
32 changes: 16 additions & 16 deletions src/main/java/org/entur/lamassu/model/id/IdValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@
package org.entur.lamassu.model.id;

public interface IdValidator {
default boolean validate(CharSequence id) {
return validate(id, 0, id.length());
}
default boolean validate(CharSequence id) {
return validate(id, 0, id.length());
}

boolean validate(CharSequence id, int offset, int length);
boolean validate(CharSequence id, int offset, int length);

default boolean validateCodespace(CharSequence codespace) {
return validateCodespace(codespace, 0, codespace.length());
}
default boolean validateCodespace(CharSequence codespace) {
return validateCodespace(codespace, 0, codespace.length());
}

boolean validateCodespace(CharSequence codespace, int offset, int length);
boolean validateCodespace(CharSequence codespace, int offset, int length);

default boolean validateType(CharSequence type) {
return validateType(type, 0, type.length());
}
default boolean validateType(CharSequence type) {
return validateType(type, 0, type.length());
}

boolean validateType(CharSequence type, int offset, int length);
boolean validateType(CharSequence type, int offset, int length);

default boolean validateValue(CharSequence value) {
return validateValue(value, 0, value.length());
}
default boolean validateValue(CharSequence value) {
return validateValue(value, 0, value.length());
}

boolean validateValue(CharSequence value, int offset, int length);
boolean validateValue(CharSequence value, int offset, int length);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,26 @@
import org.entur.lamassu.model.id.DefaultIdValidator;

public class IdCodespacePredicate implements IdPredicate {
private final char[] prefix;

public IdCodespacePredicate(CharSequence codespace) {
prefix = new char[] { codespace.charAt(0), codespace.charAt(1), codespace.charAt(2), DefaultIdValidator.ID_SEPARATOR_CHAR};
}
private final char[] prefix;

public boolean test(CharSequence t) {
return t.length() > 4 && t.charAt(0) == prefix[0] && t.charAt(1) == prefix[1] && t.charAt(2) == prefix[2] && t.charAt(3) == prefix[3];
}
public IdCodespacePredicate(CharSequence codespace) {
prefix =
new char[] {
codespace.charAt(0),
codespace.charAt(1),
codespace.charAt(2),
DefaultIdValidator.ID_SEPARATOR_CHAR,
};
}

public boolean test(CharSequence t) {
return (
t.length() > 4 &&
t.charAt(0) == prefix[0] &&
t.charAt(1) == prefix[1] &&
t.charAt(2) == prefix[2] &&
t.charAt(3) == prefix[3]
);
}
}
Loading

0 comments on commit c3d26d3

Please sign in to comment.