Skip to content

Commit c95bdea

Browse files
committed
Improve and optimize NamespacedKey parsing
1 parent ded1de3 commit c95bdea

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

api/src/main/java/org/machinemc/api/utils/NamespacedKey.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,24 @@ public static NamespacedKey of(final String namespace, final String key) {
6262
* be separated by ':'.
6363
* @param namespacedKey String to parse as NamespacedKey
6464
* @return parsed NamespacedKey
65+
* @throws IllegalArgumentException if the input isn't a valid namespaced key
6566
*/
6667
@Contract("_ -> new")
6768
public static NamespacedKey parse(final String namespacedKey) {
68-
final String[] key = parseNamespacedKey(namespacedKey).orElseThrow(() ->
69+
return parseSafe(namespacedKey).orElseThrow(() ->
6970
new IllegalArgumentException("The namespaced key '" + namespacedKey + "' "
7071
+ "does not have a separator character ':'"));
71-
return NamespacedKey.of(key[0], key[1]);
72+
}
73+
74+
/**
75+
* Parses the NamespacedKey from a String, namespace and key should
76+
* be separated by ':'.
77+
* @param namespacedKey String to parse as NamespacedKey
78+
* @return parsed NamespacedKey, or null if the input isn't a valid namespaced key
79+
*/
80+
@Contract("_ -> new")
81+
public static Optional<NamespacedKey> parseSafe(final String namespacedKey) {
82+
return parseNamespacedKey(namespacedKey).map(key -> NamespacedKey.of(key[0], key[1]));
7283
}
7384

7485
/**
@@ -134,26 +145,12 @@ public int hashCode() {
134145
* @return a string array where the first value is the namespace and the second value is the namespace,
135146
* or null if that input doesn't have a separator character ':'
136147
*/
137-
private static Optional<String[]> parseNamespacedKey(final String input) {
148+
static Optional<String[]> parseNamespacedKey(final String input) {
138149
Objects.requireNonNull(input, "Text to parse can not be null");
139-
final String[] namespacedKey = new String[2];
140-
final char[] chars = input.toCharArray();
141-
StringBuilder builder = new StringBuilder();
142-
boolean separator = false;
143-
for (final char c : chars) {
144-
if (c == ':') {
145-
separator = true;
146-
namespacedKey[0] = builder.toString();
147-
builder = new StringBuilder();
148-
continue;
149-
}
150-
151-
builder.append(c);
152-
}
153-
if (!separator)
150+
int separator = input.indexOf(':');
151+
if (separator == -1)
154152
return Optional.empty();
155-
namespacedKey[1] = builder.toString();
156-
return Optional.of(namespacedKey);
153+
return Optional.of(new String[]{input.substring(0, separator), input.substring(separator + 1)});
157154
}
158155

159156
/**
@@ -165,14 +162,12 @@ private static Optional<String[]> parseNamespacedKey(final String input) {
165162
* @return whether the namespace and key follow their formats
166163
*/
167164
private static boolean isValidNamespacedKey(final String namespace, final String key) {
168-
if (namespace.isEmpty())
165+
if (namespace.isEmpty() || key.isEmpty())
169166
return false;
170167
for (final char c : namespace.toCharArray()) {
171168
if (!isValidNamespace(c))
172169
return false;
173170
}
174-
if (key.isEmpty())
175-
return false;
176171
for (final char c : key.toCharArray()) {
177172
if (!isValidKey(c))
178173
return false;

server/src/main/java/org/machinemc/api/utils/LazyNamespacedKey.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.jetbrains.annotations.ApiStatus;
1818
import org.jetbrains.annotations.Contract;
1919

20+
import java.util.Optional;
21+
2022
/**
2123
* Util for creating namespaced keys fast.
2224
* @see org.machinemc.api.utils.NamespacedKey
@@ -38,11 +40,10 @@ private LazyNamespacedKey() {
3840
*/
3941
@Contract("_ -> new")
4042
public static NamespacedKey lazy(final String namespacedKey) {
41-
final String[] parts = namespacedKey.split(":");
42-
final StringBuilder key = new StringBuilder();
43-
for (int i = 1; i < parts.length; i++)
44-
key.append(parts[i]);
45-
return new NamespacedKey(parts[0], key.toString());
43+
final Optional<String[]> parsed = NamespacedKey.parseNamespacedKey(namespacedKey);
44+
return parsed
45+
.map(strings -> new NamespacedKey(strings[0], strings[1]))
46+
.orElse(new NamespacedKey(namespacedKey, ""));
4647
}
4748

4849
/**

0 commit comments

Comments
 (0)