From e09444d94f0a0a64cf12e844793bdc13d03dbcc3 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sat, 2 Jan 2021 20:48:14 +0100 Subject: [PATCH] code style --- .../caption/load/CaptionLoader.java | 150 +++++++++--------- .../load/ClassLoaderCaptionProvider.java | 8 +- .../caption/load/DefaultCaptionProvider.java | 10 +- 3 files changed, 88 insertions(+), 80 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/load/CaptionLoader.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/load/CaptionLoader.java index 95cbd75c0..c5d5e3139 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/load/CaptionLoader.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/load/CaptionLoader.java @@ -64,11 +64,6 @@ public final class CaptionLoader { private static final Gson GSON; - private final Map defaultMessages; - private final Locale defaultLocale; - private final Function localeExtractor; - private final DefaultCaptionProvider captionProvider; - static { GSON = new GsonBuilder() .setPrettyPrinting() @@ -76,24 +71,15 @@ public final class CaptionLoader { .create(); } - /** - * Returns a new CaptionLoader instance. That instance will use the internalLocale to extract default values - * from the captionProvider - * - * @param internalLocale the locale used internally to resolve default messages from the caption provider. - * @param localeExtractor a function to extract a locale from a path, e.g. by its name. - * @param captionProvider the provider for default captions. - * @return a CaptionLoader instance that can load and patch message files. - */ - public static CaptionLoader of(final @NonNull Locale internalLocale, - final @NonNull Function<@NonNull Path, @NonNull Locale> localeExtractor, - final @NonNull DefaultCaptionProvider captionProvider) { - return new CaptionLoader(internalLocale, localeExtractor, captionProvider); - } + private final Map defaultMessages; + private final Locale defaultLocale; + private final Function localeExtractor; + private final DefaultCaptionProvider captionProvider; - private CaptionLoader(final @NonNull Locale internalLocale, - final @NonNull Function<@NonNull Path, @NonNull Locale> localeExtractor, - final @NonNull DefaultCaptionProvider captionProvider) { + private CaptionLoader( + final @NonNull Locale internalLocale, + final @NonNull Function<@NonNull Path, @NonNull Locale> localeExtractor, + final @NonNull DefaultCaptionProvider captionProvider) { this.defaultLocale = internalLocale; this.localeExtractor = localeExtractor; this.captionProvider = captionProvider; @@ -107,6 +93,74 @@ public final class CaptionLoader { this.defaultMessages = temp; } + /** + * Returns a new CaptionLoader instance. That instance will use the internalLocale to extract default values + * from the captionProvider + * + * @param internalLocale the locale used internally to resolve default messages from the caption provider. + * @param localeExtractor a function to extract a locale from a path, e.g. by its name. + * @param captionProvider the provider for default captions. + * @return a CaptionLoader instance that can load and patch message files. + */ + public static @NonNull CaptionLoader of( + final @NonNull Locale internalLocale, + final @NonNull Function<@NonNull Path, @NonNull Locale> localeExtractor, + final @NonNull DefaultCaptionProvider captionProvider) { + return new CaptionLoader(internalLocale, localeExtractor, captionProvider); + } + + /** + * Returns a function that extracts a locale from a path using the given pattern. + * The pattern is required to have (at least) one capturing group, as this is used to access the locale + * tag.The function will throw an {@link IllegalArgumentException} if the matcher doesn't match the file name + * of the input path. The language tag is loaded using {@link Locale#forLanguageTag(String)}. + * + * @param pattern the pattern to match and extract the language tag with. + * @return a function to extract a locale from a path using a pattern. + * @see Matcher#group(int) + * @see Path#getFileName() + */ + public static @NonNull Function<@NonNull Path, @NonNull Locale> patternExtractor(final @NonNull Pattern pattern) { + return path -> { + final String fileName = path.getFileName().toString(); + final Matcher matcher = pattern.matcher(fileName); + if (matcher.matches()) { + return Locale.forLanguageTag(matcher.group(1)); + } else { + throw new IllegalArgumentException(fileName + " is an invalid message file (cannot extract locale)"); + } + }; + } + + /** + * Loads a map of translation keys mapping to their translations from a reader. + * The format is expected to be a json object: + *
{@code
+     * {
+     *     "key1": "value a",
+     *     "key2": "value b",
+     *     ...
+     * }
+     * }
+ * + * @param reader the reader to read the map from. + * @return the translation map. + */ + @SuppressWarnings("UnstableApiUsage") + static @NonNull Map<@NonNull String, @NonNull String> loadFromReader(final @NonNull Reader reader) { + final Type type = new TypeToken>() {}.getType(); + return new LinkedHashMap<>(GSON.fromJson(reader, type)); + } + + private static void save(final Path file, final Map content) { + try (final BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { + GSON.toJson(content, writer); + logger.info("Saved {} with new content", file.getFileName()); + } catch (final IOException e) { + logger.error("Failed to save caption file '{}'", file.getFileName().toString(), e); + } + } + /** * Load all message files in the given directory into a new CaptionMap. * @@ -155,35 +209,6 @@ public final class CaptionLoader { } } - /** - * Loads a map of translation keys mapping to their translations from a reader. - * The format is expected to be a json object: - *
{@code
-     * {
-     *     "key1": "value a",
-     *     "key2": "value b",
-     *     ...
-     * }
-     * }
- * - * @param reader the reader to read the map from. - * @return the translation map. - */ - @SuppressWarnings("UnstableApiUsage") - static Map loadFromReader(final Reader reader) { - final Type type = new TypeToken>() {}.getType(); - return new LinkedHashMap<>(GSON.fromJson(reader, type)); - } - - private static void save(final Path file, final Map content) { - try (final BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { - GSON.toJson(content, writer); - logger.info("Saved {} with new content", file.getFileName()); - } catch (final IOException e) { - logger.error("Failed to save caption file '{}'", file.getFileName().toString(), e); - } - } - /** * Add missing entries to the given map. * Entries are missing if the key exists in {@link #defaultLocale} but isn't present @@ -215,27 +240,4 @@ public final class CaptionLoader { } return modified; } - - /** - * Returns a function that extracts a locale from a path using the given pattern. - * The pattern is required to have (at least) one capturing group, as this is used to access the locale - * tag.The function will throw an {@link IllegalArgumentException} if the matcher doesn't match the file name - * of the input path. The language tag is loaded using {@link Locale#forLanguageTag(String)}. - * - * @param pattern the pattern to match and extract the language tag with. - * @return a function to extract a locale from a path using a pattern. - * @see Matcher#group(int) - * @see Path#getFileName() - */ - public static @NonNull Function patternExtractor(final @NonNull Pattern pattern) { - return path -> { - final String fileName = path.getFileName().toString(); - final Matcher matcher = pattern.matcher(fileName); - if (matcher.matches()) { - return Locale.forLanguageTag(matcher.group(1)); - } else { - throw new IllegalArgumentException(fileName + " is an invalid message file (cannot extract locale)"); - } - }; - } } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/load/ClassLoaderCaptionProvider.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/load/ClassLoaderCaptionProvider.java index 9189f6a0c..cd948ece2 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/load/ClassLoaderCaptionProvider.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/load/ClassLoaderCaptionProvider.java @@ -40,12 +40,14 @@ import java.util.function.Function; import static com.plotsquared.core.configuration.caption.load.CaptionLoader.loadFromReader; -class ClassLoaderCaptionProvider implements DefaultCaptionProvider { +final class ClassLoaderCaptionProvider implements DefaultCaptionProvider { private static final Logger logger = LoggerFactory.getLogger("P2/" + ClassLoaderCaptionProvider.class.getSimpleName()); private final ClassLoader classLoader; - private final Function urlProvider; + private final Function<@NonNull Locale, @NonNull String> urlProvider; - ClassLoaderCaptionProvider(ClassLoader classLoader, Function urlProvider) { + ClassLoaderCaptionProvider( + final @NonNull ClassLoader classLoader, + final @NonNull Function<@NonNull Locale, @NonNull String> urlProvider) { this.classLoader = classLoader; this.urlProvider = urlProvider; } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/load/DefaultCaptionProvider.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/load/DefaultCaptionProvider.java index 7944f3a12..5827c8d89 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/load/DefaultCaptionProvider.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/load/DefaultCaptionProvider.java @@ -42,7 +42,9 @@ public interface DefaultCaptionProvider { * @param urlProvider the function to get an url from a locale. * @return a caption provider using a function to determine resource urls. */ - static DefaultCaptionProvider forClassLoader(ClassLoader classLoader, Function urlProvider) { + static @NonNull DefaultCaptionProvider forClassLoader( + final @NonNull ClassLoader classLoader, + final @NonNull Function<@NonNull Locale, @NonNull String> urlProvider) { return new ClassLoaderCaptionProvider(classLoader, urlProvider); } @@ -56,7 +58,9 @@ public interface DefaultCaptionProvider { * {@code String.format(toFormat, Locale#toString)} * @return a caption provider using string formatting to determine resource urls. */ - static DefaultCaptionProvider forClassLoaderFormatString(ClassLoader classLoader, String toFormat) { + static @NonNull DefaultCaptionProvider forClassLoaderFormatString( + final @NonNull ClassLoader classLoader, + final @NonNull String toFormat) { return forClassLoader(classLoader, locale -> String.format(toFormat, locale.toString())); } @@ -67,5 +71,5 @@ public interface DefaultCaptionProvider { * @param locale the locale to load the values for. * @return a map of default values for the given locale. */ - @Nullable Map loadDefaults(final @NonNull Locale locale); + @Nullable Map<@NonNull String, @NonNull String> loadDefaults(final @NonNull Locale locale); }