Skip to content

Commit f216cb3

Browse files
committed
chore(key): Refactor module to remove deprecations and use modern language features
1 parent 40b4156 commit f216cb3

File tree

6 files changed

+20
-81
lines changed

6 files changed

+20
-81
lines changed

key/src/main/java/net/kyori/adventure/key/InvalidKeyException.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package net.kyori.adventure.key;
2525

26+
import java.io.Serial;
2627
import org.jetbrains.annotations.NotNull;
2728
import org.jetbrains.annotations.Nullable;
2829

@@ -32,6 +33,7 @@
3233
* @since 4.0.0
3334
*/
3435
public final class InvalidKeyException extends RuntimeException {
36+
@Serial
3537
private static final long serialVersionUID = -5413304087321449434L;
3638
private final String keyNamespace;
3739
private final String keyValue;
@@ -48,7 +50,7 @@ public final class InvalidKeyException extends RuntimeException {
4850
* @return a key
4951
* @since 4.0.0
5052
*/
51-
public final @NotNull String keyNamespace() {
53+
public @NotNull String keyNamespace() {
5254
return this.keyNamespace;
5355
}
5456

@@ -58,7 +60,7 @@ public final class InvalidKeyException extends RuntimeException {
5860
* @return a key
5961
* @since 4.0.0
6062
*/
61-
public final @NotNull String keyValue() {
63+
public @NotNull String keyValue() {
6264
return this.keyValue;
6365
}
6466
}

key/src/main/java/net/kyori/adventure/key/Key.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public interface Key extends Comparable<Key>, Examinable, Namespaced, Keyed {
122122
* @throws InvalidKeyException if the namespace or value contains an invalid character
123123
* @since 4.4.0
124124
*/
125+
@SuppressWarnings("PatternValidation") // The namespace is tested later.
125126
static @NotNull Key key(final @NotNull Namespaced namespaced, @KeyPattern.Value final @NotNull String value) {
126127
return key(Objects.requireNonNull(namespaced, "namespaced").namespace(), value);
127128
}
@@ -136,6 +137,8 @@ public interface Key extends Comparable<Key>, Examinable, Namespaced, Keyed {
136137
* @since 4.0.0
137138
*/
138139
static @NotNull Key key(@KeyPattern.Namespace final @NotNull String namespace, @KeyPattern.Value final @NotNull String value) {
140+
KeyImpl.checkError("namespace", namespace, namespace, value, Key.checkNamespace(namespace), KeyImpl.NAMESPACE_PATTERN);
141+
KeyImpl.checkError("value", value, namespace, value, Key.checkValue(value), KeyImpl.VALUE_PATTERN);
139142
return new KeyImpl(namespace, value);
140143
}
141144

@@ -176,7 +179,7 @@ static boolean parseable(final @Nullable String string) {
176179
* @since 4.12.0
177180
*/
178181
static boolean parseableNamespace(final @NotNull String namespace) {
179-
return !checkNamespace(namespace).isPresent();
182+
return checkNamespace(namespace).isEmpty();
180183
}
181184

182185
/**
@@ -204,7 +207,7 @@ static boolean parseableNamespace(final @NotNull String namespace) {
204207
* @since 4.12.0
205208
*/
206209
static boolean parseableValue(final @NotNull String value) {
207-
return !checkValue(value).isPresent();
210+
return checkValue(value).isEmpty();
208211
}
209212

210213
/**

key/src/main/java/net/kyori/adventure/key/KeyImpl.java

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,14 @@
3535

3636
import static java.util.Objects.requireNonNull;
3737

38-
final class KeyImpl implements Key {
38+
record KeyImpl(String namespace, String value) implements Key {
3939
static final Comparator<? super Key> COMPARATOR = Comparator.comparing(Key::value).thenComparing(Key::namespace);
4040

4141
static final @RegExp String NAMESPACE_PATTERN = "[a-z0-9_\\-.]+";
4242
static final @RegExp String VALUE_PATTERN = "[a-z0-9_\\-./]+";
4343

44-
private final String namespace;
45-
private final String value;
46-
47-
KeyImpl(final @NotNull String namespace, final @NotNull String value) {
48-
checkError("namespace", namespace, namespace, value, Key.checkNamespace(namespace), NAMESPACE_PATTERN);
49-
checkError("value", value, namespace, value, Key.checkValue(value), VALUE_PATTERN);
50-
this.namespace = requireNonNull(namespace, "namespace");
51-
this.value = requireNonNull(value, "value");
52-
}
53-
54-
private static void checkError(final String name, final String checkPart, final String namespace, final String value, final OptionalInt index, final String pattern) {
44+
static void checkError(final String name, final String checkPart, final String namespace, final String value, final OptionalInt index, final String pattern) {
45+
requireNonNull(checkPart, name);
5546
if (index.isPresent()) {
5647
final int indexValue = index.getAsInt();
5748
final char character = checkPart.charAt(indexValue);
@@ -74,16 +65,6 @@ static boolean allowedInValue(final char character) {
7465
return character == '_' || character == '-' || (character >= 'a' && character <= 'z') || (character >= '0' && character <= '9') || character == '.' || character == '/';
7566
}
7667

77-
@Override
78-
public @NotNull String namespace() {
79-
return this.namespace;
80-
}
81-
82-
@Override
83-
public @NotNull String value() {
84-
return this.value;
85-
}
86-
8768
@Override
8869
public @NotNull String asString() {
8970
return asString(this.namespace, this.value);
@@ -109,18 +90,10 @@ static boolean allowedInValue(final char character) {
10990
@Override
11091
public boolean equals(final Object other) {
11192
if (this == other) return true;
112-
if (!(other instanceof Key)) return false;
113-
final Key that = (Key) other;
93+
if (!(other instanceof Key that)) return false;
11494
return Objects.equals(this.namespace, that.namespace()) && Objects.equals(this.value, that.value());
11595
}
11696

117-
@Override
118-
public int hashCode() {
119-
int result = this.namespace.hashCode();
120-
result = (31 * result) + this.value.hashCode();
121-
return result;
122-
}
123-
12497
@Override
12598
public int compareTo(final @NotNull Key that) {
12699
return Key.super.compareTo(that);

key/src/main/java/net/kyori/adventure/key/KeyedValue.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package net.kyori.adventure.key;
2525

26-
import org.jetbrains.annotations.ApiStatus;
2726
import org.jetbrains.annotations.NotNull;
2827

2928
import static java.util.Objects.requireNonNull;
@@ -48,22 +47,6 @@ public interface KeyedValue<T> extends Keyed {
4847
return new KeyedValueImpl<>(key, requireNonNull(value, "value"));
4948
}
5049

51-
/**
52-
* Creates a link.
53-
*
54-
* @param key the key
55-
* @param value the value
56-
* @param <T> the value type
57-
* @return the keyed
58-
* @since 4.0.0
59-
* @deprecated for removal since 4.10.0, use {@link #keyedValue(Key, Object)} instead.
60-
*/
61-
@Deprecated
62-
@ApiStatus.ScheduledForRemoval(inVersion = "5.0.0")
63-
static <T> @NotNull KeyedValue<T> of(final @NotNull Key key, final @NotNull T value) {
64-
return new KeyedValueImpl<>(key, requireNonNull(value, "value"));
65-
}
66-
6750
/**
6851
* Gets the value.
6952
*

key/src/main/java/net/kyori/adventure/key/KeyedValueImpl.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,13 @@
3030
import org.jetbrains.annotations.NotNull;
3131
import org.jetbrains.annotations.Nullable;
3232

33-
final class KeyedValueImpl<T> implements Examinable, KeyedValue<T> {
34-
private final Key key;
35-
private final T value;
36-
37-
KeyedValueImpl(final Key key, final T value) {
38-
this.key = key;
39-
this.value = value;
40-
}
41-
42-
@Override
43-
public @NotNull Key key() {
44-
return this.key;
45-
}
46-
47-
@Override
48-
public @NotNull T value() {
49-
return this.value;
50-
}
33+
record KeyedValueImpl<T>(Key key, T value) implements Examinable, KeyedValue<T> {
5134

5235
@Override
5336
public boolean equals(final @Nullable Object other) {
5437
if (this == other) return true;
55-
if (other == null || this.getClass() != other.getClass()) return false;
56-
final KeyedValueImpl<?> that = (KeyedValueImpl<?>) other;
57-
return this.key.equals(that.key) && this.value.equals(that.value);
58-
}
59-
60-
@Override
61-
public int hashCode() {
62-
int result = this.key.hashCode();
63-
result = (31 * result) + this.value.hashCode();
64-
return result;
38+
if (!(other instanceof KeyedValue<?> that)) return false;
39+
return this.key.equals(that.key()) && this.value.equals(that.value());
6540
}
6641

6742
@Override
@@ -73,7 +48,7 @@ public int hashCode() {
7348
}
7449

7550
@Override
76-
public String toString() {
51+
public @NotNull String toString() {
7752
return this.examine(StringExaminer.simpleEscaping());
7853
}
7954
}

key/src/test/java/net/kyori/adventure/key/KeyTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void testOfNamespaceAndValueParsed() {
5353
assertEquals("empty", key.value());
5454
}
5555

56+
@SuppressWarnings("PatternValidation") // We are testing to ensure this throws an exception.
5657
@Test
5758
void testOfInvalid() {
5859
assertEquals("!", assertThrows(InvalidKeyException.class, () -> Key.key("!")).keyValue());
@@ -115,6 +116,7 @@ void testParseableValue() {
115116
assertTrue(Key.parseableValue("some/path"));
116117
}
117118

119+
@SuppressWarnings("PatternValidation") // We are testing to ensure this throws an exception.
118120
@Test
119121
void testNulChar() {
120122
assertThrows(InvalidKeyException.class, () -> Key.key("carbon:global\0\0\0"));
@@ -125,6 +127,7 @@ void testParseWithEmptyNamespace() {
125127
assertEquals(Key.key(Key.MINECRAFT_NAMESPACE, "test"), Key.key(":test"));
126128
}
127129

130+
@SuppressWarnings("PatternValidation") // We are testing to ensure this throws an exception.
128131
@Test
129132
void testKeyWithInvalidCharacter() {
130133
assertThrows(InvalidKeyException.class, () -> Key.key("a/b", "a"));

0 commit comments

Comments
 (0)