Update to Java 16 + replace javatuples usage with records

This commit is contained in:
Marc Baloup 2021-06-18 02:41:48 +02:00
parent 29d036a13f
commit 918ab59b69
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
15 changed files with 89 additions and 90 deletions

View File

@ -13,7 +13,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -1,8 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=11
org.eclipse.jdt.core.compiler.source=16

View File

@ -13,7 +13,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -1,8 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=11
org.eclipse.jdt.core.compiler.source=16

View File

@ -27,16 +27,16 @@
<version>${bungeecord.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<!-- <dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
</dependency> -->
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.90</version>
<version>4.8.108</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -5,8 +5,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.javatuples.Pair;
import net.md_5.bungee.api.ChatColor;
public class ChatColorUtil {
@ -257,10 +255,12 @@ public class ChatColorUtil {
public static class ChatValueGradient {
List<Pair<Float, ChatColor>> colors = new ArrayList<>();
private record GradientValueColor(float value, ChatColor color) { }
List<GradientValueColor> colors = new ArrayList<>();
public synchronized ChatValueGradient add(float v, ChatColor col) {
colors.add(Pair.with(v, col));
colors.add(new GradientValueColor(v, col));
return this;
}
@ -268,23 +268,23 @@ public class ChatColorUtil {
if (colors.isEmpty())
throw new IllegalStateException("Must define at least one color in this ChatValueGradient instance.");
if (colors.size() == 1)
return colors.get(0).getValue1();
return colors.get(0).color();
colors.sort((p1, p2) -> Float.compare(p1.getValue0(), p2.getValue0()));
colors.sort((p1, p2) -> Float.compare(p1.value(), p2.value()));
if (v <= colors.get(0).getValue0())
return colors.get(0).getValue1();
if (v >= colors.get(colors.size() - 1).getValue0())
return colors.get(colors.size() - 1).getValue1();
if (v <= colors.get(0).value())
return colors.get(0).color();
if (v >= colors.get(colors.size() - 1).value())
return colors.get(colors.size() - 1).color();
int p1 = 1;
for (; p1 < colors.size(); p1++) {
if (colors.get(p1).getValue0() >= v)
if (colors.get(p1).value() >= v)
break;
}
int p0 = p1 - 1;
float v0 = colors.get(p0).getValue0(), v1 = colors.get(p1).getValue0();
ChatColor cc0 = colors.get(p0).getValue1(), cc1 = colors.get(p1).getValue1();
float v0 = colors.get(p0).value(), v1 = colors.get(p1).value();
ChatColor cc0 = colors.get(p0).color(), cc1 = colors.get(p1).color();
return interpolateColor(v0, v1, v, cc0, cc1);
}

View File

@ -13,8 +13,6 @@ import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import org.javatuples.Pair;
import fr.pandacube.lib.core.util.Log;
/**
@ -70,12 +68,12 @@ public final class DB {
Collection<SQLField<E, ?>> tableFields = elem.getFields().values();
boolean first = true;
for (SQLField<E, ?> f : tableFields) {
Pair<String, List<Object>> statementPart = f.forSQLPreparedStatement();
params.addAll(statementPart.getValue1());
ParameterizedSQLString statementPart = f.forSQLPreparedStatement();
params.addAll(statementPart.parameters());
if (!first) sql += ", ";
first = false;
sql += statementPart.getValue0();
sql += statementPart.sqlString();
}
sql += ", PRIMARY KEY id(id))";
@ -197,9 +195,9 @@ public final class DB {
List<Object> params = new ArrayList<>();
if (where != null) {
Pair<String, List<Object>> ret = where.toSQL();
sql += " WHERE " + ret.getValue0();
params.addAll(ret.getValue1());
ParameterizedSQLString ret = where.toSQL();
sql += " WHERE " + ret.sqlString();
params.addAll(ret.parameters());
}
if (orderBy != null) sql += " ORDER BY " + orderBy.toSQL();
if (limit != null) sql += " LIMIT " + limit;
@ -233,9 +231,9 @@ public final class DB {
List<Object> params = new ArrayList<>();
if (where != null) {
Pair<String, List<Object>> ret = where.toSQL();
sql += " WHERE " + ret.getValue0();
params.addAll(ret.getValue1());
ParameterizedSQLString ret = where.toSQL();
sql += " WHERE " + ret.sqlString();
params.addAll(ret.parameters());
}
sql += ";";
@ -302,12 +300,12 @@ public final class DB {
return truncateTable(elemClass);
}
Pair<String, List<Object>> whereData = where.toSQL();
ParameterizedSQLString whereData = where.toSQL();
String sql = "DELETE FROM " + getTableName(elemClass)
+ " WHERE " + whereData.getValue0()
+ " WHERE " + whereData.sqlString()
+ ";";
List<Object> params = new ArrayList<>(whereData.getValue1());
List<Object> params = new ArrayList<>(whereData.parameters());
return customUpdateStatement(sql, params);

View File

@ -0,0 +1,7 @@
package fr.pandacube.lib.core.db;
import java.util.List;
public record ParameterizedSQLString(String sqlString, List<Object> parameters) {
}

View File

@ -4,13 +4,11 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.javatuples.Pair;
import fr.pandacube.lib.core.db.SQLWhere.SQLWhereComp;
import fr.pandacube.lib.core.db.SQLWhere.SQLWhereComp.SQLComparator;
import fr.pandacube.lib.core.db.SQLWhere.SQLWhereIn;
import fr.pandacube.lib.core.db.SQLWhere.SQLWhereLike;
import fr.pandacube.lib.core.db.SQLWhere.SQLWhereNull;
import fr.pandacube.lib.core.db.SQLWhere.SQLWhereComp.SQLComparator;
public class SQLField<E extends SQLElement<E>, T> {
@ -40,10 +38,10 @@ public class SQLField<E extends SQLElement<E>, T> {
this(t, nul, false, deflt);
}
/* package */ Pair<String, List<Object>> forSQLPreparedStatement() {
/* package */ ParameterizedSQLString forSQLPreparedStatement() {
List<Object> params = new ArrayList<>(1);
if (defaultValue != null && !autoIncrement) params.add(defaultValue);
return new Pair<>("`" + getName() + "` " + type.toString() + (canBeNull ? " NULL" : " NOT NULL")
return new ParameterizedSQLString("`" + getName() + "` " + type.toString() + (canBeNull ? " NULL" : " NOT NULL")
+ (autoIncrement ? " AUTO_INCREMENT" : "")
+ ((defaultValue == null || autoIncrement) ? "" : " DEFAULT ?"), params);
}
@ -72,7 +70,7 @@ public class SQLField<E extends SQLElement<E>, T> {
*/
@Override
public String toString() {
return forSQLPreparedStatement().getValue0().replaceFirst("\\?",
return forSQLPreparedStatement().sqlString().replaceFirst("\\?",
(defaultValue != null && !autoIncrement) ? defaultValue.toString() : "");
}

View File

@ -5,8 +5,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.javatuples.Pair;
import fr.pandacube.lib.core.util.Log;
public class SQLUpdate<E extends SQLElement<E>> {
@ -57,9 +55,9 @@ public class SQLUpdate<E extends SQLElement<E>> {
}
if (where != null) {
Pair<String, List<Object>> ret = where.toSQL();
sql += " WHERE " + ret.getValue0();
params.addAll(ret.getValue1());
ParameterizedSQLString ret = where.toSQL();
sql += " WHERE " + ret.sqlString();
params.addAll(ret.parameters());
}
sql += ";";

View File

@ -5,18 +5,16 @@ import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import org.javatuples.Pair;
import fr.pandacube.lib.core.util.Log;
public abstract class SQLWhere<E extends SQLElement<E>> {
public abstract Pair<String, List<Object>> toSQL() throws DBException;
public abstract ParameterizedSQLString toSQL() throws DBException;
@Override
public String toString() {
try {
return toSQL().getValue0();
return toSQL().sqlString();
} catch (DBException e) {
Log.warning(e);
return "[SQLWhere.toString() error (see logs)]";
@ -66,7 +64,7 @@ public abstract class SQLWhere<E extends SQLElement<E>> {
}
@Override
public Pair<String, List<Object>> toSQL() throws DBException {
public ParameterizedSQLString toSQL() throws DBException {
if (conditions.isEmpty()) {
throw new DBException("SQLWhereChain needs at least one element inside !");
}
@ -79,12 +77,12 @@ public abstract class SQLWhere<E extends SQLElement<E>> {
if (!first) sql += " " + operator.sql + " ";
first = false;
Pair<String, List<Object>> ret = w.toSQL();
sql += "(" + ret.getValue0() + ")";
params.addAll(ret.getValue1());
ParameterizedSQLString ret = w.toSQL();
sql += "(" + ret.sqlString() + ")";
params.addAll(ret.parameters());
}
return new Pair<>(sql, params);
return new ParameterizedSQLString(sql, params);
}
protected enum SQLBoolOp {
@ -169,10 +167,10 @@ public abstract class SQLWhere<E extends SQLElement<E>> {
}
@Override
public Pair<String, List<Object>> toSQL() throws DBException {
public ParameterizedSQLString toSQL() throws DBException {
List<Object> params = new ArrayList<>();
SQLElement.addValueToSQLObjectList(params, left, right);
return new Pair<>("`" + left.getName() + "` " + comp.sql + " ? ", params);
return new ParameterizedSQLString("`" + left.getName() + "` " + comp.sql + " ? ", params);
}
/* package */ enum SQLComparator {
@ -217,11 +215,11 @@ public abstract class SQLWhere<E extends SQLElement<E>> {
}
@Override
public Pair<String, List<Object>> toSQL() throws DBException {
public ParameterizedSQLString toSQL() throws DBException {
List<Object> params = new ArrayList<>();
if (values.isEmpty())
return new Pair<>(" 1=0 ", params);
return new ParameterizedSQLString(" 1=0 ", params);
for (Object v : values)
SQLElement.addValueToSQLObjectList(params, field, v);
@ -230,7 +228,7 @@ public abstract class SQLWhere<E extends SQLElement<E>> {
for (int i = 0; i < questions.length; i++)
questions[i] = i % 2 == 0 ? '?' : ',';
return new Pair<>("`" + field.getName() + "` IN (" + new String(questions) + ") ", params);
return new ParameterizedSQLString("`" + field.getName() + "` IN (" + new String(questions) + ") ", params);
}
}
@ -261,10 +259,10 @@ public abstract class SQLWhere<E extends SQLElement<E>> {
}
@Override
public Pair<String, List<Object>> toSQL() {
public ParameterizedSQLString toSQL() {
ArrayList<Object> params = new ArrayList<>();
params.add(likeExpr);
return new Pair<>("`" + field.getName() + "` LIKE ? ", params);
return new ParameterizedSQLString("`" + field.getName() + "` LIKE ? ", params);
}
}
@ -297,8 +295,8 @@ public abstract class SQLWhere<E extends SQLElement<E>> {
}
@Override
public Pair<String, List<Object>> toSQL() {
return new Pair<>("`" + fild.getName() + "` IS " + ((nulll) ? "NULL" : "NOT NULL"), new ArrayList<>());
public ParameterizedSQLString toSQL() {
return new ParameterizedSQLString("`" + fild.getName() + "` IS " + ((nulll) ? "NULL" : "NOT NULL"), new ArrayList<>());
}
}

View File

@ -11,8 +11,6 @@ import java.util.concurrent.TimeUnit;
import java.util.function.ToIntBiFunction;
import java.util.stream.Collectors;
import org.javatuples.Pair;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@ -39,15 +37,16 @@ public class PlayerFinder {
.maximumSize(1000)
.build();
private static Cache<Pair<String, Boolean>, UUID> playerId = CacheBuilder.newBuilder()
record PlayerIdCacheKey(String pName, boolean old) { }
private static Cache<PlayerIdCacheKey, UUID> playerId = CacheBuilder.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.maximumSize(1000)
.build();
public static void clearCacheEntry(UUID pId, String pName) {
playerLastKnownName.invalidate(pId);
playerId.invalidate(Pair.with(pName.toLowerCase(), true));
playerId.invalidate(Pair.with(pName.toLowerCase(), false));
playerId.invalidate(new PlayerIdCacheKey(pName.toLowerCase(), true));
playerId.invalidate(new PlayerIdCacheKey(pName.toLowerCase(), false));
}
public static String getLastKnownName(UUID id) {
@ -88,7 +87,7 @@ public class PlayerFinder {
return null; // évite une recherche inutile dans la base de donnée
try {
return playerId.get(Pair.with(exactName.toLowerCase(), old), () -> {
return playerId.get(new PlayerIdCacheKey(exactName.toLowerCase(), old), () -> {
try {
SQLPlayer el = DB.getFirst(SQLPlayer.class,
SQLPlayer.playerName.like(exactName.replace("_", "\\_")),
@ -204,14 +203,15 @@ public class PlayerFinder {
return DIFF_CHAR_DISTANCE;
};
private static LoadingCache<String, List<Pair<String, UUID>>> namesCache = CacheBuilder.newBuilder()
record NamesCacheResult(String name, UUID id) { }
private static LoadingCache<String, List<NamesCacheResult>> namesCache = CacheBuilder.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.maximumSize(1)
.build(CacheLoader.from((String k) -> {
List<Pair<String, UUID>> cached = new ArrayList<>();
List<NamesCacheResult> cached = new ArrayList<>();
try {
DB.forEach(SQLPlayerNameHistory.class, el -> {
cached.add(Pair.with(el.get(SQLPlayerNameHistory.playerName), el.get(SQLPlayerNameHistory.playerId)));
cached.add(new NamesCacheResult(el.get(SQLPlayerNameHistory.playerName), el.get(SQLPlayerNameHistory.playerId)));
});
} catch (DBException e) {
throw new RuntimeException(e);
@ -226,12 +226,12 @@ public class PlayerFinder {
List<FoundName> foundNames = new ArrayList<>();
try {
namesCache.get("").forEach(el -> {
String name = el.getValue0();
String name = el.name();
int dist = new LevenshteinDistance(name.toLowerCase(), query, SURPLUS_CHAR_DISTANCE, MISSING_CHAR_DISTANCE, CHAR_DISTANCE).getCurrentDistance();
if (dist <= SEARCH_MAX_DISTANCE) {
FoundName n = new FoundName();
n.dist = dist;
n.id = el.getValue1();
n.id = el.id();
n.name = name;
foundNames.add(n);
}

View File

@ -6,7 +6,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -1,8 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=11
org.eclipse.jdt.core.compiler.source=16

14
pom.xml
View File

@ -37,8 +37,8 @@
<properties>
<build.number>unknown</build.number>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<bungeecord.version>1.16-R0.4-SNAPSHOT</bungeecord.version>
@ -54,11 +54,11 @@
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>