|  | 
|  | 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.value; | 
|  | 25 | + | 
|  | 26 | +import net.kyori.option.Option; | 
|  | 27 | +import org.jetbrains.annotations.NotNull; | 
|  | 28 | +import org.jetbrains.annotations.Nullable; | 
|  | 29 | + | 
|  | 30 | +/** | 
|  | 31 | + * A source for external option values. | 
|  | 32 | + * | 
|  | 33 | + * @since 1.1.0 | 
|  | 34 | + */ | 
|  | 35 | +@FunctionalInterface | 
|  | 36 | +public interface ValueSource { | 
|  | 37 | +  /** | 
|  | 38 | +   * A value source that will extract option values from environment variables. | 
|  | 39 | +   * | 
|  | 40 | +   * <p>Any of the characters {@code :}, {@code /}, and {@code -} will be replaced with {@code _}.</p> | 
|  | 41 | +   * | 
|  | 42 | +   * @return an environment variable-backed value source with no prefix | 
|  | 43 | +   * @since 1.1.0 | 
|  | 44 | +   */ | 
|  | 45 | +  static @NotNull ValueSource environmentVariable() { | 
|  | 46 | +    return environmentVariable(""); | 
|  | 47 | +  } | 
|  | 48 | + | 
|  | 49 | +  /** | 
|  | 50 | +   * A value source that will extract option values from environment variables. | 
|  | 51 | +   * | 
|  | 52 | +   * <p>Any of the characters {@code :}, {@code /}, and {@code -} will be replaced with {@code _}.</p> | 
|  | 53 | +   * | 
|  | 54 | +   * @param prefix the prefix for environment lookup, which will be prepended to keys followed by a {@code _}, | 
|  | 55 | +   *               or the empty string for no prefix | 
|  | 56 | +   * @return an environment variable-backed value source | 
|  | 57 | +   * @since 1.1.0 | 
|  | 58 | +   */ | 
|  | 59 | +  static @NotNull ValueSource environmentVariable(final @NotNull String prefix) { | 
|  | 60 | +    return new ValueSources.EnvironmentVariable(prefix); | 
|  | 61 | +  } | 
|  | 62 | + | 
|  | 63 | +  /** | 
|  | 64 | +   * A value source that will extract option values from system properties. | 
|  | 65 | +   * | 
|  | 66 | +   * <p>Any of the characters {@code :} and {@code /} will be replaced with {@code .}.</p> | 
|  | 67 | +   * | 
|  | 68 | +   * @return a system property-backed value source with no prefix | 
|  | 69 | +   * @since 1.1.0 | 
|  | 70 | +   */ | 
|  | 71 | +  static @NotNull ValueSource systemProperty() { | 
|  | 72 | +    return systemProperty(""); | 
|  | 73 | +  } | 
|  | 74 | + | 
|  | 75 | +  /** | 
|  | 76 | +   * A value source that will extract option values from system properties. | 
|  | 77 | +   * | 
|  | 78 | +   * <p>Any of the characters {@code :} and {@code /} will be replaced with {@code .}.</p> | 
|  | 79 | +   * | 
|  | 80 | +   * @param prefix the prefix for property lookup, which will be prepended to properties followed by a {@code .}, | 
|  | 81 | +   *               or the empty string for no prefix | 
|  | 82 | +   * @return a system property-backed value source | 
|  | 83 | +   * @since 1.1.0 | 
|  | 84 | +   */ | 
|  | 85 | +  static @NotNull ValueSource systemProperty(final @NotNull String prefix) { | 
|  | 86 | +    return new ValueSources.SystemProperty(prefix); | 
|  | 87 | +  } | 
|  | 88 | + | 
|  | 89 | +  /** | 
|  | 90 | +   * Provide a value for the specified option, if any is set. | 
|  | 91 | +   * | 
|  | 92 | +   * @param option the option | 
|  | 93 | +   * @return a value, if set | 
|  | 94 | +   * @param <T> the value type | 
|  | 95 | +   * @since 1.1.0 | 
|  | 96 | +   */ | 
|  | 97 | +  <T> @Nullable T value(final @NotNull Option<T> option); | 
|  | 98 | +} | 
0 commit comments