|  | 
|  | 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 java.util.Locale; | 
|  | 27 | +import java.util.regex.Matcher; | 
|  | 28 | +import java.util.regex.Pattern; | 
|  | 29 | +import net.kyori.option.Option; | 
|  | 30 | +import org.jetbrains.annotations.NotNull; | 
|  | 31 | +import org.jetbrains.annotations.Nullable; | 
|  | 32 | + | 
|  | 33 | +final class ValueSources { | 
|  | 34 | +  static final ValueSource ENVIRONMENT = new EnvironmentVariables(""); | 
|  | 35 | +  static final ValueSource SYSTEM_PROPERTIES = new SystemProperties(""); | 
|  | 36 | + | 
|  | 37 | +  private ValueSources() { | 
|  | 38 | +  } | 
|  | 39 | + | 
|  | 40 | +  static final class EnvironmentVariables implements ValueSource { | 
|  | 41 | +    private static final Pattern ENVIRONMENT_SUBST_PATTERN = Pattern.compile("[:\\-/]"); | 
|  | 42 | +    private static final String ENVIRONMENT_VAR_SEPARATOR = "_"; | 
|  | 43 | + | 
|  | 44 | +    private final String prefix; | 
|  | 45 | + | 
|  | 46 | +    EnvironmentVariables(final String prefix) { | 
|  | 47 | +      this.prefix = prefix.isEmpty() ? "" : prefix.toUpperCase(Locale.ROOT) + ENVIRONMENT_VAR_SEPARATOR; | 
|  | 48 | +    } | 
|  | 49 | + | 
|  | 50 | +    @Override | 
|  | 51 | +    public <T> @Nullable T value(final @NotNull Option<T> option) { | 
|  | 52 | +      final StringBuffer buf = new StringBuffer(option.id().length() + this.prefix.length()); | 
|  | 53 | +      buf.append(this.prefix); | 
|  | 54 | +      final Matcher match = ENVIRONMENT_SUBST_PATTERN.matcher(option.id()); | 
|  | 55 | +      while (match.find()) { | 
|  | 56 | +        match.appendReplacement(buf, ENVIRONMENT_VAR_SEPARATOR); | 
|  | 57 | +      } | 
|  | 58 | +      match.appendTail(buf); | 
|  | 59 | + | 
|  | 60 | +      final String value = System.getenv(buf.toString().toUpperCase(Locale.ROOT)); | 
|  | 61 | +      if (value == null) { | 
|  | 62 | +        return null; | 
|  | 63 | +      } | 
|  | 64 | + | 
|  | 65 | +      return option.valueType().parse(value); | 
|  | 66 | +    } | 
|  | 67 | +  } | 
|  | 68 | + | 
|  | 69 | +  static final class SystemProperties implements ValueSource { | 
|  | 70 | +    private static final Pattern SYSTEM_PROP_SUBST_PATTERN = Pattern.compile("[:/]"); | 
|  | 71 | +    private static final String SYSTEM_PROPERTY_SEPARATOR = "."; | 
|  | 72 | + | 
|  | 73 | +    private final String prefix; | 
|  | 74 | + | 
|  | 75 | +    SystemProperties(final String prefix) { | 
|  | 76 | +      this.prefix = prefix.isEmpty() ? "" : prefix + SYSTEM_PROPERTY_SEPARATOR; | 
|  | 77 | +    } | 
|  | 78 | + | 
|  | 79 | +    @Override | 
|  | 80 | +    public <T> @Nullable T value(final @NotNull Option<T> option) { | 
|  | 81 | +      final StringBuffer buf = new StringBuffer(option.id().length() + this.prefix.length()); | 
|  | 82 | +      buf.append(this.prefix); | 
|  | 83 | +      final Matcher match = SYSTEM_PROP_SUBST_PATTERN.matcher(option.id()); | 
|  | 84 | +      while (match.find()) { | 
|  | 85 | +        match.appendReplacement(buf, SYSTEM_PROPERTY_SEPARATOR); | 
|  | 86 | +      } | 
|  | 87 | +      match.appendTail(buf); | 
|  | 88 | + | 
|  | 89 | +      final String value = System.getProperty(buf.toString()); | 
|  | 90 | +      if (value == null) { | 
|  | 91 | +        return null; | 
|  | 92 | +      } | 
|  | 93 | + | 
|  | 94 | +      return option.valueType().parse(value); | 
|  | 95 | +    } | 
|  | 96 | +  } | 
|  | 97 | +} | 
0 commit comments