Changes to option keys

This commit is contained in:
Luck committed 2019-10-12 00:27:53 +01:00
1 parent fd954d8293
commit 9438df787a
12 files changed
+134 -38

No files matched your search

@@ -45,12 +45,12 @@ public interface MetaStackDefinition {
/**
* The {@link OptionKey} for the prefix {@link MetaStackDefinition}.
*/
OptionKey<MetaStackDefinition> PREFIX_STACK_KEY = new OptionKey<MetaStackDefinition>(){};
OptionKey<MetaStackDefinition> PREFIX_STACK_KEY = OptionKey.of("prefixstack", MetaStackDefinition.class);
/**
* The {@link OptionKey} for the suffix {@link MetaStackDefinition}.
*/
OptionKey<MetaStackDefinition> SUFFIX_STACK_KEY = new OptionKey<MetaStackDefinition>(){};
OptionKey<MetaStackDefinition> SUFFIX_STACK_KEY = OptionKey.of("suffixstack", MetaStackDefinition.class);
/**
* Gets an immutable list of the elements in this stack definition
@@ -32,13 +32,10 @@ import java.util.Objects;
/**
* Represents a key for a specific type of node metadata.
*
* <p>Metadata keys are compared using reference equality, the
* {@link #equals(Object)} method should not be implemented.</p>
*
* <p>It is intended that {@link NodeMetadataKey}s are obtained and stored as
* follows.</p>
* <p><blockquote><pre>
* public static final NodeMetadataKey<SpecialMetadata> SPECIAL_METADATA_KEY = NodeMetadataKey.of("SpecialMetadata", SpecialMetadata.class);
* public static final NodeMetadataKey<SpecialMetadata> SPECIAL_METADATA_KEY = NodeMetadataKey.of("specialmetadata", SpecialMetadata.class);
* </pre></blockquote></p>
*
* @param <T> the metadata type
@@ -27,12 +27,14 @@ package net.luckperms.api.node.metadata;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Objects;
final class SimpleNodeMetadataKey<T> implements NodeMetadataKey<T> {
private final String name;
private final Class<T> type;
SimpleNodeMetadataKey(String name, Class<T> type) {
this.name = name;
this.name = name.toLowerCase();
this.type = type;
}
@@ -50,4 +52,18 @@ final class SimpleNodeMetadataKey<T> implements NodeMetadataKey<T> {
public String toString() {
return "NodeMetadataKey(name=" + this.name + ", type=" + this.type.getName() + ")";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimpleNodeMetadataKey<?> that = (SimpleNodeMetadataKey<?>) o;
return this.name.equals(that.name) &&
this.type.equals(that.type);
}
@Override
public int hashCode() {
return Objects.hash(this.name, this.type);
}
}
@@ -39,7 +39,7 @@ public interface InheritanceOriginMetadata {
/**
* The {@link NodeMetadataKey} for {@link InheritanceOriginMetadata}.
*/
NodeMetadataKey<InheritanceOriginMetadata> KEY = NodeMetadataKey.of("InheritanceOrigin", InheritanceOriginMetadata.class);
NodeMetadataKey<InheritanceOriginMetadata> KEY = NodeMetadataKey.of("inheritanceorigin", InheritanceOriginMetadata.class);
/**
* Gets the location where the {@link Node} is inherited from.
@@ -25,19 +25,52 @@
package net.luckperms.api.query;
import net.luckperms.api.node.metadata.NodeMetadataKey;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Objects;
/**
* Represents a key for a custom option defined in {@link QueryOptions}.
*
* <p>Option keys are compared using reference equality, the
* {@link #equals(Object)} method should not be implemented.</p>
*
* <p>It is intended that {@link OptionKey}s are created and defined as follows.</p>
* <p><blockquote><pre>
* public static final OptionKey<String> SPECIAL_OPTION = new OptionKey<String>(){};
* public static final OptionKey<String> SPECIAL_OPTION = OptionKey.of("special", String.class);
* </pre></blockquote></p>
*
* @param <O> the option type
* @param <T> the option type
*/
public interface OptionKey<O> {
public interface OptionKey<T> {
/**
* Creates a new {@link NodeMetadataKey} for the given name and type.
*
* <p>Note that the returned key implements object reference equality.</p>
*
* @param name the name
* @param type the type
* @param <T> the type parameter
* @return the key
*/
static <T> @NonNull OptionKey<T> of(@NonNull String name, @NonNull Class<T> type) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(type, "type");
return new SimpleOptionKey<>(name, type);
}
/**
* Gets a name describing the key type.
*
* @return the key name
*/
@NonNull String name();
/**
* Gets the type of the key
*
* @return the type
*/
@NonNull Class<T> getType();
}
@@ -0,0 +1,69 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.luckperms.api.query;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Objects;
final class SimpleOptionKey<T> implements OptionKey<T> {
private final String name;
private final Class<T> type;
SimpleOptionKey(String name, Class<T> type) {
this.name = name.toLowerCase();
this.type = type;
}
@Override
public @NonNull String name() {
return this.name;
}
@Override
public @NonNull Class<T> getType() {
return this.type;
}
@Override
public String toString() {
return "OptionKey(name=" + this.name + ", type=" + this.type.getName() + ")";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimpleOptionKey<?> that = (SimpleOptionKey<?>) o;
return this.name.equals(that.name) &&
this.type.equals(that.type);
}
@Override
public int hashCode() {
return Objects.hash(this.name, this.type);
}
}
@@ -42,7 +42,7 @@ public interface DataQueryOrderFunction {
/**
* The {@link OptionKey} for {@link DataQueryOrderFunction}.
*/
OptionKey<DataQueryOrderFunction> KEY = new OptionKey<DataQueryOrderFunction>(){};
OptionKey<DataQueryOrderFunction> KEY = OptionKey.of("dataqueryorderfunction", DataQueryOrderFunction.class);
/**
* Gets the {@link DataQueryOrder} comparator for the given