@@ -62,13 +62,24 @@ public static NamespacedKey of(final String namespace, final String key) {
62
62
* be separated by ':'.
63
63
* @param namespacedKey String to parse as NamespacedKey
64
64
* @return parsed NamespacedKey
65
+ * @throws IllegalArgumentException if the input isn't a valid namespaced key
65
66
*/
66
67
@ Contract ("_ -> new" )
67
68
public static NamespacedKey parse (final String namespacedKey ) {
68
- final String [] key = parseNamespacedKey (namespacedKey ).orElseThrow (() ->
69
+ return parseSafe (namespacedKey ).orElseThrow (() ->
69
70
new IllegalArgumentException ("The namespaced key '" + namespacedKey + "' "
70
71
+ "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 ]));
72
83
}
73
84
74
85
/**
@@ -134,26 +145,12 @@ public int hashCode() {
134
145
* @return a string array where the first value is the namespace and the second value is the namespace,
135
146
* or null if that input doesn't have a separator character ':'
136
147
*/
137
- private static Optional <String []> parseNamespacedKey (final String input ) {
148
+ static Optional <String []> parseNamespacedKey (final String input ) {
138
149
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 )
154
152
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 )});
157
154
}
158
155
159
156
/**
@@ -165,14 +162,12 @@ private static Optional<String[]> parseNamespacedKey(final String input) {
165
162
* @return whether the namespace and key follow their formats
166
163
*/
167
164
private static boolean isValidNamespacedKey (final String namespace , final String key ) {
168
- if (namespace .isEmpty ())
165
+ if (namespace .isEmpty () || key . isEmpty () )
169
166
return false ;
170
167
for (final char c : namespace .toCharArray ()) {
171
168
if (!isValidNamespace (c ))
172
169
return false ;
173
170
}
174
- if (key .isEmpty ())
175
- return false ;
176
171
for (final char c : key .toCharArray ()) {
177
172
if (!isValidKey (c ))
178
173
return false ;
0 commit comments