diff --git a/src/main/java/fr/xephi/authme/command/CommandDescription.java b/src/main/java/fr/xephi/authme/command/CommandDescription.java index b6c006f9b..18463bcaa 100644 --- a/src/main/java/fr/xephi/authme/command/CommandDescription.java +++ b/src/main/java/fr/xephi/authme/command/CommandDescription.java @@ -1,8 +1,8 @@ package fr.xephi.authme.command; import fr.xephi.authme.permission.PermissionNode; -import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.StringUtils; +import fr.xephi.authme.util.Utils; import java.util.ArrayList; import java.util.List; @@ -224,7 +224,7 @@ public class CommandDescription { * @return The generated CommandDescription object */ public CommandDescription build() { - checkArgument(!CollectionUtils.isEmpty(labels), "Labels may not be empty"); + checkArgument(!Utils.isCollectionEmpty(labels), "Labels may not be empty"); checkArgument(!StringUtils.isEmpty(description), "Description may not be empty"); checkArgument(!StringUtils.isEmpty(detailedDescription), "Detailed description may not be empty"); checkArgument(executableCommand != null, "Executable command must be set"); diff --git a/src/main/java/fr/xephi/authme/command/CommandMapper.java b/src/main/java/fr/xephi/authme/command/CommandMapper.java index 8e2b1bdc1..c5cf91c0f 100644 --- a/src/main/java/fr/xephi/authme/command/CommandMapper.java +++ b/src/main/java/fr/xephi/authme/command/CommandMapper.java @@ -2,8 +2,8 @@ package fr.xephi.authme.command; import fr.xephi.authme.command.executable.HelpCommand; import fr.xephi.authme.permission.PermissionsManager; -import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.StringUtils; +import fr.xephi.authme.util.Utils; import org.bukkit.command.CommandSender; import javax.inject.Inject; @@ -45,7 +45,7 @@ public class CommandMapper { * @return The generated {@link FoundCommandResult} */ public FoundCommandResult mapPartsToCommand(CommandSender sender, final List parts) { - if (CollectionUtils.isEmpty(parts)) { + if (Utils.isCollectionEmpty(parts)) { return new FoundCommandResult(null, parts, null, 0.0, MISSING_BASE_COMMAND); } @@ -142,7 +142,7 @@ public class CommandMapper { * @return A command if there was a complete match (including proper argument count), null otherwise */ private static CommandDescription getSuitableChild(CommandDescription baseCommand, List parts) { - if (CollectionUtils.isEmpty(parts)) { + if (Utils.isCollectionEmpty(parts)) { return null; } diff --git a/src/main/java/fr/xephi/authme/datasource/converter/xAuthConverter.java b/src/main/java/fr/xephi/authme/datasource/converter/xAuthConverter.java index 27fa6c2e9..af4d5beb7 100644 --- a/src/main/java/fr/xephi/authme/datasource/converter/xAuthConverter.java +++ b/src/main/java/fr/xephi/authme/datasource/converter/xAuthConverter.java @@ -6,7 +6,7 @@ import de.luricos.bukkit.xAuth.xAuth; import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.initialization.DataFolder; -import fr.xephi.authme.util.CollectionUtils; +import fr.xephi.authme.util.Utils; import org.bukkit.command.CommandSender; import org.bukkit.plugin.PluginManager; @@ -55,7 +55,7 @@ public class xAuthConverter implements Converter { sender.sendMessage("[AuthMe] xAuth H2 database not found, checking for MySQL or SQLite data..."); } List players = getXAuthPlayers(); - if (CollectionUtils.isEmpty(players)) { + if (Utils.isCollectionEmpty(players)) { sender.sendMessage("[AuthMe] Error while importing xAuthPlayers: did not find any players"); return; } diff --git a/src/main/java/fr/xephi/authme/service/ValidationService.java b/src/main/java/fr/xephi/authme/service/ValidationService.java index 8ef7b1daa..b6e338ec0 100644 --- a/src/main/java/fr/xephi/authme/service/ValidationService.java +++ b/src/main/java/fr/xephi/authme/service/ValidationService.java @@ -11,7 +11,6 @@ import fr.xephi.authme.settings.properties.EmailSettings; import fr.xephi.authme.settings.properties.ProtectionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.SecuritySettings; -import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.Utils; import org.bukkit.command.CommandSender; @@ -144,11 +143,11 @@ public class ValidationService implements Reloadable { private boolean validateWhitelistAndBlacklist(String value, Property> whitelist, Property> blacklist) { List whitelistValue = settings.getProperty(whitelist); - if (!CollectionUtils.isEmpty(whitelistValue)) { + if (!Utils.isCollectionEmpty(whitelistValue)) { return containsIgnoreCase(whitelistValue, value); } List blacklistValue = settings.getProperty(blacklist); - return CollectionUtils.isEmpty(blacklistValue) || !containsIgnoreCase(blacklistValue, value); + return Utils.isCollectionEmpty(blacklistValue) || !containsIgnoreCase(blacklistValue, value); } private static boolean containsIgnoreCase(Collection coll, String needle) { diff --git a/src/main/java/fr/xephi/authme/task/purge/PurgeService.java b/src/main/java/fr/xephi/authme/task/purge/PurgeService.java index 7a1d19fa4..fed077682 100644 --- a/src/main/java/fr/xephi/authme/task/purge/PurgeService.java +++ b/src/main/java/fr/xephi/authme/task/purge/PurgeService.java @@ -3,10 +3,10 @@ package fr.xephi.authme.task.purge; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.permission.PermissionsManager; +import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.PurgeSettings; -import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.util.CollectionUtils; +import fr.xephi.authme.util.Utils; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; @@ -75,7 +75,7 @@ public class PurgeService { public void runPurge(CommandSender sender, long until, boolean includeEntriesWithLastLoginZero) { //todo: note this should may run async because it may executes a SQL-Query Set toPurge = dataSource.getRecordsToPurge(until, includeEntriesWithLastLoginZero); - if (CollectionUtils.isEmpty(toPurge)) { + if (Utils.isCollectionEmpty(toPurge)) { logAndSendMessage(sender, "No players to purge"); return; } diff --git a/src/main/java/fr/xephi/authme/util/CollectionUtils.java b/src/main/java/fr/xephi/authme/util/CollectionUtils.java deleted file mode 100644 index 2388fc608..000000000 --- a/src/main/java/fr/xephi/authme/util/CollectionUtils.java +++ /dev/null @@ -1,63 +0,0 @@ -package fr.xephi.authme.util; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -/** - * Utils class for collections. - */ -public final class CollectionUtils { - - // Utility class - private CollectionUtils() { - } - - /** - * Get a range from a list based on start and count parameters in a safe way. - * - * @param element - * @param list The List - * @param start The start index - * @param count The number of elements to add - * - * @return The sublist consisting at most of {@code count} elements (less if the parameters - * exceed the size of the list) - */ - public static List getRange(List list, int start, int count) { - if (start >= list.size() || count <= 0) { - return new ArrayList<>(); - } else if (start < 0) { - start = 0; - } - int end = Math.min(list.size(), start + count); - return list.subList(start, end); - } - - /** - * Get all elements from a list starting from the given index. - * - * @param element - * @param list The List - * @param start The start index - * - * @return The sublist of all elements from index {@code start} and on; empty list - * if the start index exceeds the list's size - */ - public static List getRange(List list, int start) { - if (start >= list.size()) { - return new ArrayList<>(); - } - return getRange(list, start, list.size() - start); - } - - /** - * Null-safe way to check whether a collection is empty or not. - * - * @param coll The collection to verify - * @return True if the collection is null or empty, false otherwise - */ - public static boolean isEmpty(Collection coll) { - return coll == null || coll.isEmpty(); - } -} diff --git a/src/main/java/fr/xephi/authme/util/Utils.java b/src/main/java/fr/xephi/authme/util/Utils.java index b3ec53c7b..b7628147b 100644 --- a/src/main/java/fr/xephi/authme/util/Utils.java +++ b/src/main/java/fr/xephi/authme/util/Utils.java @@ -2,6 +2,7 @@ package fr.xephi.authme.util; import fr.xephi.authme.ConsoleLogger; +import java.util.Collection; import java.util.regex.Pattern; /** @@ -50,6 +51,16 @@ public final class Utils { } } + /** + * Null-safe way to check whether a collection is empty or not. + * + * @param coll The collection to verify + * @return True if the collection is null or empty, false otherwise + */ + public static boolean isCollectionEmpty(Collection coll) { + return coll == null || coll.isEmpty(); + } + /** * Return the available core count of the JVM. * diff --git a/src/test/java/fr/xephi/authme/util/CollectionUtilsTest.java b/src/test/java/fr/xephi/authme/util/CollectionUtilsTest.java deleted file mode 100644 index f359f0883..000000000 --- a/src/test/java/fr/xephi/authme/util/CollectionUtilsTest.java +++ /dev/null @@ -1,102 +0,0 @@ -package fr.xephi.authme.util; - -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.hamcrest.Matchers.empty; - -/** - * Test for {@link CollectionUtils}. - */ -public class CollectionUtilsTest { - - @Test - public void shouldGetFullList() { - // given - List list = Arrays.asList("test", "1", "2", "3", "4"); - - // when - List result = CollectionUtils.getRange(list, 0, 24); - - // then - assertThat(result, equalTo(list)); - } - - @Test - public void shouldReturnEmptyListForZeroCount() { - // given - List list = Arrays.asList("test", "1", "2", "3", "4"); - - // when - List result = CollectionUtils.getRange(list, 2, 0); - - // then - assertThat(result, empty()); - } - - - @Test - public void shouldReturnEmptyListForTooHighStart() { - // given - List list = Arrays.asList("test", "1", "2", "3", "4"); - - // when - List result = CollectionUtils.getRange(list, 12, 2); - - // then - assertThat(result, empty()); - } - - @Test - public void shouldReturnSubList() { - // given - List list = Arrays.asList("test", "1", "2", "3", "4"); - - // when - List result = CollectionUtils.getRange(list, 1, 3); - - // then - assertThat(result, contains("1", "2", "3")); - } - - @Test - public void shouldReturnTillEnd() { - // given - List list = Arrays.asList("test", "1", "2", "3", "4"); - - // when - List result = CollectionUtils.getRange(list, 2, 3); - - // then - assertThat(result, contains("2", "3", "4")); - } - - @Test - public void shouldRemoveFirstTwo() { - // given - List list = Arrays.asList("test", "1", "2", "3", "4"); - - // when - List result = CollectionUtils.getRange(list, 2); - - // then - assertThat(result, contains("2", "3", "4")); - } - - @Test - public void shouldHandleNegativeStart() { - // given - List list = Arrays.asList("test", "1", "2", "3", "4"); - - // when - List result = CollectionUtils.getRange(list, -4); - - // then - assertThat(result, equalTo(list)); - } -} diff --git a/src/test/java/fr/xephi/authme/util/FileUtilsTest.java b/src/test/java/fr/xephi/authme/util/FileUtilsTest.java index fa8ee9c6d..9d65047c5 100644 --- a/src/test/java/fr/xephi/authme/util/FileUtilsTest.java +++ b/src/test/java/fr/xephi/authme/util/FileUtilsTest.java @@ -137,6 +137,11 @@ public class FileUtilsTest { assertThat(result, equalTo("path" + File.separator + "to" + File.separator + "test-file.txt")); } + @Test + public void shouldHaveHiddenConstructor() { + TestHelper.validateHasOnlyPrivateEmptyConstructor(FileUtils.class); + } + private static void createFiles(File... files) throws IOException { for (File file : files) { boolean result = file.getParentFile().mkdirs() & file.createNewFile();