|
| 1 | +/* |
| 2 | + * This file is part of SpongeAPI, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 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 |
| 15 | + * all 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 |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.api.tag; |
| 26 | + |
| 27 | +import org.spongepowered.api.ResourceKey; |
| 28 | +import org.spongepowered.api.registry.DefaultedRegistryType; |
| 29 | +import org.spongepowered.api.registry.RegistryHolder; |
| 30 | +import org.spongepowered.api.registry.ValueNotFoundException; |
| 31 | + |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.function.Supplier; |
| 34 | +import java.util.stream.Stream; |
| 35 | + |
| 36 | +/** |
| 37 | + * A {@link Tag} paired with the {@link RegistryHolder}. |
| 38 | + */ |
| 39 | +public interface DefaultedTag<T> extends Tag<T> { |
| 40 | + |
| 41 | + static <T> DefaultedTag<T> of(final DefaultedRegistryType<T> registryType, final ResourceKey location) { |
| 42 | + return Tag.of(registryType, location).asDefaultedTag(registryType.defaultHolder()); |
| 43 | + } |
| 44 | + |
| 45 | + Supplier<RegistryHolder> defaultHolder(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the {@link Stream} of values tagged by this tag in the {@link #defaultHolder()}. |
| 49 | + * |
| 50 | + * <p>Great care needs to be made in calling this method with any uncertainty as to |
| 51 | + * if the {@link #registry()} will exist in the {@link #defaultHolder()}. Should the |
| 52 | + * key lack a value, a {@link ValueNotFoundException} will be thrown. Therefore, it |
| 53 | + * is advised to call {@link #findValues()} instead.</p> |
| 54 | + * |
| 55 | + * @return The {@link Stream} of values |
| 56 | + */ |
| 57 | + default Stream<T> values() { |
| 58 | + return this.defaultHolder().get().registry(this.registry()).taggedValues(this); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the {@link Stream} of values tagged by this tag in the {@link #defaultHolder()} |
| 63 | + * if it contains this tag's {@link #registry()}, or {@link Stream#empty()} otherwise. |
| 64 | + * |
| 65 | + * @return The {@link Stream} of values |
| 66 | + */ |
| 67 | + default Stream<T> findValues() { |
| 68 | + return this.defaultHolder().get().findRegistry(this.registry()).map(r -> r.taggedValues(this)).orElseGet(Stream::empty); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns whether this tag is associated with the given value in the {@link #defaultHolder()}. |
| 73 | + * |
| 74 | + * @param value The value |
| 75 | + * @return true if this tag is associated with the given value in the {@link #defaultHolder()} |
| 76 | + */ |
| 77 | + default boolean contains(final T value) { |
| 78 | + return this.findValues().anyMatch(Objects.requireNonNull(value, "value")::equals); |
| 79 | + } |
| 80 | +} |
0 commit comments