Various code simplification/fixes and a lot of typo/grammar fixes (may brake some stuff)

This commit is contained in:
2023-06-20 00:15:46 +02:00
parent c984b63cee
commit 5edd8cdfec
151 changed files with 909 additions and 983 deletions

View File

@@ -18,7 +18,7 @@ import static fr.pandacube.lib.chat.ChatStatic.text;
/**
* Cleanup a backup directory (i.e. removes old backup archives).
* It is possible to combine differents instances to affect which archive to keep or delete.
* It is possible to combine different instances to affect which archive to keep or delete.
*/
public abstract class BackupCleaner implements UnaryOperator<TreeSet<LocalDateTime>> {
@@ -48,7 +48,7 @@ public abstract class BackupCleaner implements UnaryOperator<TreeSet<LocalDateTi
* formula <code><i>YEAR</i> * (12 / <i>n</i>) + <i>MONTH</i> / <i>n</i></code>. It then keeps the first archive
* found in each section.
*
* @param n the interval in month between each kept archives. Must be a dividor of 12 (1, 2, 3, 4, 6 or 12).
* @param n the interval in month between each kept archives. Must be a divider of 12 (1, 2, 3, 4, 6 or 12).
* @return a {@link BackupCleaner} that keeps one archive every n month.
*/
public static BackupCleaner KEEPING_1_EVERY_N_MONTH(int n) {
@@ -94,11 +94,13 @@ public abstract class BackupCleaner implements UnaryOperator<TreeSet<LocalDateTi
/**
* Performs the cleanup operation on the provided directory.
* @param archiveDir the backup directory to cleanup.
* @param compressDisplayName the displayname of the backup process that manages the backup directory. Used for logs.
* @param archiveDir the backup directory to clean up.
* @param compressDisplayName the display name of the backup process that manages the backup directory. Used for logs.
*/
public void cleanupArchives(File archiveDir, String compressDisplayName) {
String[] files = archiveDir.list();
if (files == null)
return;
Log.info("[Backup] Cleaning up backup directory " + ChatColor.GRAY + compressDisplayName + ChatColor.RESET + "...");

View File

@@ -33,7 +33,7 @@ public class BackupManager extends TimerTask {
private final Timer schedulerTimer = new Timer();
/**
* Instanciate a new backup manager.
* Instantiate a new backup manager.
* @param backupDirectory the root backup directory.
*/
public BackupManager(File backupDirectory) {

View File

@@ -35,12 +35,12 @@ public abstract class BackupProcess implements Comparable<BackupProcess>, Runnab
private boolean enabled = true;
private String scheduling = "0 2 * * *"; // cron format, here is everyday at 2am
private String scheduling = "0 2 * * *"; // cron format, here is every day at 2am
private BackupCleaner backupCleaner = null;
private List<String> ignoreList = new ArrayList<>();
/**
* Instanciates a new backup process.
* Instantiates a new backup process.
* @param bm the associated backup manager.
* @param n the process identifier.
*/
@@ -66,9 +66,9 @@ public abstract class BackupProcess implements Comparable<BackupProcess>, Runnab
}
/**
* Gets the displayname of this process.
* Gets the display name of this process.
* Default implementation returns {@link #getIdentifier()}.
* @return the displayname of this process.
* @return the display name of this process.
*/
protected String getDisplayName() {
return getIdentifier();
@@ -105,8 +105,8 @@ public abstract class BackupProcess implements Comparable<BackupProcess>, Runnab
}
/**
* Gets the source directory to backup.
* @return the source directory to backup.
* Gets the source directory to back up.
* @return the source directory to back up.
*/
public abstract File getSourceDir();
@@ -123,7 +123,7 @@ public abstract class BackupProcess implements Comparable<BackupProcess>, Runnab
/**
* Called when the backup ends.
* @param success true if the backup ended successfuly.
* @param success true if the backup ended successfully.
*/
protected abstract void onBackupEnd(boolean success);
@@ -209,7 +209,7 @@ public abstract class BackupProcess implements Comparable<BackupProcess>, Runnab
File sourceDir = getSourceDir();
if (!sourceDir.exists()) {
Log.warning("[Backup] Unable to compress " + ChatColor.GRAY + getDisplayName() + ChatColor.RESET + ": source directory " + sourceDir + " doesnt exist");
Log.warning("[Backup] Unable to compress " + ChatColor.GRAY + getDisplayName() + ChatColor.RESET + ": source directory " + sourceDir + " doesn't exist");
return;
}

View File

@@ -26,7 +26,7 @@ public class Persist {
// private final Set<String> dirtyWorldsSave = new HashSet<>();
/**
* Creates a new instance, immediatly loading the data from the file if it exists, or creating an empty one if not.
* Creates a new instance, immediately loading the data from the file if it exists, or creating an empty one if not.
* @param bm the associated backup manager.
*/
public Persist(BackupManager bm) {

View File

@@ -24,7 +24,8 @@ public class RotatedLogsBackupProcess extends BackupProcess {
* @param inNewThread tells if this process should be run in a separate thread (true) or in the same thread handling
* the backup manager (false).
* @param sourceLogDir the directory where the rotated log files are stored, usually {@code ./logs/}.
* @param logFileRegexPattern the pattern to match the rotated log files (usually dated log files, excuding the current log file).
* @param logFileRegexPattern the pattern to match the rotated log files (usually dated log files, excluding the
* current log file).
*/
public RotatedLogsBackupProcess(BackupManager bm, boolean inNewThread, File sourceLogDir, String logFileRegexPattern) {
super(bm, "logs");

View File

@@ -158,8 +158,8 @@ public class ZipCompressor {
}
private class Entry {
File file;
String entry;
final File file;
final String entry;
Entry(File f, String e) {
file = f;
entry = e;

View File

@@ -1,23 +1,22 @@
package fr.pandacube.lib.core.config;
import fr.pandacube.lib.chat.ChatColorUtil;
import fr.pandacube.lib.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import fr.pandacube.lib.chat.ChatColorUtil;
import fr.pandacube.lib.util.Log;
/**
* Class that loads a specific config file or directory.
*/
public abstract class AbstractConfig {
/**
* The {@link File} corresponging to this config file or directory.
* The {@link File} corresponding to this config file or directory.
*/
protected final File configFile;
@@ -94,7 +93,8 @@ public abstract class AbstractConfig {
* @return the list of files in the config directory, or null if this config is not a directory.
*/
protected List<File> getFileList() {
return configFile.isDirectory() ? Arrays.asList(configFile.listFiles()) : null;
File[] arr = configFile.listFiles();
return arr != null ? List.of(arr) : null;
}
@@ -105,7 +105,7 @@ public abstract class AbstractConfig {
* Splits the provided string into a list of permission nodes.
* The permission nodes must be separated by {@code ";"}.
* @param perms one or more permissions nodes, separated by {@code ";"}.
* @return {@code null} if the parameter is null or is equal to {@code "*"}, or the string splitted using {@code ";"}.
* @return {@code null} if the parameter is null or is equal to {@code "*"}, or the string split using {@code ";"}.
*/
public static List<String> splitPermissionsString(String perms) {
if (perms == null || perms.equals("*"))
@@ -115,9 +115,9 @@ public abstract class AbstractConfig {
/**
* Utility method to that translate the {@code '&'} formated string to the legacy format.
* Utility method to that translate the {@code '&'} formatted string to the legacy format.
* @param string the string to convert.
* @return a legacy formated string (using {@code '§'}).
* @return a legacy formatted string (using {@code '§'}).
*/
public static String getTranslatedColorCode(String string) {
return ChatColorUtil.translateAlternateColorCodes('&', string);

View File

@@ -5,13 +5,13 @@ import java.io.IOException;
/**
* An abstract manager for a set of configuration files and folders.
* Its uses is to manage the loading/reloading of the configuration of a plugin.
* It's uses to manage the loading/reloading of the configuration of a plugin.
*/
public abstract class AbstractConfigManager {
/**
* The global configuration directory.
* May be the one provided by the environmenet API (like Plugin.getPluginFolder() in Bukkit).
* It may be the one provided by the environment API (like Plugin.getPluginFolder() in Bukkit).
*/
protected final File configDir;

View File

@@ -102,7 +102,7 @@ public class CronScheduler {
/**
* Cancel a scheduled task.
* Will not stop a current execution of the task. If the task does not exists, it will not do anything.
* Will not stop a current execution of the task. If the task does not exist, it will not do anything.
* @param taskId the id of the task to cancel.
*/
public static void unSchedule(String taskId) {

View File

@@ -12,34 +12,34 @@ import java.util.List;
import java.util.function.Function;
/**
* Provides pre-instanciated {@link Gson} instances, all with support for Java records and additionnal
* Provides pre-instanced {@link Gson} objects, all with support for Java records and additional
* {@link TypeAdapterFactory} provided with {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
*/
public class Json {
/**
* {@link Gson} instance with {@link GsonBuilder#setLenient()} and support for Java records and additionnal
* {@link Gson} instance with {@link GsonBuilder#setLenient()} and support for Java records and additional
* {@link TypeAdapterFactory} provided with {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
*/
public static final Gson gson = build(Function.identity());
/**
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#setPrettyPrinting()} and support
* for Java records and additionnal {@link TypeAdapterFactory} provided with
* for Java records and additional {@link TypeAdapterFactory} provided with
* {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
*/
public static final Gson gsonPrettyPrinting = build(GsonBuilder::setPrettyPrinting);
/**
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#serializeNulls()} and support for
* Java records and additionnal {@link TypeAdapterFactory} provided with
* Java records and additional {@link TypeAdapterFactory} provided with
* {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
*/
public static final Gson gsonSerializeNulls = build(GsonBuilder::serializeNulls);
/**
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#serializeNulls()},
* {@link GsonBuilder#setPrettyPrinting()} and support for Java records and additionnal {@link TypeAdapterFactory}
* {@link GsonBuilder#setPrettyPrinting()} and support for Java records and additional {@link TypeAdapterFactory}
* provided with {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
*/
public static final Gson gsonSerializeNullsPrettyPrinting = build(b -> b.serializeNulls().setPrettyPrinting());

View File

@@ -43,7 +43,7 @@ public class ThrowableAdapter implements JsonSerializer<Throwable>, JsonDeserial
// handle types
Throwable t = null;
if (obj.has("types") && obj.get("types").isJsonArray()) {
t = instanciate(obj.getAsJsonArray("types"), message, cause);
t = instantiate(obj.getAsJsonArray("types"), message, cause);
}
if (t == null) {
t = new Throwable(message, cause);
@@ -53,8 +53,8 @@ public class ThrowableAdapter implements JsonSerializer<Throwable>, JsonDeserial
JsonArray suppressed = obj.has("suppressed") && !obj.get("suppressed").isJsonNull()
? obj.get("suppressed").getAsJsonArray() : null;
if (suppressed != null) {
for (JsonElement jsonel : suppressed) {
t.addSuppressed(context.deserialize(jsonel, Throwable.class));
for (JsonElement jsonEl : suppressed) {
t.addSuppressed(context.deserialize(jsonEl, Throwable.class));
}
}
@@ -63,8 +63,8 @@ public class ThrowableAdapter implements JsonSerializer<Throwable>, JsonDeserial
? obj.get("stacktrace").getAsJsonArray() : null;
if (stacktrace != null) {
List<StackTraceElement> els = new ArrayList<>();
for (JsonElement jsonel : stacktrace) {
els.add(context.deserialize(jsonel, StackTraceElement.class));
for (JsonElement jsonEl : stacktrace) {
els.add(context.deserialize(jsonEl, StackTraceElement.class));
}
t.setStackTrace(els.toArray(new StackTraceElement[0]));
}
@@ -159,7 +159,7 @@ public class ThrowableAdapter implements JsonSerializer<Throwable>, JsonDeserial
}
private Throwable instanciate(JsonArray types, String message, Throwable cause) {
private Throwable instantiate(JsonArray types, String message, Throwable cause) {
Throwable t = null;
for (JsonElement clNameEl : types) {
String clName = clNameEl.getAsString();
@@ -196,7 +196,7 @@ public class ThrowableAdapter implements JsonSerializer<Throwable>, JsonDeserial
}
/**
* Utiliy method to use on {@link Throwable} class that only have a message (no cause) constructor.
* Utility method to use on {@link Throwable} class that only have a message (no cause) constructor.
* @param constructorWithMessage function that will construct a new throwable, with prefilled message.
* @return a function that will construct a throwable using the provided function, then will try to init the cause of the throwable.
* @param <T> the type of the constructed {@link Throwable}.

View File

@@ -15,8 +15,8 @@ public class TypeConverter {
/**
* Converts the provided object to an {@link Integer}.
* @param o the object to convert.
* @return a the object converted to an {@link Integer}.
* @throws ConvertionException is a conversion error occurs.
* @return the object converted to an {@link Integer}.
* @throws ConversionException is a conversion error occurs.
*/
public static Integer toInteger(Object o) {
if (o == null) {
@@ -27,7 +27,7 @@ public class TypeConverter {
try {
return ((JsonElement)o).getAsInt();
} catch(UnsupportedOperationException e) {
throw new ConvertionException(e);
throw new ConversionException(e);
}
}
@@ -38,34 +38,34 @@ public class TypeConverter {
try {
return Integer.parseInt((String)o);
} catch (NumberFormatException e) {
throw new ConvertionException(e);
throw new ConversionException(e);
}
}
if (o instanceof Boolean) {
return ((Boolean)o) ? 1 : 0;
}
throw new ConvertionException("No integer convertion available for an instance of "+o.getClass());
throw new ConversionException("No integer conversion available for an instance of "+o.getClass());
}
/**
* Converts the provided object to a primitive int.
* @param o the object to convert.
* @return a the object converted to a primitive int.
* @throws ConvertionException is a conversion error occurs.
* @return the object converted to a primitive int.
* @throws ConversionException is a conversion error occurs.
*/
public static int toPrimInt(Object o) {
Integer val = toInteger(o);
if (val == null)
throw new ConvertionException("null values can't be converted to primitive int");
throw new ConversionException("null values can't be converted to primitive int");
return val;
}
/**
* Converts the provided object to a {@link Double}.
* @param o the object to convert.
* @return a the object converted to a {@link Double}.
* @throws ConvertionException is a conversion error occurs.
* @return the object converted to a {@link Double}.
* @throws ConversionException is a conversion error occurs.
*/
public static Double toDouble(Object o) {
if (o == null) {
@@ -76,7 +76,7 @@ public class TypeConverter {
try {
return ((JsonElement)o).getAsDouble();
} catch(UnsupportedOperationException e) {
throw new ConvertionException(e);
throw new ConversionException(e);
}
}
@@ -87,35 +87,35 @@ public class TypeConverter {
try {
return Double.parseDouble((String)o);
} catch (NumberFormatException e) {
throw new ConvertionException(e);
throw new ConversionException(e);
}
}
if (o instanceof Boolean) {
return ((Boolean)o) ? 1d : 0d;
}
throw new ConvertionException("No double convertion available for an instance of "+o.getClass());
throw new ConversionException("No double conversion available for an instance of "+o.getClass());
}
/**
* Converts the provided object to a primitive double.
* @param o the object to convert.
* @return a the object converted to a primitive double.
* @throws ConvertionException is a conversion error occurs.
* @return the object converted to a primitive double.
* @throws ConversionException is a conversion error occurs.
*/
public static double toPrimDouble(Object o) {
Double val = toDouble(o);
if (val == null)
throw new ConvertionException("null values can't converted to primitive int");
throw new ConversionException("null values can't converted to primitive int");
return val;
}
/**
* Converts the provided object to a {@link String}.
* @param o the object to convert.
* @return a the object converted to a {@link String}.
* @throws ConvertionException is a conversion error occurs.
* @return the object converted to a {@link String}.
* @throws ConversionException is a conversion error occurs.
*/
public static String toString(Object o) {
if (o == null) {
@@ -126,7 +126,7 @@ public class TypeConverter {
try {
return ((JsonElement)o).getAsString();
} catch(UnsupportedOperationException e) {
throw new ConvertionException(e);
throw new ConversionException(e);
}
}
@@ -134,7 +134,7 @@ public class TypeConverter {
return o.toString();
}
throw new ConvertionException("No string convertion available for an instance of "+o.getClass());
throw new ConversionException("No string conversion available for an instance of "+o.getClass());
}
@@ -144,8 +144,8 @@ public class TypeConverter {
* @param mapIntKeys if the String key representing an int should be duplicated as integer type,
* which map to the same value as the original String key. For example, if a key is "12" and map
* to the object <i>o</i>, an integer key 12 will be added and map to the same object <i>o</i>.
* @return a the object converted to a {@link Map}.
* @throws ConvertionException is a conversion error occurs.
* @return the object converted to a {@link Map}.
* @throws ConversionException is a conversion error occurs.
*/
@SuppressWarnings("unchecked")
public static Map<Object, Object> toMap(Object o, boolean mapIntKeys) {
@@ -186,15 +186,15 @@ public class TypeConverter {
return map;
}
throw new ConvertionException("No Map convertion available for an instance of "+o.getClass());
throw new ConversionException("No Map conversion available for an instance of "+o.getClass());
}
/**
* Converts the provided object to a {@link List}.
* @param o the object to convert.
* @return a the object converted to a {@link List}.
* @throws ConvertionException is a conversion error occurs.
* @return the object converted to a {@link List}.
* @throws ConversionException is a conversion error occurs.
*/
@SuppressWarnings("unchecked")
public static List<Object> toList(Object o) {
@@ -217,7 +217,7 @@ public class TypeConverter {
}
throw new ConvertionException("No Map convertion available for an instance of "+o.getClass());
throw new ConversionException("No Map conversion available for an instance of "+o.getClass());
@@ -225,14 +225,14 @@ public class TypeConverter {
/**
* Thrown when a convertion error occurs.
* Thrown when a conversion error occurs.
*/
public static class ConvertionException extends RuntimeException {
public static class ConversionException extends RuntimeException {
private ConvertionException(String m) {
private ConversionException(String m) {
super(m);
}
private ConvertionException(Throwable t) {
private ConversionException(Throwable t) {
super(t);
}

View File

@@ -1,8 +1,6 @@
package fr.pandacube.lib.core.mc_version;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

View File

@@ -16,7 +16,7 @@ public class MinecraftVersionUtil {
/**
* Compare two Minecraft version strings. It uses the rules of semantic
* versionning to compare the versions.
* versioning to compare the versions.
* @param v1 the first version to compare.
* @param v2 the second version to compare.
* @return 0 if they are equal, &lt;0 if v1&lt;v2 and vice-versa.
@@ -49,20 +49,20 @@ public class MinecraftVersionUtil {
}
/**
* Tells if the two provided Minecraft versions are consecutives.
* Tells if the two provided Minecraft versions are consecutive.
* <p>
* Two versions are consecutives if (considering {@code 1.X[.Y]}):
* Two versions are consecutive if (considering {@code 1.X[.Y]}):
* <ul>
* <li>They are part of the same main version (X value)</li>
* <li>v1 has no Y value, and v2 has Y = 1 (eg. 1.19 and 1.19.1) OR
* both v1 and v2 has a Y value and those values are consecutives.
* both v1 and v2 has a Y value and those values are consecutive.
* </li>
* </ul>
* @param v1 the first version.
* @param v2 the second version.
* @return thue if the second version is consecutive to the first one.
*/
public static boolean areConsecutives(String v1, String v2) {
public static boolean areConsecutive(String v1, String v2) {
int[] v1Int = decomposedVersion(v1);
int[] v2Int = decomposedVersion(v2);
@@ -105,21 +105,21 @@ public class MinecraftVersionUtil {
versions = new ArrayList<>(toOrderedSet(versions));
List<String> keptVersions = new ArrayList<>(versions.size());
for (int i = 0, firstConsec = 0; i < versions.size(); i++) {
if (i == versions.size() - 1 || !areConsecutives(versions.get(i), versions.get(i + 1))) {
if (firstConsec == i) {
for (int i = 0, firstConsecutive = 0; i < versions.size(); i++) {
if (i == versions.size() - 1 || !areConsecutive(versions.get(i), versions.get(i + 1))) {
if (firstConsecutive == i) {
keptVersions.add(versions.get(i));
firstConsec++;
firstConsecutive++;
}
else {
// merge
if (i - firstConsec > 1)
keptVersions.add(versions.get(firstConsec) + "-" + versions.get(i));
if (i - firstConsecutive > 1)
keptVersions.add(versions.get(firstConsecutive) + "-" + versions.get(i));
else {
keptVersions.add(versions.get(firstConsec));
keptVersions.add(versions.get(firstConsecutive));
keptVersions.add(versions.get(i));
}
firstConsec = i + 1;
firstConsecutive = i + 1;
}
}
}

View File

@@ -23,8 +23,8 @@ import java.util.concurrent.atomic.AtomicReference;
* The data if fetch updated data from an external API on startup. If it fails,
* it uses the data stored in the current package at build time.
* <p>
* The public static methos are used to fetch an instance of {@link ProtocolVersion}
* based on the provided protocol version (eg. 763) or Minecraft version (eg. "1.20.1").
* The public static methods are used to fetch an instance of {@link ProtocolVersion}
* based on the provided protocol version (e.g. 763) or Minecraft version (e.g. "1.20.1").
* An instance of this class provides information related to a protocol version
* (the protocol version number and all the corresponding Minecraft versions).
*/
@@ -89,15 +89,24 @@ public class ProtocolVersion implements Comparable<ProtocolVersion> {
Log.warning("Unable to get minecraft version data from API. Using local data instead.");
// try local source
try (InputStream is = ProtocolVersion.class.getResourceAsStream("mcversion.json");
InputStreamReader isr = new InputStreamReader(is)) {
MinecraftVersionList data = Json.gson.fromJson(isr, MinecraftVersionList.class);
versionList.set(data);
try (InputStream is = ProtocolVersion.class.getResourceAsStream("mcversion.json")) {
if (is != null) {
try (InputStreamReader isr = new InputStreamReader(is)) {
MinecraftVersionList data = Json.gson.fromJson(isr, MinecraftVersionList.class);
versionList.set(data);
}
}
} catch (Exception e) {
Log.severe("Unable to get Minecraft versions data from classpath. Using empty data instead.");
versionList.set(new MinecraftVersionList());
Log.warning(e);
}
if (versionList.get() != null) {
return;
}
Log.severe("Unable to get Minecraft versions data from classpath. Using empty data instead.");
versionList.set(new MinecraftVersionList());
}

View File

@@ -15,7 +15,7 @@ import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
/**
* Utility class to manage searching among a set of {@link SearchResult} instances, using case insensitive keywords.
* Utility class to manage searching among a set of {@link SearchResult} instances, using case-insensitive keywords.
* The search engine is responsible for storing a database of entries ({@link SearchResult}) that can be searched using
* keywords. This class provides methods to returns a list of results for provided keywords, a list of keyword
* suggestions based on pre-typed keywords.