|
| 1 | +/* |
| 2 | + * This file is part of option, licensed under the MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) 2025 KyoriPowered |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package net.kyori.option; |
| 25 | + |
| 26 | +import java.util.Set; |
| 27 | +import org.jetbrains.annotations.ApiStatus; |
| 28 | +import org.jetbrains.annotations.NotNull; |
| 29 | +import org.jetbrains.annotations.Nullable; |
| 30 | + |
| 31 | +import static java.util.Objects.requireNonNull; |
| 32 | + |
| 33 | +/** |
| 34 | + * Represents a 'universe' of known options. |
| 35 | + * |
| 36 | + * @since 1.1.0 |
| 37 | + */ |
| 38 | +@ApiStatus.NonExtendable |
| 39 | +public interface OptionSchema { |
| 40 | + /** |
| 41 | + * Retrieve the globally-shared option schema. |
| 42 | + * |
| 43 | + * <p>This mostly exists for backwards compatibility, and should not be used in new software.</p> |
| 44 | + * |
| 45 | + * @return the global schema |
| 46 | + * @since 1.1.0 |
| 47 | + */ |
| 48 | + static OptionSchema.@NotNull Mutable globalSchema() { |
| 49 | + return OptionSchemaImpl.Instances.GLOBAL; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Return a mutable schema that's a child of the specified schema. |
| 54 | + * |
| 55 | + * <p>This schema will inherit all options defined in the parent schema at the time the child schema is created.</p> |
| 56 | + * |
| 57 | + * @param schema the parent schema |
| 58 | + * @return the mutable child schema |
| 59 | + * @since 1.1.0 |
| 60 | + */ |
| 61 | + static OptionSchema.@NotNull Mutable childSchema(final @NotNull OptionSchema schema) { |
| 62 | + final OptionSchemaImpl impl; |
| 63 | + if (schema instanceof OptionSchemaImpl.MutableImpl) { |
| 64 | + impl = (OptionSchemaImpl) ((Mutable) schema).frozenView(); |
| 65 | + } else { |
| 66 | + impl = (OptionSchemaImpl) schema; |
| 67 | + } |
| 68 | + |
| 69 | + return new OptionSchemaImpl(requireNonNull(impl, "impl")).new MutableImpl(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Create an empty schema inheriting from nothing but the contents |
| 74 | + * of the global schema at invocation time. |
| 75 | + * |
| 76 | + * @return a mutable schema |
| 77 | + * @since 1.1.0 |
| 78 | + */ |
| 79 | + static OptionSchema.@NotNull Mutable emptySchema() { |
| 80 | + return new OptionSchemaImpl(null).new MutableImpl(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Return all known options contained within this schema, and recursively through its parents. |
| 85 | + * |
| 86 | + * @return known options |
| 87 | + * @since 1.1.0 |
| 88 | + */ |
| 89 | + @NotNull Set<Option<?>> knownOptions(); |
| 90 | + |
| 91 | + /** |
| 92 | + * Return whether the provided option is known within this scheam. |
| 93 | + * |
| 94 | + * @param option the option |
| 95 | + * @return whether the option is known |
| 96 | + * @since 1.1.0 |
| 97 | + */ |
| 98 | + boolean has(final @NotNull Option<?> option); |
| 99 | + |
| 100 | + /** |
| 101 | + * Create a builder for an unversioned option state containing only options within this schema. |
| 102 | + * |
| 103 | + * @return the builder |
| 104 | + * @since 1.1.0 |
| 105 | + */ |
| 106 | + OptionState.@NotNull Builder stateBuilder(); |
| 107 | + |
| 108 | + /** |
| 109 | + * Create a builder for a versioned option state containing only values for options within this schema. |
| 110 | + * |
| 111 | + * @return the builder |
| 112 | + * @since 1.1.0 |
| 113 | + */ |
| 114 | + OptionState.@NotNull VersionedBuilder versionedStateBuilder(); |
| 115 | + |
| 116 | + /** |
| 117 | + * Create an empty option state within this schema. |
| 118 | + * |
| 119 | + * @return the empty state |
| 120 | + * @since 1.1.0 |
| 121 | + */ |
| 122 | + OptionState emptyState(); |
| 123 | + |
| 124 | + /** |
| 125 | + * A mutable view of an option schema that allows registering new options into the schema. |
| 126 | + * |
| 127 | + * @since 1.1.0 |
| 128 | + */ |
| 129 | + @ApiStatus.NonExtendable |
| 130 | + interface Mutable extends OptionSchema { |
| 131 | + /** |
| 132 | + * Create an option with a string value type. |
| 133 | + * |
| 134 | + * <p>Flag keys must not be reused within a schema tree.</p> |
| 135 | + * |
| 136 | + * @param id the flag id |
| 137 | + * @param defaultValue the default value |
| 138 | + * @return the flag instance |
| 139 | + * @since 1.1.0 |
| 140 | + */ |
| 141 | + @NotNull Option<String> stringOption(final @NotNull String id, final @Nullable String defaultValue); |
| 142 | + |
| 143 | + /** |
| 144 | + * Create an option with a boolean value type. |
| 145 | + * |
| 146 | + * <p>Flag keys must not be reused within a schema tree.</p> |
| 147 | + * |
| 148 | + * @param id the flag id |
| 149 | + * @param defaultValue the default value |
| 150 | + * @return the flag instance |
| 151 | + * @since 1.1.0 |
| 152 | + */ |
| 153 | + @NotNull Option<Boolean> booleanOption(final @NotNull String id, final boolean defaultValue); |
| 154 | + |
| 155 | + /** |
| 156 | + * Create an option with an integer value type. |
| 157 | + * |
| 158 | + * <p>Flag keys must not be reused within a schema tree.</p> |
| 159 | + * |
| 160 | + * @param id the flag id |
| 161 | + * @param defaultValue the default value |
| 162 | + * @return the flag instance |
| 163 | + * @since 1.1.0 |
| 164 | + */ |
| 165 | + @NotNull Option<Integer> intOption(final @NotNull String id, final int defaultValue); |
| 166 | + |
| 167 | + /** |
| 168 | + * Create an option with a double value type. |
| 169 | + * |
| 170 | + * <p>Flag keys must not be reused within a schema tree.</p> |
| 171 | + * |
| 172 | + * @param id the flag id |
| 173 | + * @param defaultValue the default value |
| 174 | + * @return the flag instance |
| 175 | + * @since 1.1.0 |
| 176 | + */ |
| 177 | + @NotNull Option<Double> doubleOption(final @NotNull String id, final double defaultValue); |
| 178 | + |
| 179 | + /** |
| 180 | + * Create an option with an enum value type. |
| 181 | + * |
| 182 | + * <p>Flag keys must not be reused within a schema tree.</p> |
| 183 | + * |
| 184 | + * @param id the flag id |
| 185 | + * @param enumClazz the value type |
| 186 | + * @param defaultValue the default value |
| 187 | + * @param <E> the enum type |
| 188 | + * @return the flag instance |
| 189 | + * @since 1.1.0 |
| 190 | + */ |
| 191 | + <E extends Enum<E>> @NotNull Option<E> enumOption(final @NotNull String id, final @NotNull Class<E> enumClazz, final @Nullable E defaultValue); |
| 192 | + |
| 193 | + /** |
| 194 | + * Return a view of this schema which does not allow consumers to register new options. |
| 195 | + * |
| 196 | + * <p>This allows exposing known options within a schema without the risk of having values polluted.</p> |
| 197 | + * |
| 198 | + * @return the frozen view of this schema |
| 199 | + * @since 1.1.0 |
| 200 | + */ |
| 201 | + @NotNull OptionSchema frozenView(); |
| 202 | + } |
| 203 | +} |
0 commit comments