From d29ebe53ef5337d5f6e07850d9f4ca4e44899bb2 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 2 Apr 2022 10:53:10 -0700 Subject: [PATCH 1/5] Add orThrow methods to Index --- .../java/net/kyori/adventure/util/Index.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/api/src/main/java/net/kyori/adventure/util/Index.java b/api/src/main/java/net/kyori/adventure/util/Index.java index 47db3ac51..22029d4d7 100644 --- a/api/src/main/java/net/kyori/adventure/util/Index.java +++ b/api/src/main/java/net/kyori/adventure/util/Index.java @@ -29,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Set; import java.util.function.Function; import java.util.function.IntFunction; @@ -154,6 +155,22 @@ private Index(final Map keyToValue, final Map valueToKey) { return this.valueToKey.get(value); } + /** + * Gets the key for a value or throws an exception. + * + * @param value the value + * @return the key + * @throws NoSuchElementException if there is no key for the value + * @since 4.11.0 + */ + public @NotNull K keyOrThrow(final @NotNull V value) { + final K key = this.key(value); + if (key == null) { + throw new NoSuchElementException("There is no key for value " + value); + } + return key; + } + /** * Gets the keys. * @@ -175,6 +192,22 @@ private Index(final Map keyToValue, final Map valueToKey) { return this.keyToValue.get(key); } + /** + * Gets a value by its key. + * + * @param key the key + * @return the value + * @throws NoSuchElementException if there is no value for the key + * @since 4.11.0 + */ + public @NotNull V valueOrThrow(final @NotNull K key) { + final V value = this.value(key); + if (value == null) { + throw new NoSuchElementException("There is no value for key " + key); + } + return value; + } + /** * Get an unmodifiable mapping of index entries from key to value. * From c4554b10d8862b637778055484e7586f5f80c916 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 2 Apr 2022 10:56:31 -0700 Subject: [PATCH 2/5] add test --- .../net/kyori/adventure/util/IndexTest.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/api/src/test/java/net/kyori/adventure/util/IndexTest.java b/api/src/test/java/net/kyori/adventure/util/IndexTest.java index 6219eb350..eeda45740 100644 --- a/api/src/test/java/net/kyori/adventure/util/IndexTest.java +++ b/api/src/test/java/net/kyori/adventure/util/IndexTest.java @@ -23,6 +23,7 @@ */ package net.kyori.adventure.util; +import java.util.NoSuchElementException; import java.util.UUID; import org.junit.jupiter.api.Test; @@ -31,7 +32,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; class IndexTest { - private static final Index THINGS = Index.create(Thing.class, thing -> thing.name); + private static final Index THINGS = Index.create(Thing.class, thing -> thing.name, Thing.ABC, Thing.DEF); @Test void testCreateWithNonUniqueKey() { @@ -46,6 +47,7 @@ void testCreateWithNonUniqueValue() { @Test void testKey() { for (final Thing thing : Thing.values()) { + if (thing == Thing.NOT_PRESENT) continue; assertEquals(thing.name, THINGS.key(thing)); } } @@ -53,10 +55,21 @@ void testKey() { @Test void testValue() { for (final Thing thing : Thing.values()) { + if (thing == Thing.NOT_PRESENT) continue; assertEquals(thing, THINGS.value(thing.name)); } } + @Test + void testNoValue() { + assertThrows(NoSuchElementException.class, () -> THINGS.valueOrThrow("__NO_VALUE__")); + } + + @Test + void testNoKey() { + assertThrows(NoSuchElementException.class, () -> THINGS.keyOrThrow(Thing.NOT_PRESENT)); + } + @Test void testKeys() { assertThat(THINGS.keys()).containsExactly("abc", "def"); @@ -69,7 +82,8 @@ void testValues() { private enum Thing { ABC("abc"), - DEF("def"); + DEF("def"), + NOT_PRESENT("not_present"); private final String name; From aacaa6a53528c66f7bd1332108d8af7a053ec8b6 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 2 Apr 2022 11:05:20 -0700 Subject: [PATCH 3/5] Add orDefault methods --- .../java/net/kyori/adventure/util/Index.java | 32 +++++++++++++++++++ .../net/kyori/adventure/util/IndexTest.java | 14 ++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/util/Index.java b/api/src/main/java/net/kyori/adventure/util/Index.java index 22029d4d7..97ee24c1b 100644 --- a/api/src/main/java/net/kyori/adventure/util/Index.java +++ b/api/src/main/java/net/kyori/adventure/util/Index.java @@ -33,6 +33,8 @@ import java.util.Set; import java.util.function.Function; import java.util.function.IntFunction; + +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -171,6 +173,21 @@ private Index(final Map keyToValue, final Map valueToKey) { return key; } + /** + * Gets a key by its value or returns + * a fallback key. + * + * @param value the value + * @param defaultKey the fallback key + * @return the key + * @since 4.11.0 + */ + @Contract("_, null -> null; _, !null -> !null") + public K keyOrDefault(final @NotNull V value, @Nullable K defaultKey) { + final K key = this.key(value); + return key == null ? defaultKey : key; + } + /** * Gets the keys. * @@ -208,6 +225,21 @@ private Index(final Map keyToValue, final Map valueToKey) { return value; } + /** + * Gets a value by its key or returns + * a fallback value. + * + * @param key the key + * @param defaultValue the fallback value + * @return the value + * @since 4.11.0 + */ + @Contract("_, null -> null; _, !null -> !null") + public V valueOrDefault(final @NotNull K key, @Nullable V defaultValue) { + final V value = this.value(key); + return value == null ? defaultValue : value; + } + /** * Get an unmodifiable mapping of index entries from key to value. * diff --git a/api/src/test/java/net/kyori/adventure/util/IndexTest.java b/api/src/test/java/net/kyori/adventure/util/IndexTest.java index eeda45740..9a1f82a03 100644 --- a/api/src/test/java/net/kyori/adventure/util/IndexTest.java +++ b/api/src/test/java/net/kyori/adventure/util/IndexTest.java @@ -61,15 +61,25 @@ void testValue() { } @Test - void testNoValue() { + void testValueOrThrow() { assertThrows(NoSuchElementException.class, () -> THINGS.valueOrThrow("__NO_VALUE__")); } @Test - void testNoKey() { + void testKeyOrThrow() { assertThrows(NoSuchElementException.class, () -> THINGS.keyOrThrow(Thing.NOT_PRESENT)); } + @Test + void testValueOrDefault() { + assertEquals(Thing.NOT_PRESENT, THINGS.valueOrDefault("__NO_VALUE__", Thing.NOT_PRESENT)); + } + + @Test + void testKeyOrDefault() { + assertEquals("__DEFAULT__", THINGS.keyOrDefault(Thing.NOT_PRESENT, "__DEFAULT__")); + } + @Test void testKeys() { assertThat(THINGS.keys()).containsExactly("abc", "def"); From 9822db8f17abbd340717ee18f888a5c464eb4f74 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 2 Apr 2022 11:17:06 -0700 Subject: [PATCH 4/5] Fix checkstyle errors --- api/src/main/java/net/kyori/adventure/util/Index.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/util/Index.java b/api/src/main/java/net/kyori/adventure/util/Index.java index 97ee24c1b..987dc7a54 100644 --- a/api/src/main/java/net/kyori/adventure/util/Index.java +++ b/api/src/main/java/net/kyori/adventure/util/Index.java @@ -33,7 +33,6 @@ import java.util.Set; import java.util.function.Function; import java.util.function.IntFunction; - import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -183,7 +182,7 @@ private Index(final Map keyToValue, final Map valueToKey) { * @since 4.11.0 */ @Contract("_, null -> null; _, !null -> !null") - public K keyOrDefault(final @NotNull V value, @Nullable K defaultKey) { + public K keyOrDefault(final @NotNull V value, final @Nullable K defaultKey) { final K key = this.key(value); return key == null ? defaultKey : key; } @@ -235,7 +234,7 @@ public K keyOrDefault(final @NotNull V value, @Nullable K defaultKey) { * @since 4.11.0 */ @Contract("_, null -> null; _, !null -> !null") - public V valueOrDefault(final @NotNull K key, @Nullable V defaultValue) { + public V valueOrDefault(final @NotNull K key, final @Nullable V defaultValue) { final V value = this.value(key); return value == null ? defaultValue : value; } From 4d5c135e1540f454f3decd8e3496636a65571159 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 2 Apr 2022 11:18:26 -0700 Subject: [PATCH 5/5] small fixes --- api/src/main/java/net/kyori/adventure/util/Index.java | 10 ++++------ .../test/java/net/kyori/adventure/util/IndexTest.java | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/util/Index.java b/api/src/main/java/net/kyori/adventure/util/Index.java index 987dc7a54..4b56650c4 100644 --- a/api/src/main/java/net/kyori/adventure/util/Index.java +++ b/api/src/main/java/net/kyori/adventure/util/Index.java @@ -173,8 +173,7 @@ private Index(final Map keyToValue, final Map valueToKey) { } /** - * Gets a key by its value or returns - * a fallback key. + * Gets a key by its value or returns a fallback key. * * @param value the value * @param defaultKey the fallback key @@ -182,7 +181,7 @@ private Index(final Map keyToValue, final Map valueToKey) { * @since 4.11.0 */ @Contract("_, null -> null; _, !null -> !null") - public K keyOrDefault(final @NotNull V value, final @Nullable K defaultKey) { + public K keyOr(final @NotNull V value, final @Nullable K defaultKey) { final K key = this.key(value); return key == null ? defaultKey : key; } @@ -225,8 +224,7 @@ public K keyOrDefault(final @NotNull V value, final @Nullable K defaultKey) { } /** - * Gets a value by its key or returns - * a fallback value. + * Gets a value by its key or returns a fallback value. * * @param key the key * @param defaultValue the fallback value @@ -234,7 +232,7 @@ public K keyOrDefault(final @NotNull V value, final @Nullable K defaultKey) { * @since 4.11.0 */ @Contract("_, null -> null; _, !null -> !null") - public V valueOrDefault(final @NotNull K key, final @Nullable V defaultValue) { + public V valueOr(final @NotNull K key, final @Nullable V defaultValue) { final V value = this.value(key); return value == null ? defaultValue : value; } diff --git a/api/src/test/java/net/kyori/adventure/util/IndexTest.java b/api/src/test/java/net/kyori/adventure/util/IndexTest.java index 9a1f82a03..d8eb857ad 100644 --- a/api/src/test/java/net/kyori/adventure/util/IndexTest.java +++ b/api/src/test/java/net/kyori/adventure/util/IndexTest.java @@ -72,12 +72,12 @@ void testKeyOrThrow() { @Test void testValueOrDefault() { - assertEquals(Thing.NOT_PRESENT, THINGS.valueOrDefault("__NO_VALUE__", Thing.NOT_PRESENT)); + assertEquals(Thing.NOT_PRESENT, THINGS.valueOr("__NO_VALUE__", Thing.NOT_PRESENT)); } @Test void testKeyOrDefault() { - assertEquals("__DEFAULT__", THINGS.keyOrDefault(Thing.NOT_PRESENT, "__DEFAULT__")); + assertEquals("__DEFAULT__", THINGS.keyOr(Thing.NOT_PRESENT, "__DEFAULT__")); } @Test