|
| 1 | +package net.ornithemc.osl.core.api.util; |
| 2 | + |
| 3 | +import java.util.Comparator; |
| 4 | + |
| 5 | +import net.ornithemc.osl.core.impl.util.NamespacedIdentifierException; |
| 6 | +import net.ornithemc.osl.core.impl.util.NamespacedIdentifierParseException; |
| 7 | +import net.ornithemc.osl.core.impl.util.NamespacedIdentifierImpl; |
| 8 | + |
| 9 | +/** |
| 10 | + * Utility methods for creating and validating {@link NamespacedIdentifier}s. |
| 11 | + */ |
| 12 | +public final class NamespacedIdentifiers { |
| 13 | + |
| 14 | + /** |
| 15 | + * The {@code minecraft} namespace is used for Vanilla resources and ids. |
| 16 | + */ |
| 17 | + public static final String MINECRAFT_NAMESPACE = "minecraft"; |
| 18 | + /** |
| 19 | + * The default namespace of {@code NamespacedIdentifier}s. |
| 20 | + * It is recommended to use a custom namespace for your own identifiers. |
| 21 | + */ |
| 22 | + public static final String DEFAULT_NAMESPACE = MINECRAFT_NAMESPACE; |
| 23 | + |
| 24 | + /** |
| 25 | + * The maximum length of a {@code NamespacedIdentifier}'s namespace string. |
| 26 | + */ |
| 27 | + public static final int MAX_LENGTH_NAMESPACE = Integer.MAX_VALUE; |
| 28 | + /** |
| 29 | + * The maximum length of a {@code NamespacedIdentifier} identifier string. |
| 30 | + */ |
| 31 | + public static final int MAX_LENGTH_IDENTIFIER = Integer.MAX_VALUE; |
| 32 | + |
| 33 | + /** |
| 34 | + * A comparator for {@code NamespacedIdentifier}s, comparing first by identifier, then by namespace. |
| 35 | + */ |
| 36 | + public static final Comparator<NamespacedIdentifier> COMPARATOR = (a, b) -> { |
| 37 | + int c = a.identifier().compareTo(b.identifier()); |
| 38 | + if (c == 0) { |
| 39 | + c = a.namespace().compareTo(b.namespace()); |
| 40 | + } |
| 41 | + |
| 42 | + return c; |
| 43 | + }; |
| 44 | + |
| 45 | + /** |
| 46 | + * Construct and validate a {@code NamespacedIdentifier} with the default namespace and the given identifier. |
| 47 | + * |
| 48 | + * @return a {@code NamespacedIdentifier} with the default namespace and the given identifier. |
| 49 | + * @throws NamespacedIdentifierException |
| 50 | + * if the given identifier is invalid. |
| 51 | + */ |
| 52 | + public static NamespacedIdentifier from(String identifier) { |
| 53 | + return from(DEFAULT_NAMESPACE, identifier); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Construct and validate a {@code NamespacedIdentifier} from the given namespace and identifier. |
| 58 | + * |
| 59 | + * @return a {@code NamespacedIdentifier} with the given namespace and identifier. |
| 60 | + * @throws NamespacedIdentifierException |
| 61 | + * if the given namespace or identifier is invalid. |
| 62 | + */ |
| 63 | + public static NamespacedIdentifier from(String namespace, String identifier) { |
| 64 | + return new NamespacedIdentifierImpl( |
| 65 | + validateNamespace(namespace), |
| 66 | + validateIdentifier(identifier) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Parse a {@code NamespacedIdentifier} from the given {@code String}. |
| 72 | + * The returned identifier is always valid. If no valid identifier can |
| 73 | + * be parsed from the given string, an exception is thrown. |
| 74 | + * |
| 75 | + * @return the {@code NamespacedIdentifier}} represented by the {@code String}. |
| 76 | + * @throws NamespacedIdentifierParseException |
| 77 | + * if no valid {@code NamespacedIdentifier} can be parsed from the given {@code String}. |
| 78 | + */ |
| 79 | + public static NamespacedIdentifier parse(String s) { |
| 80 | + int i = s.indexOf(NamespacedIdentifier.SEPARATOR); |
| 81 | + |
| 82 | + try { |
| 83 | + if (i < 0) { |
| 84 | + return from(s.substring(i + 1)); |
| 85 | + } else if (i > 0) { |
| 86 | + return from(s.substring(0, i), s.substring(i + 1)); |
| 87 | + } else { |
| 88 | + throw NamespacedIdentifierParseException.invalid(s, "badly formatted"); |
| 89 | + } |
| 90 | + } catch (NamespacedIdentifierException e) { |
| 91 | + throw NamespacedIdentifierParseException.invalid(s, e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Check whether the given {@code NamespacedIdentifier} is valid, or throw an exception. |
| 97 | + */ |
| 98 | + public static NamespacedIdentifier validate(NamespacedIdentifier id) { |
| 99 | + try { |
| 100 | + validateNamespace(id.namespace()); |
| 101 | + validateIdentifier(id.identifier()); |
| 102 | + |
| 103 | + return id; |
| 104 | + } catch (NamespacedIdentifierException e) { |
| 105 | + throw NamespacedIdentifierException.invalid(id, e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Check that the given namespace is valid for a {@code NamespacedIdentifier}. |
| 111 | + */ |
| 112 | + public static String validateNamespace(String namespace) { |
| 113 | + if (namespace == null || namespace.isEmpty()) { |
| 114 | + throw NamespacedIdentifierException.invalidNamespace(namespace, "null or empty"); |
| 115 | + } |
| 116 | + if (namespace.length() > MAX_LENGTH_NAMESPACE) { |
| 117 | + throw NamespacedIdentifierException.invalidNamespace(namespace, "length " + namespace.length() + " is greater than maximum allowed " + MAX_LENGTH_NAMESPACE); |
| 118 | + } |
| 119 | + if (!namespace.chars().allMatch(chr -> chr == '-' || chr == '.' || chr == '_' || (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9'))) { |
| 120 | + throw NamespacedIdentifierException.invalidNamespace(namespace, "contains illegal characters - only [a-zA-Z0-9-._] are allowed"); |
| 121 | + } |
| 122 | + |
| 123 | + return namespace; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Check that the given identifier is valid for a {@code NamespacedIdentifier}. |
| 128 | + */ |
| 129 | + public static String validateIdentifier(String identifier) { |
| 130 | + if (identifier == null || identifier.isEmpty()) { |
| 131 | + throw NamespacedIdentifierException.invalidIdentifier(identifier, "null or empty"); |
| 132 | + } |
| 133 | + if (identifier.length() > MAX_LENGTH_IDENTIFIER) { |
| 134 | + throw NamespacedIdentifierException.invalidIdentifier(identifier, "length " + identifier.length() + " is greater than maximum allowed " + MAX_LENGTH_IDENTIFIER); |
| 135 | + } |
| 136 | + if (!identifier.chars().allMatch(chr -> chr == '-' || chr == '.' || chr == '_' || chr == '/' || (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9'))) { |
| 137 | + throw NamespacedIdentifierException.invalidIdentifier(identifier, "contains illegal characters - only [a-zA-Z0-9-._/] are allowed"); |
| 138 | + } |
| 139 | + |
| 140 | + return identifier; |
| 141 | + } |
| 142 | + |
| 143 | + public static boolean equals(NamespacedIdentifier a, NamespacedIdentifier b) { |
| 144 | + return a.namespace().equals(b.namespace()) && a.identifier().equals(b.identifier()); |
| 145 | + } |
| 146 | +} |
0 commit comments