From 918ab59b6995691582fd93ae00be9dcd45885eec Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 18 Jun 2021 02:41:48 +0200 Subject: [PATCH] Update to Java 16 + replace javatuples usage with records --- Bungee/.classpath | 2 +- Bungee/.settings/org.eclipse.jdt.core.prefs | 6 ++-- Core/.classpath | 2 +- Core/.settings/org.eclipse.jdt.core.prefs | 6 ++-- Core/pom.xml | 6 ++-- .../lib/core/chat/ChatColorUtil.java | 26 +++++++------- .../java/fr/pandacube/lib/core/db/DB.java | 26 +++++++------- .../lib/core/db/ParameterizedSQLString.java | 7 ++++ .../fr/pandacube/lib/core/db/SQLField.java | 10 +++--- .../fr/pandacube/lib/core/db/SQLUpdate.java | 8 ++--- .../fr/pandacube/lib/core/db/SQLWhere.java | 34 +++++++++---------- .../lib/core/players/PlayerFinder.java | 24 ++++++------- Paper/.classpath | 2 +- Paper/.settings/org.eclipse.jdt.core.prefs | 6 ++-- pom.xml | 14 ++++---- 15 files changed, 89 insertions(+), 90 deletions(-) create mode 100644 Core/src/main/java/fr/pandacube/lib/core/db/ParameterizedSQLString.java diff --git a/Bungee/.classpath b/Bungee/.classpath index 4559ca0..d4e0e69 100644 --- a/Bungee/.classpath +++ b/Bungee/.classpath @@ -13,7 +13,7 @@ - + diff --git a/Bungee/.settings/org.eclipse.jdt.core.prefs b/Bungee/.settings/org.eclipse.jdt.core.prefs index 2af1e7b..1b835cb 100644 --- a/Bungee/.settings/org.eclipse.jdt.core.prefs +++ b/Bungee/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/Core/.classpath b/Core/.classpath index 4559ca0..d4e0e69 100644 --- a/Core/.classpath +++ b/Core/.classpath @@ -13,7 +13,7 @@ - + diff --git a/Core/.settings/org.eclipse.jdt.core.prefs b/Core/.settings/org.eclipse.jdt.core.prefs index 2af1e7b..1b835cb 100644 --- a/Core/.settings/org.eclipse.jdt.core.prefs +++ b/Core/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/Core/pom.xml b/Core/pom.xml index ac6fc7c..508fbfa 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -27,16 +27,16 @@ ${bungeecord.version} compile - + io.github.classgraph classgraph - 4.8.90 + 4.8.108 compile diff --git a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatColorUtil.java b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatColorUtil.java index 99e3e4f..8026064 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatColorUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatColorUtil.java @@ -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> colors = new ArrayList<>(); + private record GradientValueColor(float value, ChatColor color) { } + + List 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); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/DB.java b/Core/src/main/java/fr/pandacube/lib/core/db/DB.java index b36c202..694cdc1 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/DB.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/DB.java @@ -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> tableFields = elem.getFields().values(); boolean first = true; for (SQLField f : tableFields) { - Pair> 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 params = new ArrayList<>(); if (where != null) { - Pair> 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 params = new ArrayList<>(); if (where != null) { - Pair> 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> whereData = where.toSQL(); + ParameterizedSQLString whereData = where.toSQL(); String sql = "DELETE FROM " + getTableName(elemClass) - + " WHERE " + whereData.getValue0() + + " WHERE " + whereData.sqlString() + ";"; - List params = new ArrayList<>(whereData.getValue1()); + List params = new ArrayList<>(whereData.parameters()); return customUpdateStatement(sql, params); diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/ParameterizedSQLString.java b/Core/src/main/java/fr/pandacube/lib/core/db/ParameterizedSQLString.java new file mode 100644 index 0000000..a5eb1d0 --- /dev/null +++ b/Core/src/main/java/fr/pandacube/lib/core/db/ParameterizedSQLString.java @@ -0,0 +1,7 @@ +package fr.pandacube.lib.core.db; + +import java.util.List; + +public record ParameterizedSQLString(String sqlString, List parameters) { + +} diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLField.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLField.java index 3f0113a..3102689 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLField.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLField.java @@ -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, T> { @@ -40,10 +38,10 @@ public class SQLField, T> { this(t, nul, false, deflt); } - /* package */ Pair> forSQLPreparedStatement() { + /* package */ ParameterizedSQLString forSQLPreparedStatement() { List 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, T> { */ @Override public String toString() { - return forSQLPreparedStatement().getValue0().replaceFirst("\\?", + return forSQLPreparedStatement().sqlString().replaceFirst("\\?", (defaultValue != null && !autoIncrement) ? defaultValue.toString() : ""); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLUpdate.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLUpdate.java index fb4a416..ee57494 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLUpdate.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLUpdate.java @@ -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> { @@ -57,9 +55,9 @@ public class SQLUpdate> { } if (where != null) { - Pair> ret = where.toSQL(); - sql += " WHERE " + ret.getValue0(); - params.addAll(ret.getValue1()); + ParameterizedSQLString ret = where.toSQL(); + sql += " WHERE " + ret.sqlString(); + params.addAll(ret.parameters()); } sql += ";"; diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLWhere.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLWhere.java index 799471b..5502776 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLWhere.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLWhere.java @@ -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> { - public abstract Pair> 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> { } @Override - public Pair> 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> { if (!first) sql += " " + operator.sql + " "; first = false; - Pair> 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> { } @Override - public Pair> toSQL() throws DBException { + public ParameterizedSQLString toSQL() throws DBException { List 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> { } @Override - public Pair> toSQL() throws DBException { + public ParameterizedSQLString toSQL() throws DBException { List 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> { 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> { } @Override - public Pair> toSQL() { + public ParameterizedSQLString toSQL() { ArrayList 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> { } @Override - public Pair> 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<>()); } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java b/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java index dced1c6..2f3e332 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java @@ -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; @@ -38,16 +36,17 @@ public class PlayerFinder { .expireAfterWrite(2, TimeUnit.MINUTES) .maximumSize(1000) .build(); - - private static Cache, UUID> playerId = CacheBuilder.newBuilder() + + record PlayerIdCacheKey(String pName, boolean old) { } + private static Cache 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>> namesCache = CacheBuilder.newBuilder() + record NamesCacheResult(String name, UUID id) { } + private static LoadingCache> namesCache = CacheBuilder.newBuilder() .expireAfterWrite(2, TimeUnit.MINUTES) .maximumSize(1) .build(CacheLoader.from((String k) -> { - List> cached = new ArrayList<>(); + List 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 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); } diff --git a/Paper/.classpath b/Paper/.classpath index 3721ade..a4f28fd 100644 --- a/Paper/.classpath +++ b/Paper/.classpath @@ -6,7 +6,7 @@ - + diff --git a/Paper/.settings/org.eclipse.jdt.core.prefs b/Paper/.settings/org.eclipse.jdt.core.prefs index 2af1e7b..1b835cb 100644 --- a/Paper/.settings/org.eclipse.jdt.core.prefs +++ b/Paper/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/pom.xml b/pom.xml index bab1ff9..3a4162f 100644 --- a/pom.xml +++ b/pom.xml @@ -37,8 +37,8 @@ unknown - 11 - 11 + 16 + 16 UTF-8 1.16-R0.4-SNAPSHOT @@ -54,11 +54,11 @@ - junit - junit - 4.12 - test - + org.junit.jupiter + junit-jupiter-api + 5.7.2 + test +