diff --git a/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java index ee2e19b9a..53aeab1d7 100644 --- a/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java @@ -29,7 +29,7 @@ import java.util.stream.Stream; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.util.Nag; -import net.kyori.adventure.util.internal.AdventureConfig; +import net.kyori.adventure.util.internal.AdventureProperties; import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -38,7 +38,7 @@ import static java.util.Objects.requireNonNull; final class TextComponentImpl extends AbstractComponent implements TextComponent { - private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = AdventureConfig.getBoolean(AdventureConfig.OPTION_TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED, false); + private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = AdventureProperties.booleanValueOf(AdventureProperties.TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED, false); @VisibleForTesting static final char SECTION_CHAR = 'ยง'; diff --git a/api/src/main/java/net/kyori/adventure/translation/TranslationLocales.java b/api/src/main/java/net/kyori/adventure/translation/TranslationLocales.java index d9c37a150..fda60ccb7 100644 --- a/api/src/main/java/net/kyori/adventure/translation/TranslationLocales.java +++ b/api/src/main/java/net/kyori/adventure/translation/TranslationLocales.java @@ -25,13 +25,13 @@ import java.util.Locale; import java.util.function.Supplier; -import net.kyori.adventure.util.internal.AdventureConfig; +import net.kyori.adventure.util.internal.AdventureProperties; final class TranslationLocales { private static final Supplier GLOBAL; static { - final String property = AdventureConfig.getString(AdventureConfig.OPTION_DEFAULT_TRANSLATION_LOCALE); + final String property = AdventureProperties.valueOf(AdventureProperties.DEFAULT_TRANSLATION_LOCALE, null); if (property == null || property.isEmpty()) { GLOBAL = () -> Locale.US; } else if (property.equals("system")) { diff --git a/api/src/main/java/net/kyori/adventure/util/Services.java b/api/src/main/java/net/kyori/adventure/util/Services.java index 2669f8bbd..838fbd31b 100644 --- a/api/src/main/java/net/kyori/adventure/util/Services.java +++ b/api/src/main/java/net/kyori/adventure/util/Services.java @@ -26,7 +26,7 @@ import java.util.Iterator; import java.util.Optional; import java.util.ServiceLoader; -import net.kyori.adventure.util.internal.AdventureConfig; +import net.kyori.adventure.util.internal.AdventureProperties; import org.jetbrains.annotations.NotNull; /** @@ -36,7 +36,7 @@ */ public final class Services { // net.kyori.adventure.serviceLoadFailuresAreFatal - private static final boolean SERVICE_LOAD_FAILURES_ARE_FATAL = AdventureConfig.getBoolean(AdventureConfig.OPTION_SERVICE_LOAD_FAILURES_ARE_FATAL, true); + private static final boolean SERVICE_LOAD_FAILURES_ARE_FATAL = AdventureProperties.booleanValueOf(AdventureProperties.SERVICE_LOAD_FAILURES_ARE_FATAL, true); private Services() { } diff --git a/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfig.java b/api/src/main/java/net/kyori/adventure/util/internal/AdventureProperties.java similarity index 53% rename from api/src/main/java/net/kyori/adventure/util/internal/AdventureConfig.java rename to api/src/main/java/net/kyori/adventure/util/internal/AdventureProperties.java index 4f1ed4f70..20d1251f2 100644 --- a/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfig.java +++ b/api/src/main/java/net/kyori/adventure/util/internal/AdventureProperties.java @@ -23,89 +23,96 @@ */ package net.kyori.adventure.util.internal; +import java.util.function.Function; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.UnknownNullability; /** - * Adventure configuration. + * Adventure properties. * * @since 4.10.0 */ @ApiStatus.Internal -public final class AdventureConfig { - /** - * The directory in which our configuration file lives. - * - * @since 4.10.0 - */ - public static final String FILESYSTEM_DIRECTORY_NAME = "config"; - /** - * The configuration file name. - * - * @since 4.10.0 - */ - public static final String FILESYSTEM_FILE_NAME = "adventure.properties"; - +public final class AdventureProperties { /** * Option for specifying the default translation locale. * * @since 4.10.0 */ - public static final String OPTION_DEFAULT_TRANSLATION_LOCALE = "defaultTranslationLocale"; + public static final Option DEFAULT_TRANSLATION_LOCALE = option("defaultTranslationLocale", Function.identity()); /** * Option for specifying whether service load failures are fatal. * * @since 4.10.0 */ - public static final String OPTION_SERVICE_LOAD_FAILURES_ARE_FATAL = "serviceLoadFailuresAreFatal"; + public static final Option SERVICE_LOAD_FAILURES_ARE_FATAL = option("serviceLoadFailuresAreFatal", Boolean::parseBoolean); /** * Option for specifying whether to warn when legacy formatting is detected. * * @since 4.10.0 */ - public static final String OPTION_TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED = "text.warnWhenLegacyFormattingDetected"; + public static final Option TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED = option("text.warnWhenLegacyFormattingDetected", Boolean::parseBoolean); - private AdventureConfig() { + private AdventureProperties() { + } + + /** + * Creates a new option. + * + * @param name the option name + * @param parser the value parser + * @param the value type + * @return the option + * @since 4.10.0 + */ + public static @NotNull Option option(final @NotNull String name, final @NotNull Function parser) { + return new AdventurePropertiesImpl.OptionImpl<>(name, parser); } /** * Gets a boolean value. * - * @param key the key + * @param option the option * @param defaultValue the default value * @return the boolean value * @since 4.10.0 */ - @SuppressWarnings("checkstyle:MethodName") - public static boolean getBoolean(final @NotNull String key, final boolean defaultValue) { - return Boolean.parseBoolean(getString(key, Boolean.toString(defaultValue))); + public static boolean booleanValueOf(final @NotNull Option option, final boolean defaultValue) { + return Boolean.TRUE.equals(valueOf(option, defaultValue)); } /** - * Gets a string value, or {@code null}. + * Gets a boolean value. * - * @param key the key - * @return the string value, or {@code null} + * @param option the option + * @param defaultValue the default value + * @return the boolean value * @since 4.10.0 */ - @SuppressWarnings("checkstyle:MethodName") - public static @Nullable String getString(final @NotNull String key) { - return getString(key, null); + public static @Nullable T valueOf(final @NotNull Option option, final @Nullable T defaultValue) { + final String key = option.name(); + final String property = String.join(".", "net", "kyori", "adventure", key); + final String value = System.getProperty(property, AdventurePropertiesImpl.PROPERTIES.getProperty(key)); + return value != null ? ((AdventurePropertiesImpl.OptionImpl) option).parser.apply(value) : defaultValue; } /** - * Gets a string value. + * An option. * - * @param key the key - * @param defaultValue the default value - * @return the string value + * @param the value type * @since 4.10.0 */ - @SuppressWarnings("checkstyle:MethodName") - public static @Nullable String getString(final @NotNull String key, final @Nullable String defaultValue) { - final String property = String.join(".", "net", "kyori", "adventure", key); - return AdventureConfigImpl.PROPERTIES.getProperty(key, System.getProperty(property, defaultValue)); + @ApiStatus.Internal + @ApiStatus.NonExtendable + @SuppressWarnings("unused") + public interface Option { + /** + * Gets the name. + * + * @return the name + * @since 4.10.0 + */ + @NotNull String name(); } } diff --git a/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfigImpl.java b/api/src/main/java/net/kyori/adventure/util/internal/AdventurePropertiesImpl.java similarity index 66% rename from api/src/main/java/net/kyori/adventure/util/internal/AdventureConfigImpl.java rename to api/src/main/java/net/kyori/adventure/util/internal/AdventurePropertiesImpl.java index 3f9c6109a..0a9cc3424 100644 --- a/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfigImpl.java +++ b/api/src/main/java/net/kyori/adventure/util/internal/AdventurePropertiesImpl.java @@ -29,16 +29,20 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; +import java.util.function.Function; +import org.jetbrains.annotations.NotNull; -final class AdventureConfigImpl { +final class AdventurePropertiesImpl { + private static final String FILESYSTEM_DIRECTORY_NAME = "config"; + private static final String FILESYSTEM_FILE_NAME = "adventure.properties"; static final Properties PROPERTIES = loadProperties(); - private AdventureConfigImpl() { + private AdventurePropertiesImpl() { } private static Properties loadProperties() { final Properties properties = new Properties(); - final Path path = Paths.get(AdventureConfig.FILESYSTEM_DIRECTORY_NAME, AdventureConfig.FILESYSTEM_FILE_NAME); + final Path path = Paths.get(FILESYSTEM_DIRECTORY_NAME, FILESYSTEM_FILE_NAME); if (Files.isRegularFile(path)) { try (final InputStream is = Files.newInputStream(path)) { properties.load(is); @@ -49,4 +53,29 @@ private static Properties loadProperties() { } return properties; } + + static final class OptionImpl implements AdventureProperties.Option { + private final String name; + final Function parser; + + OptionImpl(final @NotNull String name, final @NotNull Function parser) { + this.name = name; + this.parser = parser; + } + + @Override + public @NotNull String name() { + return this.name; + } + + @Override + public boolean equals(final Object that) { + return this == that; + } + + @Override + public int hashCode() { + return this.name.hashCode(); + } + } }