mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-12-28 17:38:01 +01:00
code style
This commit is contained in:
parent
0c76833997
commit
e09444d94f
@ -64,11 +64,6 @@ public final class CaptionLoader {
|
||||
|
||||
private static final Gson GSON;
|
||||
|
||||
private final Map<String, String> defaultMessages;
|
||||
private final Locale defaultLocale;
|
||||
private final Function<Path, Locale> 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<String, String> defaultMessages;
|
||||
private final Locale defaultLocale;
|
||||
private final Function<Path, Locale> 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:
|
||||
* <pre>{@code
|
||||
* {
|
||||
* "key1": "value a",
|
||||
* "key2": "value b",
|
||||
* ...
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* @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<Map<String, String>>() {}.getType();
|
||||
return new LinkedHashMap<>(GSON.fromJson(reader, type));
|
||||
}
|
||||
|
||||
private static void save(final Path file, final Map<String, String> 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:
|
||||
* <pre>{@code
|
||||
* {
|
||||
* "key1": "value a",
|
||||
* "key2": "value b",
|
||||
* ...
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* @param reader the reader to read the map from.
|
||||
* @return the translation map.
|
||||
*/
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
static Map<String, String> loadFromReader(final Reader reader) {
|
||||
final Type type = new TypeToken<Map<String, String>>() {}.getType();
|
||||
return new LinkedHashMap<>(GSON.fromJson(reader, type));
|
||||
}
|
||||
|
||||
private static void save(final Path file, final Map<String, String> 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<Path, 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)");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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<Locale, String> urlProvider;
|
||||
private final Function<@NonNull Locale, @NonNull String> urlProvider;
|
||||
|
||||
ClassLoaderCaptionProvider(ClassLoader classLoader, Function<Locale, String> urlProvider) {
|
||||
ClassLoaderCaptionProvider(
|
||||
final @NonNull ClassLoader classLoader,
|
||||
final @NonNull Function<@NonNull Locale, @NonNull String> urlProvider) {
|
||||
this.classLoader = classLoader;
|
||||
this.urlProvider = urlProvider;
|
||||
}
|
||||
|
@ -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<Locale, String> 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<String, String> loadDefaults(final @NonNull Locale locale);
|
||||
@Nullable Map<@NonNull String, @NonNull String> loadDefaults(final @NonNull Locale locale);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user