|
| 1 | +/* |
| 2 | + * This file is part of LuckPerms, licensed under the MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) lucko (Luck) <luck@lucko.me> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all |
| 15 | + * copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + * SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +package me.lucko.luckperms.common.cacheddata.result; |
| 27 | + |
| 28 | +import net.luckperms.api.cacheddata.Result; |
| 29 | +import net.luckperms.api.node.Node; |
| 30 | +import net.luckperms.api.node.types.WeightNode; |
| 31 | + |
| 32 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 33 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 34 | + |
| 35 | +/** |
| 36 | + * Represents the result of an integer meta lookup |
| 37 | + * |
| 38 | + * @param <N> the node type |
| 39 | + */ |
| 40 | +public final class IntegerResult<N extends Node> implements Result<Integer, N> { |
| 41 | + |
| 42 | + /** The result */ |
| 43 | + private final int result; |
| 44 | + /** The node that caused the result */ |
| 45 | + private final N node; |
| 46 | + /** A reference to another result that this one overrides */ |
| 47 | + private IntegerResult<N> overriddenResult; |
| 48 | + |
| 49 | + public IntegerResult(int result, N node, IntegerResult<N> overriddenResult) { |
| 50 | + this.result = result; |
| 51 | + this.node = node; |
| 52 | + this.overriddenResult = overriddenResult; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + @Deprecated // use intResult() |
| 57 | + public @NonNull Integer result() { |
| 58 | + return this.result; |
| 59 | + } |
| 60 | + |
| 61 | + public int intResult() { |
| 62 | + return this.result; |
| 63 | + } |
| 64 | + |
| 65 | + public StringResult<N> asStringResult() { |
| 66 | + if (isNull()) { |
| 67 | + return StringResult.nullResult(); |
| 68 | + } else { |
| 69 | + StringResult<N> result = StringResult.of(Integer.toString(this.result), this.node); |
| 70 | + if (this.overriddenResult != null) { |
| 71 | + result.setOverriddenResult(this.overriddenResult.asStringResult()); |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public @Nullable N node() { |
| 79 | + return this.node; |
| 80 | + } |
| 81 | + |
| 82 | + public @Nullable IntegerResult<N> overriddenResult() { |
| 83 | + return this.overriddenResult; |
| 84 | + } |
| 85 | + |
| 86 | + public void setOverriddenResult(IntegerResult<N> overriddenResult) { |
| 87 | + this.overriddenResult = overriddenResult; |
| 88 | + } |
| 89 | + |
| 90 | + public boolean isNull() { |
| 91 | + return this == NULL_RESULT; |
| 92 | + } |
| 93 | + |
| 94 | + public IntegerResult<N> copy() { |
| 95 | + return new IntegerResult<>(this.result, this.node, this.overriddenResult); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String toString() { |
| 100 | + return "IntegerResult(" + |
| 101 | + "result=" + this.result + ", " + |
| 102 | + "node=" + this.node + ", " + |
| 103 | + "overriddenResult=" + this.overriddenResult + ')'; |
| 104 | + } |
| 105 | + |
| 106 | + private static final IntegerResult<?> NULL_RESULT = new IntegerResult<>(0, null, null); |
| 107 | + |
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + public static <N extends Node> IntegerResult<N> nullResult() { |
| 110 | + return (IntegerResult<N>) NULL_RESULT; |
| 111 | + } |
| 112 | + |
| 113 | + public static <N extends Node> IntegerResult<N> of(int result) { |
| 114 | + return new IntegerResult<>(result, null, null); |
| 115 | + } |
| 116 | + |
| 117 | + public static <N extends Node> IntegerResult<N> of(int result, N node) { |
| 118 | + return new IntegerResult<>(result, node, null); |
| 119 | + } |
| 120 | + |
| 121 | + public static IntegerResult<WeightNode> of(WeightNode node) { |
| 122 | + return new IntegerResult<>(node.getWeight(), node, null); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments