Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(key): expose a Comparator for Key #645

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions key/src/main/java/net/kyori/adventure/key/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.key;

import java.util.Comparator;
import java.util.stream.Stream;
import net.kyori.examination.Examinable;
import net.kyori.examination.ExaminableProperty;
Expand Down Expand Up @@ -129,6 +130,17 @@ public interface Key extends Comparable<Key>, Examinable {
return new KeyImpl(namespace, value);
}

/**
* Gets the comparator.
*
* <p>The {@link #value() value} is compared first, followed by the {@link #namespace() namespace}.</p>
*
* @since 4.10.0
*/
static @NotNull Comparator<? super Key> comparator() {
return KeyImpl.COMPARATOR;
}

/**
* Gets the namespace.
*
Expand Down Expand Up @@ -163,10 +175,6 @@ public interface Key extends Comparable<Key>, Examinable {

@Override
default int compareTo(final @NotNull Key that) {
final int value = this.value().compareTo(that.value());
if (value != 0) {
return KeyImpl.clampCompare(value);
}
return KeyImpl.clampCompare(this.namespace().compareTo(that.namespace()));
return comparator().compare(this, that);
}
}
9 changes: 3 additions & 6 deletions key/src/main/java/net/kyori/adventure/key/KeyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.key;

import java.util.Comparator;
import java.util.Objects;
import java.util.function.IntPredicate;
import java.util.stream.Stream;
Expand All @@ -33,6 +34,8 @@
import static java.util.Objects.requireNonNull;

final class KeyImpl implements Key {
static final Comparator<? super Key> COMPARATOR = Comparator.comparing(Key::value).thenComparing(Key::namespace);

static final String NAMESPACE_PATTERN = "[a-z0-9_\\-.]+";
static final String VALUE_PATTERN = "[a-z0-9_\\-./]+";

Expand Down Expand Up @@ -119,10 +122,4 @@ public int hashCode() {
public int compareTo(final @NotNull Key that) {
return Key.super.compareTo(that);
}

static int clampCompare(final int value) {
if (value < 0) return -1;
if (value > 0) return 1;
return value;
}
}
4 changes: 2 additions & 2 deletions key/src/test/java/net/kyori/adventure/key/KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ void testEquality() {

@Test
void testCompare() {
assertEquals(-1, Key.key("air").compareTo(Key.key("stone")));
assertTrue(Key.key("air").compareTo(Key.key("stone")) < 0);
assertEquals(0, Key.key("empty").compareTo(Key.key("empty")));
assertEquals(1, Key.key("stone").compareTo(Key.key("air")));
assertTrue(Key.key("stone").compareTo(Key.key("air")) > 0);
}

@Test
Expand Down