Fixed various code smells

- Replaced all uses of boxed primitive type Suppliers with specialized
  suppliers Eg. Supplier<Integer> with IntSupplier
- Replaced uses of CharSet.forName("UTF-8") with StandardCharsets.UTF-8
- Removed Class.forName("org.h2.Driver") as this check is not required
  since JDBC drivers in the classpath are always loaded since JDK 6.
This commit is contained in:
Rsl1122 2018-11-16 19:24:19 +02:00
parent 4be6fec077
commit ffa04f1b9f
8 changed files with 32 additions and 30 deletions

View File

@ -18,17 +18,17 @@ package com.djrapitops.plan.system.info.server.properties;
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import java.util.function.Supplier;
import java.util.function.IntSupplier;
/**
* Players online supplier when using RedisBungee.
*
* @author Rsl1122
*/
public class RedisPlayersOnlineSupplier implements Supplier<Integer> {
public class RedisPlayersOnlineSupplier implements IntSupplier {
@Override
public Integer get() {
public int getAsInt() {
return RedisBungee.getApi().getPlayerCount();
}
}

View File

@ -72,12 +72,6 @@ public class H2DB extends SQLDB {
@Override
public void setupDataSource() throws DBInitException {
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
errorHandler.log(L.CRITICAL, this.getClass(), e);
}
try {
connection = getNewConnection(databaseFile);
} catch (SQLException e) {

View File

@ -16,6 +16,7 @@
*/
package com.djrapitops.plan.system.info.server.properties;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
/**
@ -35,12 +36,18 @@ public abstract class ServerProperties {
private final Supplier<String> ip;
private final int maxPlayers;
private final Supplier<Integer> onlinePlayers;
private final IntSupplier onlinePlayers;
protected ServerProperties(
String id, String name, int port,
String version, String implVersion,
Supplier<String> ip, int maxPlayers, Supplier<Integer> onlinePlayers) {
String id,
String name,
int port,
String version,
String implVersion,
Supplier<String> ip,
int maxPlayers,
IntSupplier onlinePlayers
) {
this.id = id;
this.name = name;
this.port = port;
@ -85,6 +92,6 @@ public abstract class ServerProperties {
}
public int getOnlinePlayers() {
return onlinePlayers.get();
return onlinePlayers.getAsInt();
}
}

View File

@ -20,7 +20,7 @@ import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.database.databases.operation.SaveOperations;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.function.BooleanSupplier;
/**
* Updates ban and OP status of the player to the database.
@ -30,13 +30,13 @@ import java.util.function.Supplier;
public class BanAndOpProcessor implements Runnable {
private final UUID uuid;
private final Supplier<Boolean> banned;
private final BooleanSupplier banned;
private final boolean op;
private final Database database;
BanAndOpProcessor(
UUID uuid, Supplier<Boolean> banned, boolean op,
UUID uuid, BooleanSupplier banned, boolean op,
Database database
) {
this.uuid = uuid;
@ -48,7 +48,7 @@ public class BanAndOpProcessor implements Runnable {
@Override
public void run() {
SaveOperations save = database.save();
save.banStatus(uuid, banned.get());
save.banStatus(uuid, banned.getAsBoolean());
save.opStatus(uuid, op);
}
}

View File

@ -30,7 +30,8 @@ import javax.inject.Singleton;
import java.net.InetAddress;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.function.BooleanSupplier;
import java.util.function.LongSupplier;
/**
* Factory for creating Runnables related to Player data to run with {@link com.djrapitops.plan.system.processing.Processing}.
@ -61,7 +62,7 @@ public class PlayerProcessors {
this.geolocationCache = geolocationCache;
}
public BanAndOpProcessor banAndOpProcessor(UUID uuid, Supplier<Boolean> banned, boolean op) {
public BanAndOpProcessor banAndOpProcessor(UUID uuid, BooleanSupplier banned, boolean op) {
return new BanAndOpProcessor(uuid, banned, op, dbSystem.get().getDatabase());
}
@ -90,7 +91,7 @@ public class PlayerProcessors {
return new PingInsertProcessor(uuid, serverInfo.get().getServerUUID(), pingList, dbSystem.get().getDatabase());
}
public RegisterProcessor registerProcessor(UUID uuid, Supplier<Long> registered, String name, Runnable... afterProcess) {
public RegisterProcessor registerProcessor(UUID uuid, LongSupplier registered, String name, Runnable... afterProcess) {
return new RegisterProcessor(uuid, registered, name, processing.get(), dbSystem.get().getDatabase(), afterProcess);
}

View File

@ -24,7 +24,7 @@ import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.utilities.Verify;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.function.LongSupplier;
/**
* Registers the user to the database and marks first session if the user has no actions.
@ -34,7 +34,7 @@ import java.util.function.Supplier;
public class RegisterProcessor extends AbsRunnable {
private final UUID uuid;
private final Supplier<Long> registered;
private final LongSupplier registered;
private final String name;
private final Runnable[] afterProcess;
@ -42,7 +42,7 @@ public class RegisterProcessor extends AbsRunnable {
private final Database database;
RegisterProcessor(
UUID uuid, Supplier<Long> registered, String name,
UUID uuid, LongSupplier registered, String name,
Processing processing, Database database,
Runnable... afterProcess
) {
@ -62,10 +62,10 @@ public class RegisterProcessor extends AbsRunnable {
SaveOperations save = database.save();
try {
if (!check.isPlayerRegistered(uuid)) {
save.registerNewUser(uuid, registered.get(), name);
save.registerNewUser(uuid, registered.getAsLong(), name);
}
if (!check.isPlayerRegisteredOnThisServer(uuid)) {
save.registerNewUserOnThisServer(uuid, registered.get());
save.registerNewUserOnThisServer(uuid, registered.getAsLong());
}
} finally {
for (Runnable runnable : afterProcess) {

View File

@ -27,7 +27,7 @@ import com.djrapitops.plugin.api.Check;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
@ -79,7 +79,7 @@ public abstract class SpecificExport implements Runnable {
}
protected void export(File to, List<String> lines) throws IOException {
Files.write(to.toPath(), lines, Charset.forName("UTF-8"));
Files.write(to.toPath(), lines, StandardCharsets.UTF_8);
}
protected File getServerFolder() {

View File

@ -7,7 +7,7 @@ import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;
@ -27,7 +27,7 @@ public class LocaleFileWriterTest {
new LocaleFileWriter(new Locale()).writeToFile(file);
long expected = LocaleSystem.getIdentifiers().size();
int result = FileUtil.lines(file, Charset.forName("UTF-8")).size();
int result = FileUtil.lines(file, StandardCharsets.UTF_8).size();
assertEquals(expected, result);
}