From df324de9ece55d6368214b2886f01dfc884213a6 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 12 Jun 2020 23:14:33 +0100 Subject: [PATCH] Add DataTypeFilter --- .../api/query/dataorder/DataQueryOrder.java | 2 + .../dataorder/DataQueryOrderFunction.java | 12 ++ .../api/query/dataorder/DataTypeFilter.java | 110 ++++++++++++++++++ .../dataorder/DataTypeFilterFunction.java | 69 +++++++++++ .../common/model/PermissionHolder.java | 11 +- .../luckperms/common/query/DataSelector.java | 84 +++++++++++++ 6 files changed, 280 insertions(+), 8 deletions(-) create mode 100644 api/src/main/java/net/luckperms/api/query/dataorder/DataTypeFilter.java create mode 100644 api/src/main/java/net/luckperms/api/query/dataorder/DataTypeFilterFunction.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/query/DataSelector.java diff --git a/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrder.java b/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrder.java index 3abb672c1..db133c252 100644 --- a/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrder.java +++ b/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrder.java @@ -48,6 +48,8 @@ import java.util.function.Consumer; * it is possible that more {@link DataType}s may be added in the future. * Ideally the {@link Comparator} implementations should be able to handle these * smoothly.

+ * + * @see DataQueryOrderFunction */ public enum DataQueryOrder implements Comparator { diff --git a/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrderFunction.java b/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrderFunction.java index 3f32b4b72..60be40080 100644 --- a/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrderFunction.java +++ b/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrderFunction.java @@ -44,6 +44,18 @@ public interface DataQueryOrderFunction { */ OptionKey KEY = OptionKey.of("dataqueryorderfunction", DataQueryOrderFunction.class); + /** + * Creates a {@link DataQueryOrderFunction} that always returns the given + * {@code comparator}. + * + * @param comparator the comparator + * @return the data query order function + * @since 5.2 + */ + static DataQueryOrderFunction always(Comparator comparator) { + return id -> comparator; + } + /** * Gets the {@link DataQueryOrder} comparator for the given * {@link PermissionHolder.Identifier holder identifier}. diff --git a/api/src/main/java/net/luckperms/api/query/dataorder/DataTypeFilter.java b/api/src/main/java/net/luckperms/api/query/dataorder/DataTypeFilter.java new file mode 100644 index 000000000..9edc0860f --- /dev/null +++ b/api/src/main/java/net/luckperms/api/query/dataorder/DataTypeFilter.java @@ -0,0 +1,110 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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.dataorder; + +import net.luckperms.api.model.data.DataType; +import net.luckperms.api.query.QueryOptions; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.function.Predicate; + +/** + * Represents which different {@link DataType}s are used for a query. + * + *

The {@link DataTypeFilter} enum simply represents some default + * implementations of the {@link Predicate} required by {@link QueryOptions} + * and the {@link #values(Predicate)} method.

+ * + *

Users are free to implement their own predicate. However, be aware that + * it is possible that more {@link DataType}s may be added in the future. + * Ideally the {@link Predicate} implementations should be able to handle these + * smoothly.

+ * + * @see DataTypeFilterFunction + * @since 5.2 + */ +public enum DataTypeFilter implements Predicate { + + /** + * A data type filter indicating that all {@link DataType}s should be used. + */ + ALL { + @Override + public boolean test(DataType dataType) { + return true; + } + }, + + /** + * A data type filter indicating that only {@link DataType#NORMAL} should be used. + */ + NORMAL_ONLY { + @Override + public boolean test(DataType dataType) { + return dataType == DataType.NORMAL; + } + }, + + /** + * A data type filter indicating that only {@link DataType#TRANSIENT} should be used. + */ + TRANSIENT_ONLY { + @Override + public boolean test(DataType dataType) { + return dataType == DataType.TRANSIENT; + } + }; + + private static final List ALL_LIST = Collections.unmodifiableList(Arrays.asList(DataType.NORMAL, DataType.TRANSIENT)); + private static final List NORMAL_ONLY_LIST = Collections.unmodifiableList(Collections.singletonList(DataType.NORMAL)); + private static final List TRANSIENT_ONLY_LIST = Collections.unmodifiableList(Collections.singletonList(DataType.TRANSIENT)); + + /** + * Gets a {@link List} of all {@link DataType}s, filtered by the {@code predicate}. + * + * @param predicate the predicate to filter with + * @return the list of data types + */ + public static @NonNull List values(@NonNull Predicate predicate) { + boolean normal = predicate.test(DataType.NORMAL); + boolean trans = predicate.test(DataType.TRANSIENT); + + if (normal && trans) { + return ALL_LIST; + } else if (normal) { + return NORMAL_ONLY_LIST; + } else if (trans) { + return TRANSIENT_ONLY_LIST; + } else { + return Collections.emptyList(); + } + } + +} diff --git a/api/src/main/java/net/luckperms/api/query/dataorder/DataTypeFilterFunction.java b/api/src/main/java/net/luckperms/api/query/dataorder/DataTypeFilterFunction.java new file mode 100644 index 000000000..0fc5e6ff0 --- /dev/null +++ b/api/src/main/java/net/luckperms/api/query/dataorder/DataTypeFilterFunction.java @@ -0,0 +1,69 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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.dataorder; + +import net.luckperms.api.model.PermissionHolder; +import net.luckperms.api.model.data.DataType; +import net.luckperms.api.query.OptionKey; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.function.Predicate; + +/** + * A function that generates a {@link DataTypeFilter} predicate for + * {@link PermissionHolder}s as required during inheritance. + * + * @since 5.2 + */ +public interface DataTypeFilterFunction { + + /** + * The {@link OptionKey} for {@link DataTypeFilterFunction}. + */ + OptionKey KEY = OptionKey.of("datatypefilterfunction", DataTypeFilterFunction.class); + + /** + * Creates a {@link DataTypeFilterFunction} that always returns the given + * {@code predicate}. + * + * @param predicate the predicate + * @return the data type filter function + */ + static DataTypeFilterFunction always(Predicate predicate) { + return id -> predicate; + } + + /** + * Gets the {@link DataTypeFilter} predicate for the given + * {@link PermissionHolder.Identifier holder identifier}. + * + * @param holderIdentifier the holder identifier + * @return the predicate to use + */ + @NonNull Predicate getTypeFilter(PermissionHolder.@NonNull Identifier holderIdentifier); + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java index 6b9990578..3d4c1cabf 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java @@ -36,6 +36,7 @@ import me.lucko.luckperms.common.inheritance.InheritanceComparator; import me.lucko.luckperms.common.inheritance.InheritanceGraph; import me.lucko.luckperms.common.node.comparator.NodeWithContextComparator; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.query.DataSelector; import net.luckperms.api.context.ContextSet; import net.luckperms.api.context.ImmutableContextSet; @@ -48,8 +49,6 @@ import net.luckperms.api.node.NodeType; import net.luckperms.api.node.types.InheritanceNode; import net.luckperms.api.query.Flag; import net.luckperms.api.query.QueryOptions; -import net.luckperms.api.query.dataorder.DataQueryOrder; -import net.luckperms.api.query.dataorder.DataQueryOrderFunction; import net.luckperms.api.util.Tristate; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @@ -262,12 +261,8 @@ public abstract class PermissionHolder { invalidateCache(); } - private List queryOrder(QueryOptions queryOptions) { - Comparator comparator = queryOptions.option(DataQueryOrderFunction.KEY) - .map(func -> func.getOrderComparator(getIdentifier())) - .orElse(DataQueryOrder.TRANSIENT_FIRST); - - return DataQueryOrder.order(comparator); + private DataType[] queryOrder(QueryOptions queryOptions) { + return DataSelector.select(queryOptions, getIdentifier()); } public List getOwnNodes(QueryOptions queryOptions) { diff --git a/common/src/main/java/me/lucko/luckperms/common/query/DataSelector.java b/common/src/main/java/me/lucko/luckperms/common/query/DataSelector.java new file mode 100644 index 000000000..92ba97f4b --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/query/DataSelector.java @@ -0,0 +1,84 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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 me.lucko.luckperms.common.query; + +import net.luckperms.api.model.PermissionHolder; +import net.luckperms.api.model.data.DataType; +import net.luckperms.api.query.QueryOptions; +import net.luckperms.api.query.dataorder.DataQueryOrder; +import net.luckperms.api.query.dataorder.DataQueryOrderFunction; +import net.luckperms.api.query.dataorder.DataTypeFilter; +import net.luckperms.api.query.dataorder.DataTypeFilterFunction; + +import java.util.Comparator; +import java.util.function.Predicate; + +public final class DataSelector { + private DataSelector() {} + + // These arrays are only ever exposed internally - we ensure they don't get modified + private static final DataType[] TRANSIENT_NORMAL = new DataType[]{DataType.TRANSIENT, DataType.NORMAL}; + private static final DataType[] NORMAL_TRANSIENT = new DataType[]{DataType.NORMAL, DataType.TRANSIENT}; + private static final DataType[] NORMAL = new DataType[]{DataType.NORMAL}; + private static final DataType[] TRANSIENT = new DataType[]{DataType.TRANSIENT}; + private static final DataType[] NONE = new DataType[0]; + + public static DataType[] select(QueryOptions queryOptions, PermissionHolder.Identifier identifier) { + final DataQueryOrderFunction orderFunc = queryOptions.option(DataQueryOrderFunction.KEY).orElse(null); + final DataTypeFilterFunction filterFunc = queryOptions.option(DataTypeFilterFunction.KEY).orElse(null); + + if (orderFunc == null && filterFunc == null) { + return TRANSIENT_NORMAL; + } + + Predicate predicate = filterFunc != null ? filterFunc.getTypeFilter(identifier) : DataTypeFilter.ALL; + + boolean normal = predicate.test(DataType.NORMAL); + boolean trans = predicate.test(DataType.TRANSIENT); + + // if both are included - we need to consult the order comparator + if (normal && trans) { + Comparator comparator = orderFunc != null ? orderFunc.getOrderComparator(identifier) : DataQueryOrder.TRANSIENT_FIRST; + + int compare = comparator.compare(DataType.TRANSIENT, DataType.NORMAL); + if (compare == 0) { + throw new IllegalStateException("Comparator " + comparator + " does not define an order between DataType.NORMAL and DataType.TRANSIENT!"); + } + + return compare > 0 ? TRANSIENT_NORMAL : NORMAL_TRANSIENT; + } + + // otherwise, no need to worry about order + if (normal) { + return NORMAL; + } else if (trans) { + return TRANSIENT; + } else { + return NONE; + } + } + +}