Delete CollectionUtils (unused)

This commit is contained in:
ljacqu 2017-01-21 14:47:18 +01:00
parent 8aa1bb9935
commit 666e242212
9 changed files with 28 additions and 178 deletions

View File

@ -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");

View File

@ -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<String> 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<String> parts) {
if (CollectionUtils.isEmpty(parts)) {
if (Utils.isCollectionEmpty(parts)) {
return null;
}

View File

@ -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<Integer> players = getXAuthPlayers();
if (CollectionUtils.isEmpty(players)) {
if (Utils.isCollectionEmpty(players)) {
sender.sendMessage("[AuthMe] Error while importing xAuthPlayers: did not find any players");
return;
}

View File

@ -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<List<String>> whitelist,
Property<List<String>> blacklist) {
List<String> whitelistValue = settings.getProperty(whitelist);
if (!CollectionUtils.isEmpty(whitelistValue)) {
if (!Utils.isCollectionEmpty(whitelistValue)) {
return containsIgnoreCase(whitelistValue, value);
}
List<String> blacklistValue = settings.getProperty(blacklist);
return CollectionUtils.isEmpty(blacklistValue) || !containsIgnoreCase(blacklistValue, value);
return Utils.isCollectionEmpty(blacklistValue) || !containsIgnoreCase(blacklistValue, value);
}
private static boolean containsIgnoreCase(Collection<String> coll, String needle) {

View File

@ -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<String> toPurge = dataSource.getRecordsToPurge(until, includeEntriesWithLastLoginZero);
if (CollectionUtils.isEmpty(toPurge)) {
if (Utils.isCollectionEmpty(toPurge)) {
logAndSendMessage(sender, "No players to purge");
return;
}

View File

@ -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 <T> 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 <T> List<T> getRange(List<T> 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 <T> 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 <T> List<T> getRange(List<T> 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();
}
}

View File

@ -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.
*

View File

@ -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<String> list = Arrays.asList("test", "1", "2", "3", "4");
// when
List<String> result = CollectionUtils.getRange(list, 0, 24);
// then
assertThat(result, equalTo(list));
}
@Test
public void shouldReturnEmptyListForZeroCount() {
// given
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
// when
List<String> result = CollectionUtils.getRange(list, 2, 0);
// then
assertThat(result, empty());
}
@Test
public void shouldReturnEmptyListForTooHighStart() {
// given
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
// when
List<String> result = CollectionUtils.getRange(list, 12, 2);
// then
assertThat(result, empty());
}
@Test
public void shouldReturnSubList() {
// given
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
// when
List<String> result = CollectionUtils.getRange(list, 1, 3);
// then
assertThat(result, contains("1", "2", "3"));
}
@Test
public void shouldReturnTillEnd() {
// given
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
// when
List<String> result = CollectionUtils.getRange(list, 2, 3);
// then
assertThat(result, contains("2", "3", "4"));
}
@Test
public void shouldRemoveFirstTwo() {
// given
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
// when
List<String> result = CollectionUtils.getRange(list, 2);
// then
assertThat(result, contains("2", "3", "4"));
}
@Test
public void shouldHandleNegativeStart() {
// given
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
// when
List<String> result = CollectionUtils.getRange(list, -4);
// then
assertThat(result, equalTo(list));
}
}

View File

@ -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();