Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@

@Mixin(Identifier.class)
public class IdentifierMixin implements NamespacedIdentifier {

@Shadow
private String namespace;

@Shadow
private String path;

@Inject(
method = "equals",
remap = false,
cancellable = true,
at = @At(
value = "HEAD"
)
)
@Inject(method = "equals", at = @At(value = "HEAD"), cancellable = true, remap = false)
private void osl$core$equalsNamespacedIdentifier(Object o, CallbackInfoReturnable<Boolean> cir) {
if (o instanceof NamespacedIdentifier) {
cir.setReturnValue(NamespacedIdentifiers.equals(this, (NamespacedIdentifier) o));
Expand All @@ -39,7 +32,7 @@ public String namespace() {
}

@Override
public String identifier() {
public String path() {
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@

@Mixin(Identifier.class)
public class IdentifierMixin implements NamespacedIdentifier {

@Shadow
private String namespace;

@Shadow
private String path;

@Inject(
method = "equals",
remap = false,
cancellable = true,
at = @At(
value = "HEAD"
)
)
@Inject(method = "equals", at = @At(value = "HEAD"), cancellable = true, remap = false)
private void osl$core$equalsNamespacedIdentifier(Object o, CallbackInfoReturnable<Boolean> cir) {
if (o instanceof NamespacedIdentifier) {
cir.setReturnValue(NamespacedIdentifiers.equals(this, (NamespacedIdentifier) o));
Expand All @@ -39,7 +32,7 @@ public String namespace() {
}

@Override
public String identifier() {
public String path() {
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,26 @@ public interface NamespacedIdentifier {
* The separator between the namespace and identifier in the {@code String}
* representation of a {@code NamespacedIdentifier}.
*/
static char SEPARATOR = ':';
char SEPARATOR = ':';

/**
* The {@code minecraft} namespace, used for Vanilla resources and ids.
*/
String VANILLA_NAMESPACE = "minecraft";

/**
* @return the namespace of this {@code NamespacedIdentifier}.
*/
String namespace();

/**
* @return the identifier of this {@code NamespacedIdentifier}.
* @return the path of this {@code NamespacedIdentifier}.
*/
String identifier();
String path();

@Deprecated
default String identifier() {
return this.path();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,41 @@
* Utility methods for creating and validating {@link NamespacedIdentifier}s.
*/
public final class NamespacedIdentifiers {

/**
* The {@code minecraft} namespace is used for Vanilla resources and ids.
*/
public static final String MINECRAFT_NAMESPACE = "minecraft";
/**
* The default namespace of {@code NamespacedIdentifier}s.
* It is recommended to use a custom namespace for your own identifiers.
*/
public static final String DEFAULT_NAMESPACE = MINECRAFT_NAMESPACE;

/**
* The maximum length of a {@code NamespacedIdentifier}'s namespace string.
*/
public static final int MAX_LENGTH_NAMESPACE = Integer.MAX_VALUE;

/**
* The maximum length of a {@code NamespacedIdentifier} identifier string.
* The maximum length of a {@code NamespacedIdentifier} path string.
*/
public static final int MAX_LENGTH_IDENTIFIER = Integer.MAX_VALUE;
public static final int MAX_LENGTH_PATH = Integer.MAX_VALUE;

/**
* A comparator for {@code NamespacedIdentifier}s, comparing first by identifier, then by namespace.
* A comparator for {@code NamespacedIdentifier}s, comparing first by path, then by namespace.
*/
public static final Comparator<NamespacedIdentifier> COMPARATOR = (a, b) -> {
int c = a.identifier().compareTo(b.identifier());
if (c == 0) {
c = a.namespace().compareTo(b.namespace());
}

return c;
};
public static final Comparator<NamespacedIdentifier> COMPARATOR = Comparator.comparing(NamespacedIdentifier::path).thenComparing(NamespacedIdentifier::namespace);

/**
* Construct and validate a {@code NamespacedIdentifier} with the default namespace and the given identifier.
* Construct and validate a {@code NamespacedIdentifier} with the default namespace and the given path.
*
* @return a {@code NamespacedIdentifier} with the default namespace and the given identifier.
* @return a {@code NamespacedIdentifier} with the default namespace and the given path.
* @throws NamespacedIdentifierException
* if the given identifier is invalid.
*/
public static NamespacedIdentifier from(String identifier) {
return from(DEFAULT_NAMESPACE, identifier);
public static NamespacedIdentifier from(String path) {
return from(NamespacedIdentifier.VANILLA_NAMESPACE, path);
}

/**
* Construct and validate a {@code NamespacedIdentifier} from the given namespace and identifier.
* Construct and validate a {@code NamespacedIdentifier} from the given namespace and path.
*
* @return a {@code NamespacedIdentifier} with the given namespace and identifier.
* @return a {@code NamespacedIdentifier} with the given namespace and path.
* @throws NamespacedIdentifierException
* if the given namespace or identifier is invalid.
*/
public static NamespacedIdentifier from(String namespace, String identifier) {
return new NamespacedIdentifierImpl(
validateNamespace(namespace),
validateIdentifier(identifier)
);
public static NamespacedIdentifier from(String namespace, String path) {
return new NamespacedIdentifierImpl(validateNamespace(namespace), validatePath(path));
}

/**
Expand All @@ -76,19 +56,18 @@ public static NamespacedIdentifier from(String namespace, String identifier) {
* @throws NamespacedIdentifierParseException
* if no valid {@code NamespacedIdentifier} can be parsed from the given {@code String}.
*/
public static NamespacedIdentifier parse(String s) {
int i = s.indexOf(NamespacedIdentifier.SEPARATOR);

public static NamespacedIdentifier parse(String input) {
int index = input.indexOf(NamespacedIdentifier.SEPARATOR);
try {
if (i < 0) {
return from(s.substring(i + 1));
} else if (i > 0) {
return from(s.substring(0, i), s.substring(i + 1));
if (index < 0) {
return from(input.substring(index + 1));
} else if (index > 0) {
return from(input.substring(0, index), input.substring(index + 1));
} else {
throw NamespacedIdentifierParseException.invalid(s, "badly formatted");
throw NamespacedIdentifierParseException.invalid(input, "badly formatted");
}
} catch (NamespacedIdentifierException e) {
throw NamespacedIdentifierParseException.invalid(s, e);
} catch (NamespacedIdentifierException exception) {
throw NamespacedIdentifierParseException.invalid(input, exception);
}
}

Expand All @@ -98,11 +77,10 @@ public static NamespacedIdentifier parse(String s) {
public static NamespacedIdentifier validate(NamespacedIdentifier id) {
try {
validateNamespace(id.namespace());
validateIdentifier(id.identifier());

validatePath(id.path());
return id;
} catch (NamespacedIdentifierException e) {
throw NamespacedIdentifierException.invalid(id, e);
} catch (NamespacedIdentifierException exception) {
throw NamespacedIdentifierException.invalid(id, exception);
}
}

Expand All @@ -113,9 +91,11 @@ public static String validateNamespace(String namespace) {
if (namespace == null || namespace.isEmpty()) {
throw NamespacedIdentifierException.invalidNamespace(namespace, "null or empty");
}

if (namespace.length() > MAX_LENGTH_NAMESPACE) {
throw NamespacedIdentifierException.invalidNamespace(namespace, "length " + namespace.length() + " is greater than maximum allowed " + MAX_LENGTH_NAMESPACE);
}

if (!namespace.chars().allMatch(chr -> chr == '-' || chr == '.' || chr == '_' || (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9'))) {
throw NamespacedIdentifierException.invalidNamespace(namespace, "contains illegal characters - only [a-zA-Z0-9-._] are allowed");
}
Expand All @@ -124,23 +104,25 @@ public static String validateNamespace(String namespace) {
}

/**
* Check that the given identifier is valid for a {@code NamespacedIdentifier}.
* Check that the given path is valid for a {@code NamespacedIdentifier}.
*/
public static String validateIdentifier(String identifier) {
if (identifier == null || identifier.isEmpty()) {
throw NamespacedIdentifierException.invalidIdentifier(identifier, "null or empty");
public static String validatePath(String path) {
if (path == null || path.isEmpty()) {
throw NamespacedIdentifierException.invalidPath(path, "null or empty");
}
if (identifier.length() > MAX_LENGTH_IDENTIFIER) {
throw NamespacedIdentifierException.invalidIdentifier(identifier, "length " + identifier.length() + " is greater than maximum allowed " + MAX_LENGTH_IDENTIFIER);

if (path.length() > MAX_LENGTH_PATH) {
throw NamespacedIdentifierException.invalidPath(path, "length " + path.length() + " is greater than maximum allowed " + MAX_LENGTH_PATH);
}
if (!identifier.chars().allMatch(chr -> chr == '-' || chr == '.' || chr == '_' || chr == '/' || (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9'))) {
throw NamespacedIdentifierException.invalidIdentifier(identifier, "contains illegal characters - only [a-zA-Z0-9-._/] are allowed");

if (!path.chars().allMatch(chr -> chr == '-' || chr == '.' || chr == '_' || chr == '/' || (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9'))) {
throw NamespacedIdentifierException.invalidPath(path, "contains illegal characters - only [a-zA-Z0-9-._/] are allowed");
}

return identifier;
return path;
}

public static boolean equals(NamespacedIdentifier a, NamespacedIdentifier b) {
return a.namespace().equals(b.namespace()) && a.identifier().equals(b.identifier());
public static boolean equals(NamespacedIdentifier left, NamespacedIdentifier right) {
return left.namespace().equals(right.namespace()) && left.path().equals(right.path());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
// This interface is used for transitive interface injection into Identifier.
// Its purpose is to provide method implementations to keep the compiler happy.
public interface IdentifierImpl extends NamespacedIdentifier {

@Override
default String namespace() {
throw new AbstractMethodError("Not implemented!");
}

@Override
default String identifier() {
default String path() {
throw new AbstractMethodError("Not implemented!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

@SuppressWarnings("serial")
public class NamespacedIdentifierException extends RuntimeException {

private NamespacedIdentifierException(String message) {
super(message);
}
Expand All @@ -25,7 +24,7 @@ public static NamespacedIdentifierException invalidNamespace(String namespace, S
return new NamespacedIdentifierException("\'" + namespace + "\' is not a valid namespace: " + reason);
}

public static NamespacedIdentifierException invalidIdentifier(String identifier, String reason) {
return new NamespacedIdentifierException("\'" + identifier + "\' is not a valid identifier: " + reason);
public static NamespacedIdentifierException invalidPath(String path, String reason) {
return new NamespacedIdentifierException("\'" + path + "\' is not a valid path: " + reason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
public final class NamespacedIdentifierImpl implements NamespacedIdentifier {

private final String namespace;
private final String identifier;
private final String path;

public NamespacedIdentifierImpl(String namespace, String identifier) {
public NamespacedIdentifierImpl(String namespace, String path) {
this.namespace = namespace;
this.identifier = identifier;
this.path = path;
}

@Override
Expand All @@ -37,12 +37,12 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
// this impl matches Vanilla Identifier's impl
return 31 * namespace.hashCode() + identifier.hashCode();
return 31 * namespace.hashCode() + path.hashCode();
}

@Override
public String toString() {
return namespace + SEPARATOR + identifier;
return namespace + SEPARATOR + path;
}

@Override
Expand All @@ -51,7 +51,7 @@ public String namespace() {
}

@Override
public String identifier() {
return identifier;
public String path() {
return path;
}
}
Loading