From a3cd3adb3245e44927f39c96355f7147a1d91a44 Mon Sep 17 00:00:00 2001 From: Rsl1122 <24460436+Rsl1122@users.noreply.github.com> Date: Thu, 19 Dec 2019 00:09:29 +0200 Subject: [PATCH] Cleaning up some code: - Some extension API implementation things refactored - getOrDefault+put calls replaced with computeIfAbsent using Maps#create, Lists#create - stream().map(mapper).collect(toList/toSet) optimized using Lists#map, #mapUnique - stream().filter(by).collect(toList) optimized using Lists#filter --- .gitignore | 2 + .../domain/mutators/DateHoldersMutator.java | 21 +++-- .../domain/mutators/MutatorFunctions.java | 6 +- .../delivery/domain/mutators/PingMutator.java | 20 ++--- .../domain/mutators/PlayerKillMutator.java | 8 +- .../domain/mutators/PlayerVersusMutator.java | 5 +- .../domain/mutators/PlayersMutator.java | 38 ++++---- .../domain/mutators/SessionsMutator.java | 52 ++++++----- .../delivery/domain/mutators/TPSMutator.java | 25 ++---- .../rendering/json/PlayerJSONCreator.java | 6 +- .../json/graphs/GraphJSONCreator.java | 8 +- .../json/graphs/calendar/PlayerCalendar.java | 4 +- .../rendering/pages/ServerPluginTabs.java | 6 +- .../response/pages/RawDataResponse.java | 12 +-- .../implementation/CallerImplementation.java | 10 +-- .../implementation/DataProviderExtractor.java | 59 ++++++------- .../providers/DataProvider.java | 16 ++++ .../providers/DataProviders.java | 66 +++++++------- .../providers/MethodWrapper.java | 24 ++--- .../gathering/ProviderValueGatherer.java | 2 +- .../results/ExtensionTabData.java | 11 +-- .../queries/ExtensionInformationQueries.java | 4 +- .../queries/ExtensionPlayerDataQuery.java | 4 +- .../storage/queries/QueriedTabData.java | 4 +- .../storage/queries/QueriedTables.java | 4 +- .../database/queries/LargeFetchQueries.java | 8 +- .../ServerPlayerContainersQuery.java | 2 +- .../queries/objects/GeoInfoQueries.java | 11 +-- .../queries/objects/NicknameQueries.java | 13 ++- .../database/queries/objects/PingQueries.java | 4 +- .../queries/objects/SessionQueries.java | 7 +- .../database/queries/objects/TPSQueries.java | 6 +- .../queries/objects/UserInfoQueries.java | 4 +- .../events/PingStoreTransaction.java | 5 +- .../patches/NicknameLastSeenPatch.java | 13 +-- .../djrapitops/plan/utilities/Predicates.java | 5 +- .../djrapitops/plan/utilities/java/Lists.java | 88 +++++++++++++++++++ .../djrapitops/plan/utilities/java/Maps.java | 43 +++++++++ .../plan/version/VersionCheckSystem.java | 4 +- .../plan/storage/database/DatabaseTest.java | 7 +- .../utilities/comparators/ComparatorTest.java | 5 +- .../plan/utilities/java/MapsSanityTest.java | 43 +++++++++ 42 files changed, 423 insertions(+), 262 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/utilities/java/Lists.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/utilities/java/Maps.java create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/utilities/java/MapsSanityTest.java diff --git a/.gitignore b/.gitignore index 454fa1e71..74a16a4ad 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ Plan.iml PlanPluginBridge.iml .sonar/ builds/ +# Nukkit creates server.log during tests for some reason. +server.log *.db diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/DateHoldersMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/DateHoldersMutator.java index d69c0b5f0..1aae76093 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/DateHoldersMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/DateHoldersMutator.java @@ -17,6 +17,7 @@ package com.djrapitops.plan.delivery.domain.mutators; import com.djrapitops.plan.delivery.domain.DateHolder; +import com.djrapitops.plan.utilities.java.Lists; import java.util.*; import java.util.concurrent.TimeUnit; @@ -41,19 +42,18 @@ public class DateHoldersMutator { long date = holder.getDate(); long startOfSection = date - (date % sectionLenght); - List list = map.getOrDefault(startOfSection, new ArrayList<>()); + List list = map.computeIfAbsent(startOfSection, Lists::create); list.add(holder); - map.put(startOfSection, list); } return map; } public SortedMap> groupByStartOfDay(TimeZone timeZone) { long twentyFourHours = TimeUnit.DAYS.toMillis(1L); - TreeMap> map = new TreeMap<>(); + TreeMap> byStart = new TreeMap<>(); if (dateHolders.isEmpty()) { - return map; + return byStart; } for (T holder : dateHolders) { @@ -61,22 +61,21 @@ public class DateHoldersMutator { long dateWithOffset = date + timeZone.getOffset(date); long startOfSection = dateWithOffset - (dateWithOffset % twentyFourHours); - List list = map.getOrDefault(startOfSection, new ArrayList<>()); - list.add(holder); - map.put(startOfSection, list); + List grouped = byStart.computeIfAbsent(startOfSection, Lists::create); + grouped.add(holder); } // Empty map firstKey attempt causes NPE if not checked. - if (!map.isEmpty()) { + if (!byStart.isEmpty()) { // Add missing in-between dates - long start = map.firstKey(); + long start = byStart.firstKey(); long now = System.currentTimeMillis(); long end = now - (now % twentyFourHours); for (long date = start; date < end; date += twentyFourHours) { - map.putIfAbsent(date, new ArrayList<>()); + byStart.putIfAbsent(date, new ArrayList<>()); } } - return map; + return byStart; } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/MutatorFunctions.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/MutatorFunctions.java index bed4699c3..b316b31fb 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/MutatorFunctions.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/MutatorFunctions.java @@ -17,17 +17,15 @@ package com.djrapitops.plan.delivery.domain.mutators; import com.djrapitops.plan.delivery.rendering.json.graphs.line.Point; +import com.djrapitops.plan.utilities.java.Lists; import com.djrapitops.plugin.utilities.Verify; import java.util.*; -import java.util.stream.Collectors; public class MutatorFunctions { public static List toPoints(NavigableMap map) { - return map.entrySet().stream() - .map(entry -> new Point(entry.getKey(), entry.getValue())) - .collect(Collectors.toList()); + return Lists.map(map.entrySet(), entry -> new Point(entry.getKey(), entry.getValue())); } public static NavigableMap addMissing(NavigableMap points, long accuracy, Integer replacement) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PingMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PingMutator.java index 034daa861..e2c88be37 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PingMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PingMutator.java @@ -22,11 +22,12 @@ import com.djrapitops.plan.delivery.domain.keys.CommonKeys; import com.djrapitops.plan.delivery.domain.keys.SessionKeys; import com.djrapitops.plan.gathering.domain.Ping; import com.djrapitops.plan.gathering.domain.Session; +import com.djrapitops.plan.utilities.Predicates; import com.djrapitops.plan.utilities.comparators.DateHolderOldestComparator; +import com.djrapitops.plan.utilities.java.Lists; import java.util.*; import java.util.function.Predicate; -import java.util.stream.Collectors; public class PingMutator { @@ -41,7 +42,7 @@ public class PingMutator { } public PingMutator filterBy(Predicate predicate) { - return new PingMutator(pings.stream().filter(predicate).collect(Collectors.toList())); + return new PingMutator(Lists.filter(pings, predicate)); } public PingMutator filterByServer(UUID serverUUID) { @@ -52,13 +53,12 @@ public class PingMutator { DateHoldersMutator dateMutator = new DateHoldersMutator<>(pings); SortedMap> byStartOfMinute = dateMutator.groupByStartOfMinute(); - return new PingMutator(byStartOfMinute.entrySet().stream() - .map(entry -> { - PingMutator mutator = new PingMutator(entry.getValue()); + return new PingMutator(Lists.map(byStartOfMinute.entrySet(), entry -> { + PingMutator mutator = new PingMutator(entry.getValue()); - return new Ping(entry.getKey(), null, - mutator.min(), mutator.max(), mutator.average()); - }).collect(Collectors.toList())); + return new Ping(entry.getKey(), null, + mutator.min(), mutator.max(), mutator.average()); + })); } public static Map> sortByServers(List pings) { @@ -92,7 +92,7 @@ public class PingMutator { for (Session session : sessionsOfServer) { long start = session.getDate(); Long end = session.getValue(SessionKeys.END).orElseGet(System::currentTimeMillis); - // Calcuclate average ping for each session with a Ping submap + // Calculate average ping for each session with a Ping submap SortedMap duringSession = pingOfServer.subMap(start, end); for (Ping ping : duringSession.values()) { pingCount += ping.getAverage(); @@ -143,7 +143,7 @@ public class PingMutator { public double average() { return pings.stream().mapToDouble(Ping::getAverage) - .filter(value -> value > 0 && value <= 4000) + .filter(Predicates::pingInRange) .average().orElse(-1); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayerKillMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayerKillMutator.java index 37c244c6f..df76a9d41 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayerKillMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayerKillMutator.java @@ -18,11 +18,11 @@ package com.djrapitops.plan.delivery.domain.mutators; import com.djrapitops.plan.delivery.formatting.Formatters; import com.djrapitops.plan.gathering.domain.PlayerKill; +import com.djrapitops.plan.utilities.java.Lists; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; /** * Mutator functions for {@link PlayerKill} objects. @@ -38,11 +38,11 @@ public class PlayerKillMutator { } public PlayerKillMutator filterNonSelfKills() { - return new PlayerKillMutator(kills.stream().filter(PlayerKill::isNotSelfKill).collect(Collectors.toList())); + return new PlayerKillMutator(Lists.filter(kills, PlayerKill::isNotSelfKill)); } public List> toJSONAsMap(Formatters formatters) { - return kills.stream().map( + return Lists.map(kills, kill -> { Map killMap = new HashMap<>(); killMap.put("date", formatters.secondLong().apply(kill.getDate())); @@ -51,6 +51,6 @@ public class PlayerKillMutator { killMap.put("weapon", kill.getWeapon()); return killMap; } - ).collect(Collectors.toList()); + ); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayerVersusMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayerVersusMutator.java index 62b41680b..e5a6c2d22 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayerVersusMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayerVersusMutator.java @@ -21,6 +21,7 @@ import com.djrapitops.plan.delivery.domain.container.DataContainer; import com.djrapitops.plan.delivery.domain.keys.PlayerKeys; import com.djrapitops.plan.gathering.domain.PlayerKill; import com.djrapitops.plan.utilities.Predicates; +import com.djrapitops.plan.utilities.java.Lists; import java.util.Collections; import java.util.HashMap; @@ -54,8 +55,8 @@ public class PlayerVersusMutator { return new PlayerVersusMutator( sessionsMutator.filterSessionsBetween(after, before), - kills.stream().filter(killWithinDate).collect(Collectors.toList()), - deaths.stream().filter(killWithinDate).collect(Collectors.toList()) + Lists.filter(kills, killWithinDate), + Lists.filter(deaths, killWithinDate) ); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayersMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayersMutator.java index 4c60b5ff5..451c23ed1 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayersMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PlayersMutator.java @@ -25,9 +25,12 @@ import com.djrapitops.plan.delivery.domain.keys.SessionKeys; import com.djrapitops.plan.gathering.domain.GeoInfo; import com.djrapitops.plan.gathering.domain.Ping; import com.djrapitops.plan.gathering.domain.Session; +import com.djrapitops.plan.utilities.java.Lists; +import com.djrapitops.plan.utilities.java.Maps; import com.djrapitops.plugin.api.TimeAmount; import java.util.*; +import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -53,7 +56,7 @@ public class PlayersMutator { } public > PlayersMutator filterBy(T by) { - return new PlayersMutator(players.stream().filter(by).collect(Collectors.toList())); + return new PlayersMutator(Lists.filter(players, by)); } public PlayersMutator filterPlayedBetween(long after, long before) { @@ -129,11 +132,8 @@ public class PlayersMutator { } List pings = player.getValue(PlayerKeys.PING).orElse(new ArrayList<>()); String country = mostRecent.get().getGeolocation(); - List countryPings = pingPerCountry.getOrDefault(country, new ArrayList<>()); - pings.stream() - .filter(ping -> ping.getServerUUID().equals(serverUUID)) - .forEach(countryPings::add); - pingPerCountry.put(country, countryPings); + List countryPings = pingPerCountry.computeIfAbsent(country, Lists::create); + countryPings.addAll(new PingMutator(pings).filterByServer(serverUUID).all()); } return pingPerCountry; @@ -142,7 +142,7 @@ public class PlayersMutator { public TreeMap>> toActivityDataMap(long date, long msThreshold) { TreeMap>> activityData = new TreeMap<>(); for (long time = date; time >= date - TimeAmount.MONTH.toMillis(2L); time -= TimeAmount.WEEK.toMillis(1L)) { - Map> map = activityData.getOrDefault(time, new HashMap<>()); + Map> map = activityData.computeIfAbsent(time, Maps::create); if (!players.isEmpty()) { for (PlayerContainer player : players) { if (player.getValue(PlayerKeys.REGISTERED).orElse(0L) > time) { @@ -151,12 +151,10 @@ public class PlayersMutator { ActivityIndex activityIndex = player.getActivityIndex(time, msThreshold); String activityGroup = activityIndex.getGroup(); - Set uuids = map.getOrDefault(activityGroup, new HashSet<>()); + Set uuids = map.computeIfAbsent(activityGroup, Maps::createSet); uuids.add(player.getUnsafe(PlayerKeys.UUID)); - map.put(activityGroup, uuids); } } - activityData.put(time, map); } return activityData; } @@ -170,14 +168,12 @@ public class PlayersMutator { } public TreeMap newPerDay(TimeZone timeZone) { - List registerDates = registerDates().stream() - .map(value -> new DateObj<>(value, value)) - .collect(Collectors.toList()); + List> registerDates = Lists.map(registerDates(), value -> new DateObj<>(value, value)); // Adds timezone offset - SortedMap> byDay = new DateHoldersMutator<>(registerDates).groupByStartOfDay(timeZone); + SortedMap>> byDay = new DateHoldersMutator<>(registerDates).groupByStartOfDay(timeZone); TreeMap byDayCounts = new TreeMap<>(); - for (Map.Entry> entry : byDay.entrySet()) { + for (Map.Entry>> entry : byDay.entrySet()) { byDayCounts.put( entry.getKey(), entry.getValue().size() @@ -225,12 +221,9 @@ public class PlayersMutator { throw new IllegalStateException("No players to compare to after rejecting with dateLimit"); } - List retained = retainedAfterMonth.stream() - .map(player -> new RetentionData(player, onlineResolver, activityMsThreshold)) - .collect(Collectors.toList()); - List notRetained = notRetainedAfterMonth.stream() - .map(player -> new RetentionData(player, onlineResolver, activityMsThreshold)) - .collect(Collectors.toList()); + Function mapper = player -> new RetentionData(player, onlineResolver, activityMsThreshold); + List retained = Lists.map(retainedAfterMonth, mapper); + List notRetained = Lists.map(notRetainedAfterMonth, mapper); RetentionData avgRetained = RetentionData.average(retained); RetentionData avgNotRetained = RetentionData.average(notRetained); @@ -260,8 +253,7 @@ public class PlayersMutator { } public List operators() { - return players.stream() - .filter(player -> player.getValue(PlayerKeys.OPERATOR).orElse(false)).collect(Collectors.toList()); + return Lists.filter(players, player -> player.getValue(PlayerKeys.OPERATOR).orElse(false)); } public List pings() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/SessionsMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/SessionsMutator.java index 9b1b81b41..146b455d2 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/SessionsMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/SessionsMutator.java @@ -28,6 +28,7 @@ import com.djrapitops.plan.gathering.domain.Session; import com.djrapitops.plan.gathering.domain.WorldTimes; import com.djrapitops.plan.settings.config.WorldAliasSettings; import com.djrapitops.plan.utilities.analysis.Median; +import com.djrapitops.plan.utilities.java.Lists; import java.util.*; import java.util.function.Function; @@ -62,10 +63,15 @@ public class SessionsMutator { return this; } - public SessionsMutator filterBy(Predicate predicate) { - return new SessionsMutator(sessions.stream() - .filter(predicate) - .collect(Collectors.toList())); + public static Map> sortByPlayers(List sessions) { + Map> sorted = new HashMap<>(); + for (Session session : sessions) { + UUID playerUUID = session.getUnsafe(SessionKeys.UUID); + List playerSessions = sorted.computeIfAbsent(playerUUID, Lists::create); + playerSessions.add(session); + sorted.put(playerUUID, playerSessions); + } + return sorted; } public SessionsMutator filterSessionsBetween(long after, long before) { @@ -157,9 +163,14 @@ public class SessionsMutator { return 0L; } - public long toMedianSessionLength() { - List sessionLengths = sessions.stream().map(Session::getLength).collect(Collectors.toList()); - return (long) Median.forList(sessionLengths).calculate(); + public static Map> sortByServers(List sessions) { + Map> sorted = new HashMap<>(); + for (Session session : sessions) { + UUID serverUUID = session.getUnsafe(SessionKeys.SERVER_UUID); + List serverSessions = sorted.computeIfAbsent(serverUUID, Lists::create); + serverSessions.add(session); + } + return sorted; } public int toUniquePlayers() { @@ -189,26 +200,13 @@ public class SessionsMutator { }; } - public static Map> sortByPlayers(List sessions) { - Map> sorted = new HashMap<>(); - for (Session session : sessions) { - UUID playerUUID = session.getUnsafe(SessionKeys.UUID); - List playerSessions = sorted.getOrDefault(playerUUID, new ArrayList<>()); - playerSessions.add(session); - sorted.put(playerUUID, playerSessions); - } - return sorted; + public SessionsMutator filterBy(Predicate predicate) { + return new SessionsMutator(Lists.filter(sessions, predicate)); } - public static Map> sortByServers(List sessions) { - Map> sorted = new HashMap<>(); - for (Session session : sessions) { - UUID serverUUID = session.getUnsafe(SessionKeys.SERVER_UUID); - List serverSessions = sorted.getOrDefault(serverUUID, new ArrayList<>()); - serverSessions.add(session); - sorted.put(serverUUID, serverSessions); - } - return sorted; + public long toMedianSessionLength() { + List sessionLengths = Lists.map(sessions, Session::getLength); + return (long) Median.forList(sessionLengths).calculate(); } public static Map> sortByServersToMaps(List sessions) { @@ -264,7 +262,7 @@ public class SessionsMutator { Formatters formatters, Function, Object> nameFunction ) { - return sessions.stream().map(session -> { + return Lists.map(sessions, session -> { Map sessionMap = new HashMap<>(); sessionMap.put("player_name", session.getValue(SessionKeys.NAME).orElse(session.getUnsafe(SessionKeys.UUID).toString())); sessionMap.put("player_uuid", session.getUnsafe(SessionKeys.UUID).toString()); @@ -298,6 +296,6 @@ public class SessionsMutator { sessionMap.put("avg_ping", formatters.decimals().apply(averagePing) + " ms") ); return sessionMap; - }).collect(Collectors.toList()); + }); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/TPSMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/TPSMutator.java index 18b8ac04f..b2233c500 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/TPSMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/TPSMutator.java @@ -21,6 +21,7 @@ import com.djrapitops.plan.delivery.domain.keys.ServerKeys; import com.djrapitops.plan.delivery.rendering.json.graphs.line.Point; import com.djrapitops.plan.gathering.domain.TPS; import com.djrapitops.plan.utilities.comparators.TPSComparator; +import com.djrapitops.plan.utilities.java.Lists; import java.util.ArrayList; import java.util.List; @@ -53,9 +54,7 @@ public class TPSMutator { } public TPSMutator filterBy(Predicate filter) { - return new TPSMutator(tpsData.stream() - .filter(filter) - .collect(Collectors.toList())); + return new TPSMutator(Lists.filter(tpsData, filter)); } public TPSMutator filterDataBetween(long after, long before) { @@ -71,15 +70,11 @@ public class TPSMutator { } public List playersOnlinePoints() { - return tpsData.stream() - .map(tps -> new Point(tps.getDate(), tps.getPlayers())) - .collect(Collectors.toList()); + return Lists.map(tpsData, tps -> new Point(tps.getDate(), tps.getPlayers())); } public List tpsPoints() { - return tpsData.stream() - .map(tps -> new Point(tps.getDate(), tps.getTicksPerSecond())) - .collect(Collectors.toList()); + return Lists.map(tpsData, tps -> new Point(tps.getDate(), tps.getTicksPerSecond())); } public List cpuPoints() { @@ -90,21 +85,15 @@ public class TPSMutator { } public List ramUsagePoints() { - return tpsData.stream() - .map(tps -> new Point(tps.getDate(), tps.getUsedMemory())) - .collect(Collectors.toList()); + return Lists.map(tpsData, tps -> new Point(tps.getDate(), tps.getUsedMemory())); } public List entityPoints() { - return tpsData.stream() - .map(tps -> new Point(tps.getDate(), tps.getEntityCount())) - .collect(Collectors.toList()); + return Lists.map(tpsData, tps -> new Point(tps.getDate(), tps.getEntityCount())); } public List chunkPoints() { - return tpsData.stream() - .map(tps -> new Point(tps.getDate(), tps.getChunksLoaded())) - .collect(Collectors.toList()); + return Lists.map(tpsData, tps -> new Point(tps.getDate(), tps.getChunksLoaded())); } public List freeDiskPoints() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PlayerJSONCreator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PlayerJSONCreator.java index 2cf6f37aa..542ab362b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PlayerJSONCreator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PlayerJSONCreator.java @@ -40,13 +40,13 @@ import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.database.queries.containers.PlayerContainerQuery; import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator; +import com.djrapitops.plan.utilities.java.Lists; import org.apache.commons.text.StringEscapeUtils; import javax.inject.Inject; import javax.inject.Singleton; import java.util.*; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; @Singleton public class PlayerJSONCreator { @@ -304,9 +304,7 @@ public class PlayerJSONCreator { } public static List fromGeoInfo(List geoInfo, Formatter dateFormatter) { - return geoInfo.stream() - .map(i -> new ConnectionInfo(i.getGeolocation(), dateFormatter.apply(i.getDate()))) - .collect(Collectors.toList()); + return Lists.map(geoInfo, i -> new ConnectionInfo(i.getGeolocation(), dateFormatter.apply(i.getDate()))); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java index 0d1e83a85..35a43b58a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java @@ -41,13 +41,13 @@ import com.djrapitops.plan.storage.database.queries.analysis.ActivityIndexQuerie import com.djrapitops.plan.storage.database.queries.analysis.NetworkActivityIndexQueries; import com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries; import com.djrapitops.plan.storage.database.queries.objects.*; +import com.djrapitops.plan.utilities.java.Lists; import com.djrapitops.plugin.api.TimeAmount; import javax.inject.Inject; import javax.inject.Singleton; import java.util.*; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; /** * Perses Graph related Data JSON. @@ -98,9 +98,9 @@ public class GraphJSONCreator { long now = System.currentTimeMillis(); long halfYearAgo = now - TimeUnit.DAYS.toMillis(180L); - List points = db.query(TPSQueries.fetchPlayersOnlineOfServer(halfYearAgo, now, serverUUID)).stream() - .map(point -> new Point(point.getDate(), point.getValue())) - .collect(Collectors.toList()); + List points = Lists.map(db.query(TPSQueries.fetchPlayersOnlineOfServer(halfYearAgo, now, serverUUID)), + point -> new Point(point.getDate(), point.getValue()) + ); return "{\"playersOnline\":" + graphs.line().lineGraph(points).toHighChartsSeries() + '}'; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/calendar/PlayerCalendar.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/calendar/PlayerCalendar.java index 858fee51f..e67d26178 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/calendar/PlayerCalendar.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/calendar/PlayerCalendar.java @@ -26,6 +26,7 @@ import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.locale.lang.HtmlLang; import com.djrapitops.plan.settings.theme.Theme; import com.djrapitops.plan.settings.theme.ThemeVal; +import com.djrapitops.plan.utilities.java.Lists; import java.util.*; import java.util.concurrent.TimeUnit; @@ -124,9 +125,8 @@ public class PlayerCalendar { for (Session session : allSessions) { String day = iso8601Formatter.apply(session.getDate()); - List sessionsOfDay = sessionsByDay.getOrDefault(day, new ArrayList<>()); + List sessionsOfDay = sessionsByDay.computeIfAbsent(day, Lists::create); sessionsOfDay.add(session); - sessionsByDay.put(day, sessionsOfDay); } return sessionsByDay; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ServerPluginTabs.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ServerPluginTabs.java index ad5936f5d..ef6b55119 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ServerPluginTabs.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ServerPluginTabs.java @@ -25,10 +25,10 @@ import com.djrapitops.plan.extension.ElementOrder; import com.djrapitops.plan.extension.FormatType; import com.djrapitops.plan.extension.implementation.TabInformation; import com.djrapitops.plan.extension.implementation.results.*; +import com.djrapitops.plan.utilities.java.Lists; import com.djrapitops.plugin.utilities.Format; import java.util.*; -import java.util.stream.Collectors; /** * Responsible for generating /server page plugin tabs based on DataExtension API data. @@ -61,9 +61,7 @@ public class ServerPluginTabs { Formatters formatters ) { this.serverData = serverData; - this.extraTabServerData = serverData.stream() - .filter(ExtensionData::doesNeedWiderSpace) - .collect(Collectors.toList()); + this.extraTabServerData = Lists.filter(serverData, ExtensionData::doesNeedWiderSpace); this.serverData.removeAll(extraTabServerData); numberFormatters = new EnumMap<>(FormatType.class); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/pages/RawDataResponse.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/pages/RawDataResponse.java index b91c99dfd..a23794722 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/pages/RawDataResponse.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/response/pages/RawDataResponse.java @@ -18,11 +18,11 @@ package com.djrapitops.plan.delivery.webserver.response.pages; import com.djrapitops.plan.delivery.domain.container.DataContainer; import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse; +import com.djrapitops.plan.utilities.java.Lists; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; /** * Response for sending raw data as JSON when it is inside a DataContainer. @@ -46,10 +46,10 @@ public class RawDataResponse extends JSONResponse { value = mapToNormalMap((DataContainer) value); } if (value instanceof Map) { - value = handleMap((Map) value); + value = handleMap((Map) value); } if (value instanceof List) { - value = handleList((List) value); + value = handleList((List) value); } values.put(key.getKeyName(), value); } @@ -57,14 +57,14 @@ public class RawDataResponse extends JSONResponse { return values; } - private static List handleList(List list) { + private static List handleList(List list) { if (list.stream().findAny().orElse(null) instanceof DataContainer) { - return (List) list.stream().map(obj -> mapToNormalMap((DataContainer) obj)).collect(Collectors.toList()); + return Lists.map(list, obj -> mapToNormalMap((DataContainer) obj)); } return list; } - private static Map handleMap(Map map) { + private static Map handleMap(Map map) { if (map.values().stream().findAny().orElse(null) instanceof DataContainer) { Map newMap = new HashMap<>(); map.forEach((key, value) -> newMap.put(key, mapToNormalMap((DataContainer) value))); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/CallerImplementation.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/CallerImplementation.java index 5b376b4c7..52fa9eca8 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/CallerImplementation.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/CallerImplementation.java @@ -32,26 +32,26 @@ import java.util.UUID; public class CallerImplementation implements Caller { private final ProviderValueGatherer gatherer; - private final ExtensionServiceImplementation extensionServiceImplementation; + private final ExtensionServiceImplementation extensionService; private final Processing processing; public CallerImplementation( ProviderValueGatherer gatherer, - ExtensionServiceImplementation extensionServiceImplementation, + ExtensionServiceImplementation extensionService, Processing processing ) { this.gatherer = gatherer; - this.extensionServiceImplementation = extensionServiceImplementation; + this.extensionService = extensionService; this.processing = processing; } @Override public void updatePlayerData(UUID playerUUID, String playerName) { - processing.submitNonCritical(() -> extensionServiceImplementation.updatePlayerValues(gatherer, playerUUID, playerName, CallEvents.MANUAL)); + processing.submitNonCritical(() -> extensionService.updatePlayerValues(gatherer, playerUUID, playerName, CallEvents.MANUAL)); } @Override public void updateServerData() { - processing.submitNonCritical(() -> extensionServiceImplementation.updateServerValues(gatherer, CallEvents.MANUAL)); + processing.submitNonCritical(() -> extensionService.updateServerValues(gatherer, CallEvents.MANUAL)); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/DataProviderExtractor.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/DataProviderExtractor.java index 0e47cc202..5f2f64b3e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/DataProviderExtractor.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/DataProviderExtractor.java @@ -23,6 +23,7 @@ import com.djrapitops.plan.extension.extractor.MethodAnnotations; import com.djrapitops.plan.extension.icon.Color; import com.djrapitops.plan.extension.icon.Icon; import com.djrapitops.plan.extension.implementation.providers.*; +import com.djrapitops.plan.utilities.java.Lists; import java.lang.annotation.Annotation; import java.lang.reflect.Method; @@ -42,8 +43,8 @@ import java.util.stream.Collectors; */ public class DataProviderExtractor { - private final ExtensionExtractor extensionExtractor; - private final DataProviders dataProviders; + private final ExtensionExtractor extractor; + private final DataProviders providers; /** * Create a DataProviderExtractor. @@ -52,31 +53,31 @@ public class DataProviderExtractor { * @throws IllegalArgumentException If something is badly wrong with the specified extension class annotations. */ public DataProviderExtractor(DataExtension extension) { - extensionExtractor = new ExtensionExtractor(extension); + extractor = new ExtensionExtractor(extension); - extensionExtractor.extractAnnotationInformation(); + extractor.extractAnnotationInformation(); - dataProviders = new DataProviders(); - extractAllDataProviders(); + providers = new DataProviders(); + extractProviders(); } public String getPluginName() { - return extensionExtractor.getPluginInfo().name(); + return extractor.getPluginInfo().name(); } public Icon getPluginIcon() { - PluginInfo pluginInfo = extensionExtractor.getPluginInfo(); + PluginInfo pluginInfo = extractor.getPluginInfo(); return new Icon(pluginInfo.iconFamily(), pluginInfo.iconName(), pluginInfo.color()); } public Collection getPluginTabs() { - Map tabInformation = extensionExtractor.getTabInformation() + Map tabInformation = extractor.getTabInformation() .stream().collect(Collectors.toMap(TabInfo::tab, Function.identity(), (one, two) -> one)); Map order = getTabOrder().map(this::orderToMap).orElse(new HashMap<>()); // Extracts PluginTabs - return extensionExtractor.getMethodAnnotations().getAnnotations(Tab.class).stream() + return extractor.getMethodAnnotations().getAnnotations(Tab.class).stream() .map(Tab::value) .distinct() .map(tabName -> { @@ -99,44 +100,42 @@ public class DataProviderExtractor { } public Optional getTabOrder() { - return extensionExtractor.getTabOrder().map(TabOrder::value); + return extractor.getTabOrder().map(TabOrder::value); } public Collection getInvalidatedMethods() { - return extensionExtractor.getInvalidateMethodAnnotations().stream() - .map(InvalidateMethod::value) - .collect(Collectors.toSet()); + return Lists.mapUnique(extractor.getInvalidateMethodAnnotations(), InvalidateMethod::value); } - public DataProviders getDataProviders() { - return dataProviders; + public DataProviders getProviders() { + return providers; } - private void extractAllDataProviders() { - PluginInfo pluginInfo = extensionExtractor.getPluginInfo(); + private void extractProviders() { + PluginInfo pluginInfo = extractor.getPluginInfo(); - MethodAnnotations methodAnnotations = extensionExtractor.getMethodAnnotations(); + MethodAnnotations methodAnnotations = extractor.getMethodAnnotations(); Map tabs = methodAnnotations.getMethodAnnotations(Tab.class); Map conditions = methodAnnotations.getMethodAnnotations(Conditional.class); - extractDataProviders(pluginInfo, tabs, conditions, BooleanProvider.class, BooleanDataProvider::placeToDataProviders); - extractDataProviders(pluginInfo, tabs, conditions, DoubleProvider.class, DoubleDataProvider::placeToDataProviders); - extractDataProviders(pluginInfo, tabs, conditions, PercentageProvider.class, PercentageDataProvider::placeToDataProviders); - extractDataProviders(pluginInfo, tabs, conditions, NumberProvider.class, NumberDataProvider::placeToDataProviders); - extractDataProviders(pluginInfo, tabs, conditions, StringProvider.class, StringDataProvider::placeToDataProviders); - extractDataProviders(pluginInfo, tabs, conditions, TableProvider.class, TableDataProvider::placeToDataProviders); - extractDataProviders(pluginInfo, tabs, conditions, GroupProvider.class, GroupDataProvider::placeToDataProviders); + extractProviders(pluginInfo, tabs, conditions, BooleanProvider.class, BooleanDataProvider::placeToDataProviders); + extractProviders(pluginInfo, tabs, conditions, DoubleProvider.class, DoubleDataProvider::placeToDataProviders); + extractProviders(pluginInfo, tabs, conditions, PercentageProvider.class, PercentageDataProvider::placeToDataProviders); + extractProviders(pluginInfo, tabs, conditions, NumberProvider.class, NumberDataProvider::placeToDataProviders); + extractProviders(pluginInfo, tabs, conditions, StringProvider.class, StringDataProvider::placeToDataProviders); + extractProviders(pluginInfo, tabs, conditions, TableProvider.class, TableDataProvider::placeToDataProviders); + extractProviders(pluginInfo, tabs, conditions, GroupProvider.class, GroupDataProvider::placeToDataProviders); } - private void extractDataProviders(PluginInfo pluginInfo, Map tabs, Map conditions, Class ofKind, DataProviderFactory factory) { - for (Map.Entry entry : extensionExtractor.getMethodAnnotations().getMethodAnnotations(ofKind).entrySet()) { + private void extractProviders(PluginInfo pluginInfo, Map tabs, Map conditions, Class ofKind, DataProviderFactory factory) { + for (Map.Entry entry : extractor.getMethodAnnotations().getMethodAnnotations(ofKind).entrySet()) { Method method = entry.getKey(); T annotation = entry.getValue(); Optional conditional = Optional.ofNullable(conditions.get(method)); Optional tab = Optional.ofNullable(tabs.get(method)); factory.placeToDataProviders( - dataProviders, method, annotation, + providers, method, annotation, conditional.orElse(null), tab.map(Tab::value).orElse(null), pluginInfo.name() @@ -145,7 +144,7 @@ public class DataProviderExtractor { } public Collection getWarnings() { - return extensionExtractor.getWarnings(); + return extractor.getWarnings(); } /** diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProvider.java index 8e3bf1b62..0c9216adf 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProvider.java @@ -18,6 +18,8 @@ package com.djrapitops.plan.extension.implementation.providers; import com.djrapitops.plan.extension.implementation.ProviderInformation; +import java.util.Objects; + /** * Abstract representation of all values a Provider annotation provides. * @@ -40,4 +42,18 @@ public abstract class DataProvider { public ProviderInformation getProviderInformation() { return providerInformation; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof DataProvider)) return false; + DataProvider that = (DataProvider) o; + return Objects.equals(providerInformation, that.providerInformation) && + Objects.equals(method, that.method); + } + + @Override + public int hashCode() { + return Objects.hash(providerInformation, method); + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProviders.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProviders.java index aa51019c8..8848c66ac 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProviders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProviders.java @@ -17,9 +17,10 @@ package com.djrapitops.plan.extension.implementation.providers; import com.djrapitops.plan.extension.implementation.MethodType; +import com.djrapitops.plan.utilities.java.Lists; +import com.djrapitops.plan.utilities.java.Maps; import java.util.*; -import java.util.stream.Collectors; /** * Group class for handling multiple different types of {@link DataProvider}s. @@ -28,74 +29,73 @@ import java.util.stream.Collectors; */ public class DataProviders { - private final Map>> byMethodType; + private final Map, List>>> byMethodType; public DataProviders() { byMethodType = new EnumMap<>(MethodType.class); } - public void put(DataProvider provider) { - MethodWrapper method = provider.getMethod(); - + public void put(DataProvider provider) { + MethodWrapper method = provider.getMethod(); MethodType methodType = method.getMethodType(); - Class resultType = method.getResultType(); + Class returnType = method.getReturnType(); - Map> byParameterType = byMethodType.getOrDefault(methodType, new HashMap<>()); - List dataProviders = byParameterType.getOrDefault(resultType, new ArrayList<>()); + computeIfAbsent(methodType, returnType).add(provider); + } - dataProviders.add(provider); + private List> computeIfAbsent(MethodType methodType, Class returnType) { + return byMethodType.computeIfAbsent(methodType, Maps::create).computeIfAbsent(returnType, Lists::create); + } - byParameterType.put(resultType, dataProviders); - byMethodType.put(methodType, byParameterType); + private List> getProvidersByTypes(MethodType methodType, Class returnType) { + Map, List>> byReturnType = byMethodType.getOrDefault(methodType, Collections.emptyMap()); + return byReturnType.getOrDefault(returnType, Collections.emptyList()); } public List> getPlayerMethodsByType(Class returnType) { - Map> providersAcceptingUUID = byMethodType.getOrDefault(MethodType.PLAYER_UUID, new HashMap<>()); - Map> providersAcceptingName = byMethodType.getOrDefault(MethodType.PLAYER_NAME, new HashMap<>()); - List> byReturnType = new ArrayList<>(); - for (DataProvider dataProvider : providersAcceptingUUID.getOrDefault(returnType, Collections.emptyList())) { + for (DataProvider dataProvider : getProvidersByTypes(MethodType.PLAYER_UUID, returnType)) { byReturnType.add((DataProvider) dataProvider); } - for (DataProvider dataProvider : providersAcceptingName.getOrDefault(returnType, Collections.emptyList())) { + for (DataProvider dataProvider : getProvidersByTypes(MethodType.PLAYER_NAME, returnType)) { byReturnType.add((DataProvider) dataProvider); } return byReturnType; } public List> getServerMethodsByType(Class returnType) { - List> byReturnType = new ArrayList<>(); - for (DataProvider dataProvider : byMethodType.getOrDefault(MethodType.SERVER, new HashMap<>()).getOrDefault(returnType, Collections.emptyList())) { - byReturnType.add((DataProvider) dataProvider); + List> providers = new ArrayList<>(); + for (DataProvider dataProvider : getProvidersByTypes(MethodType.SERVER, returnType)) { + providers.add((DataProvider) dataProvider); } - return byReturnType; + return providers; } public List> getGroupMethodsByType(Class returnType) { List> byReturnType = new ArrayList<>(); - for (DataProvider dataProvider : byMethodType.getOrDefault(MethodType.GROUP, new HashMap<>()).getOrDefault(returnType, Collections.emptyList())) { + for (DataProvider dataProvider : getProvidersByTypes(MethodType.GROUP, returnType)) { byReturnType.add((DataProvider) dataProvider); } return byReturnType; } - public void removeProviderWithMethod(MethodWrapper toRemove) { + public void removeProviderWithMethod(MethodWrapper toRemove) { MethodType methodType = toRemove.getMethodType(); - Map> byResultType = byMethodType.getOrDefault(methodType, Collections.emptyMap()); + Map, List>> byResultType = byMethodType.getOrDefault(methodType, Collections.emptyMap()); if (byResultType.isEmpty()) { return; } - Class resultType = toRemove.getResultType(); - List providers = byResultType.getOrDefault(resultType, Collections.emptyList()); - if (providers.isEmpty()) { - return; - } + Class returnType = toRemove.getReturnType(); + List> providers = getProvidersByTypes(methodType, returnType); - byResultType.put(resultType, providers.stream() - .filter(provider -> !provider.getMethod().equals(toRemove)) - .collect(Collectors.toList()) - ); - byMethodType.put(methodType, byResultType); + DataProvider providerToRemove = null; + for (DataProvider provider : providers) { + if (provider.getMethod().equals(toRemove)) { + providerToRemove = provider; + break; + } + } + providers.remove(providerToRemove); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/MethodWrapper.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/MethodWrapper.java index 2523bcab6..0339ccc4a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/MethodWrapper.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/MethodWrapper.java @@ -34,18 +34,18 @@ import java.util.UUID; public class MethodWrapper { private final Method method; - private final Class resultType; + private final Class returnType; private final MethodType methodType; - public MethodWrapper(Method method, Class resultType) { + public MethodWrapper(Method method, Class returnType) { this.method = method; - this.resultType = resultType; + this.returnType = returnType; methodType = MethodType.forMethod(this.method); } public T callMethod(DataExtension extension, UUID playerUUID, String playerName) { if (methodType != MethodType.PLAYER_NAME && methodType != MethodType.PLAYER_UUID) { - throw new IllegalStateException(method.getDeclaringClass() + " method " + method.getName() + " is not GROUP method."); + throw new IllegalStateException(method.getDeclaringClass() + " method " + method.getName() + " is not PLAYER method."); } return callMethod(extension, playerUUID, playerName, null); } @@ -68,13 +68,13 @@ public class MethodWrapper { try { switch (methodType) { case SERVER: - return resultType.cast(method.invoke(extension)); + return returnType.cast(method.invoke(extension)); case PLAYER_UUID: - return resultType.cast(method.invoke(extension, playerUUID)); + return returnType.cast(method.invoke(extension, playerUUID)); case PLAYER_NAME: - return resultType.cast(method.invoke(extension, playerName)); + return returnType.cast(method.invoke(extension, playerName)); case GROUP: - return resultType.cast(method.invoke(extension, group)); + return returnType.cast(method.invoke(extension, group)); default: throw new IllegalArgumentException(method.getDeclaringClass() + " method " + method.getName() + " had invalid parameters."); } @@ -97,8 +97,8 @@ public class MethodWrapper { return methodType; } - public Class getResultType() { - return resultType; + public Class getReturnType() { + return returnType; } @Override @@ -107,12 +107,12 @@ public class MethodWrapper { if (!(o instanceof MethodWrapper)) return false; MethodWrapper that = (MethodWrapper) o; return method.equals(that.method) && - resultType.equals(that.resultType) && + returnType.equals(that.returnType) && methodType == that.methodType; } @Override public int hashCode() { - return Objects.hash(method, resultType, methodType); + return Objects.hash(method, returnType, methodType); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/ProviderValueGatherer.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/ProviderValueGatherer.java index e4faea5a6..f3435f2a9 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/ProviderValueGatherer.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/ProviderValueGatherer.java @@ -68,7 +68,7 @@ public class ProviderValueGatherer { String pluginName = extractor.getPluginName(); UUID serverUUID = serverInfo.getServerUUID(); Database database = dbSystem.getDatabase(); - dataProviders = extractor.getDataProviders(); + dataProviders = extractor.getProviders(); booleanGatherer = new BooleanProviderValueGatherer( pluginName, extension, serverUUID, database, dataProviders ); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionTabData.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionTabData.java index 2b7020b38..ef49a8665 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionTabData.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionTabData.java @@ -17,6 +17,7 @@ package com.djrapitops.plan.extension.implementation.results; import com.djrapitops.plan.extension.implementation.TabInformation; +import com.djrapitops.plan.utilities.java.Lists; import java.util.*; import java.util.stream.Collectors; @@ -131,11 +132,11 @@ public class ExtensionTabData implements Comparable { } private void createOrderingList() { - booleanData.values().stream().map(DescribedExtensionData::getDescriptive).forEach(descriptives::add); - doubleData.values().stream().map(DescribedExtensionData::getDescriptive).forEach(descriptives::add); - percentageData.values().stream().map(DescribedExtensionData::getDescriptive).forEach(descriptives::add); - numberData.values().stream().map(DescribedExtensionData::getDescriptive).forEach(descriptives::add); - stringData.values().stream().map(DescribedExtensionData::getDescriptive).forEach(descriptives::add); + descriptives.addAll(Lists.map(booleanData.values(), DescribedExtensionData::getDescriptive)); + descriptives.addAll(Lists.map(doubleData.values(), DescribedExtensionData::getDescriptive)); + descriptives.addAll(Lists.map(percentageData.values(), DescribedExtensionData::getDescriptive)); + descriptives.addAll(Lists.map(numberData.values(), DescribedExtensionData::getDescriptive)); + descriptives.addAll(Lists.map(stringData.values(), DescribedExtensionData::getDescriptive)); order = descriptives.stream().sorted() .map(ExtensionDescriptive::getName) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionInformationQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionInformationQueries.java index 732d8af62..9293c976b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionInformationQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionInformationQueries.java @@ -25,6 +25,7 @@ import com.djrapitops.plan.storage.database.queries.QueryAllStatement; import com.djrapitops.plan.storage.database.queries.QueryStatement; import com.djrapitops.plan.storage.database.sql.tables.ExtensionIconTable; import com.djrapitops.plan.storage.database.sql.tables.ExtensionPluginTable; +import com.djrapitops.plan.utilities.java.Lists; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -103,9 +104,8 @@ public class ExtensionInformationQueries { Map> byServerUUID = new HashMap<>(); while (set.next()) { UUID serverUUID = UUID.fromString(set.getString(ExtensionPluginTable.SERVER_UUID)); - List information = byServerUUID.getOrDefault(serverUUID, new ArrayList<>()); + List information = byServerUUID.computeIfAbsent(serverUUID, Lists::create); information.add(extractExtensionInformationFromQuery(set)); - byServerUUID.put(serverUUID, information); } return byServerUUID; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerDataQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerDataQuery.java index a2fc75b7a..8da94c2e3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerDataQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerDataQuery.java @@ -30,6 +30,7 @@ import com.djrapitops.plan.storage.database.sql.tables.ExtensionIconTable; import com.djrapitops.plan.storage.database.sql.tables.ExtensionPlayerValueTable; import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable; import com.djrapitops.plan.storage.database.sql.tables.ExtensionTabTable; +import com.djrapitops.plan.utilities.java.Lists; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -100,9 +101,8 @@ public class ExtensionPlayerDataQuery implements Query list = extensionDataByServerUUID.getOrDefault(serverUUID, new ArrayList<>()); + List list = extensionDataByServerUUID.computeIfAbsent(serverUUID, Lists::create); list.add(data.setInformation(extensionInformation).build()); - extensionDataByServerUUID.put(serverUUID, list); } } return extensionDataByServerUUID; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTabData.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTabData.java index 318937f3f..a52c436c0 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTabData.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTabData.java @@ -19,6 +19,7 @@ package com.djrapitops.plan.extension.implementation.storage.queries; import com.djrapitops.plan.extension.implementation.TabInformation; import com.djrapitops.plan.extension.implementation.results.ExtensionData; import com.djrapitops.plan.extension.implementation.results.ExtensionTabData; +import com.djrapitops.plan.utilities.java.Maps; import com.djrapitops.plan.utilities.java.ThrowingSupplier; import java.util.HashMap; @@ -38,7 +39,7 @@ public class QueriedTabData { } public ExtensionTabData.Builder getTab(int pluginID, String tabName, ThrowingSupplier newDefault) throws K { - Map byTabName = byPluginID.getOrDefault(pluginID, new HashMap<>()); + Map byTabName = byPluginID.computeIfAbsent(pluginID, Maps::create); ExtensionTabData.Builder tab = byTabName.get(tabName); if (tab == null) { @@ -46,7 +47,6 @@ public class QueriedTabData { } byTabName.put(tabName, tab); - byPluginID.put(pluginID, byTabName); return tab; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTables.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTables.java index ddc9a628e..cd3d85722 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTables.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTables.java @@ -24,6 +24,7 @@ import com.djrapitops.plan.extension.implementation.results.ExtensionTabData; import com.djrapitops.plan.extension.implementation.results.ExtensionTableData; import com.djrapitops.plan.extension.table.Table; import com.djrapitops.plan.extension.table.TableAccessor; +import com.djrapitops.plan.utilities.java.Maps; import java.util.HashMap; import java.util.Map; @@ -48,9 +49,8 @@ public class QueriedTables { } public void put(int pluginID, int tableID, Table.Factory table) { - Map byTableID = byPluginID.getOrDefault(pluginID, new HashMap<>()); + Map byTableID = byPluginID.computeIfAbsent(pluginID, Maps::create); byTableID.put(tableID, table); - byPluginID.put(pluginID, byTableID); } public void addRow(int pluginID, int tableID, Object... row) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeFetchQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeFetchQueries.java index 305b5d1d0..fb43da99d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeFetchQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeFetchQueries.java @@ -21,6 +21,8 @@ import com.djrapitops.plan.gathering.domain.builders.TPSBuilder; import com.djrapitops.plan.storage.database.sql.tables.ServerTable; import com.djrapitops.plan.storage.database.sql.tables.TPSTable; import com.djrapitops.plan.storage.database.sql.tables.WorldTable; +import com.djrapitops.plan.utilities.java.Lists; +import com.djrapitops.plan.utilities.java.Maps; import java.sql.ResultSet; import java.sql.SQLException; @@ -67,7 +69,7 @@ public class LargeFetchQueries { while (set.next()) { UUID serverUUID = UUID.fromString(set.getString("s_uuid")); - List tpsList = serverMap.getOrDefault(serverUUID, new ArrayList<>()); + List tpsList = serverMap.computeIfAbsent(serverUUID, Lists::create); TPS tps = TPSBuilder.get() .date(set.getLong(TPSTable.DATE)) @@ -81,7 +83,6 @@ public class LargeFetchQueries { .toTPS(); tpsList.add(tps); - serverMap.put(serverUUID, tpsList); } return serverMap; } @@ -102,9 +103,8 @@ public class LargeFetchQueries { Map> worldMap = new HashMap<>(); while (set.next()) { UUID serverUUID = UUID.fromString(set.getString(WorldTable.SERVER_UUID)); - Collection worlds = worldMap.getOrDefault(serverUUID, new HashSet<>()); + Collection worlds = worldMap.computeIfAbsent(serverUUID, Maps::createSet); worlds.add(set.getString(WorldTable.NAME)); - worldMap.put(serverUUID, worlds); } return worldMap; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/containers/ServerPlayerContainersQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/containers/ServerPlayerContainersQuery.java index 51ab5c8ed..6828e45d4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/containers/ServerPlayerContainersQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/containers/ServerPlayerContainersQuery.java @@ -79,7 +79,7 @@ public class ServerPlayerContainersQuery implements Query> container.putRawData(PlayerKeys.KICK_COUNT, user.getTimesKicked()); // GeoInfo - container.putRawData(PlayerKeys.GEO_INFO, geoInformation.getOrDefault(uuid, new ArrayList<>())); + container.putRawData(PlayerKeys.GEO_INFO, geoInformation.getOrDefault(uuid, Collections.emptyList())); // Ping container.putRawData(PlayerKeys.PING, pingData.get(uuid)); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/GeoInfoQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/GeoInfoQueries.java index a6710c7c3..5701da692 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/GeoInfoQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/GeoInfoQueries.java @@ -22,6 +22,7 @@ import com.djrapitops.plan.storage.database.queries.QueryAllStatement; import com.djrapitops.plan.storage.database.queries.QueryStatement; import com.djrapitops.plan.storage.database.sql.tables.GeoInfoTable; import com.djrapitops.plan.storage.database.sql.tables.UserInfoTable; +import com.djrapitops.plan.utilities.java.Lists; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -66,13 +67,9 @@ public class GeoInfoQueries { while (set.next()) { UUID uuid = UUID.fromString(set.getString(GeoInfoTable.USER_UUID)); - List userGeoInfo = geoInformation.getOrDefault(uuid, new ArrayList<>()); - - String geolocation = set.getString(GeoInfoTable.GEOLOCATION); - long lastUsed = set.getLong(GeoInfoTable.LAST_USED); - userGeoInfo.add(new GeoInfo(geolocation, lastUsed)); - - geoInformation.put(uuid, userGeoInfo); + List userGeoInfo = geoInformation.computeIfAbsent(uuid, Lists::create); + GeoInfo geoInfo = new GeoInfo(set.getString(GeoInfoTable.GEOLOCATION), set.getLong(GeoInfoTable.LAST_USED)); + userGeoInfo.add(geoInfo); } return geoInformation; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/NicknameQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/NicknameQueries.java index 6d5351806..2d5068724 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/NicknameQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/NicknameQueries.java @@ -21,6 +21,8 @@ import com.djrapitops.plan.storage.database.queries.Query; import com.djrapitops.plan.storage.database.queries.QueryAllStatement; import com.djrapitops.plan.storage.database.queries.QueryStatement; import com.djrapitops.plan.storage.database.sql.tables.NicknamesTable; +import com.djrapitops.plan.utilities.java.Lists; +import com.djrapitops.plan.utilities.java.Maps; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -61,17 +63,14 @@ public class NicknameQueries { UUID serverUUID = UUID.fromString(set.getString(NicknamesTable.SERVER_UUID)); UUID uuid = UUID.fromString(set.getString(NicknamesTable.USER_UUID)); - Map> serverMap = map.getOrDefault(serverUUID, new HashMap<>()); - List nicknames = serverMap.getOrDefault(uuid, new ArrayList<>()); + Map> serverMap = map.computeIfAbsent(serverUUID, Maps::create); + List nicknames = serverMap.computeIfAbsent(uuid, Lists::create); nicknames.add(new Nickname( set.getString(NicknamesTable.NICKNAME), set.getLong(NicknamesTable.LAST_USED), serverUUID )); - - serverMap.put(uuid, nicknames); - map.put(serverUUID, serverMap); } return map; } @@ -168,15 +167,13 @@ public class NicknameQueries { UUID serverUUID = UUID.fromString(set.getString(NicknamesTable.SERVER_UUID)); UUID uuid = UUID.fromString(set.getString(NicknamesTable.USER_UUID)); - List nicknames = serverMap.getOrDefault(uuid, new ArrayList<>()); + List nicknames = serverMap.computeIfAbsent(uuid, Lists::create); nicknames.add(new Nickname( set.getString(NicknamesTable.NICKNAME), set.getLong(NicknamesTable.LAST_USED), serverUUID )); - - serverMap.put(uuid, nicknames); } return serverMap; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/PingQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/PingQueries.java index e7324399b..4a301fc60 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/PingQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/PingQueries.java @@ -23,6 +23,7 @@ import com.djrapitops.plan.storage.database.queries.QueryAllStatement; import com.djrapitops.plan.storage.database.queries.QueryStatement; import com.djrapitops.plan.storage.database.sql.tables.GeoInfoTable; import com.djrapitops.plan.storage.database.sql.tables.PingTable; +import com.djrapitops.plan.utilities.java.Lists; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -75,12 +76,11 @@ public class PingQueries { int minPing = set.getInt(PingTable.MIN_PING); int maxPing = set.getInt(PingTable.MAX_PING); - List pings = userPings.getOrDefault(uuid, new ArrayList<>()); + List pings = userPings.computeIfAbsent(uuid, Lists::create); pings.add(new Ping(date, serverUUID, minPing, maxPing, avgPing)); - userPings.put(uuid, pings); } return userPings; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/SessionQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/SessionQueries.java index fbd0428b5..40a488e40 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/SessionQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/SessionQueries.java @@ -29,6 +29,7 @@ import com.djrapitops.plan.storage.database.queries.QueryStatement; import com.djrapitops.plan.storage.database.sql.building.Sql; import com.djrapitops.plan.storage.database.sql.tables.*; import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator; +import com.djrapitops.plan.utilities.java.Maps; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -159,10 +160,10 @@ public class SessionQueries { while (set.next()) { UUID serverUUID = UUID.fromString(set.getString(SessionsTable.SERVER_UUID)); - Map> serverSessions = tempSessionMap.getOrDefault(serverUUID, new HashMap<>()); + Map> serverSessions = tempSessionMap.computeIfAbsent(serverUUID, Maps::create); UUID playerUUID = UUID.fromString(set.getString(SessionsTable.USER_UUID)); - SortedMap playerSessions = serverSessions.getOrDefault(playerUUID, new TreeMap<>(longRecentComparator)); + SortedMap playerSessions = serverSessions.computeIfAbsent(playerUUID, key -> new TreeMap<>(longRecentComparator)); long sessionStart = set.getLong(SessionsTable.SESSION_START); // id, uuid, serverUUID, sessionStart, sessionEnd, mobKills, deaths, afkTime @@ -206,8 +207,6 @@ public class SessionQueries { session.setAsFirstSessionIfMatches(set.getLong("registered")); playerSessions.put(sessionStart, session); - serverSessions.put(playerUUID, playerSessions); - tempSessionMap.put(serverUUID, serverSessions); } return tempSessionMap.values().stream() diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/TPSQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/TPSQueries.java index 8d8ed9897..27e57f8cc 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/TPSQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/TPSQueries.java @@ -23,6 +23,7 @@ import com.djrapitops.plan.storage.database.queries.Query; import com.djrapitops.plan.storage.database.queries.QueryStatement; import com.djrapitops.plan.storage.database.sql.building.Select; import com.djrapitops.plan.storage.database.sql.tables.ServerTable; +import com.djrapitops.plan.utilities.java.Lists; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -157,11 +158,8 @@ public class TPSQueries { Map> byServer = new HashMap<>(); while (set.next()) { UUID serverUUID = UUID.fromString(set.getString(ServerTable.SERVER_UUID)); - List ofServer = byServer.getOrDefault(serverUUID, new ArrayList<>()); - + List ofServer = byServer.computeIfAbsent(serverUUID, Lists::create); ofServer.add(extractTPS(set)); - - byServer.put(serverUUID, ofServer); } return byServer; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/UserInfoQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/UserInfoQueries.java index 25919890d..808e87331 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/UserInfoQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/UserInfoQueries.java @@ -21,6 +21,7 @@ import com.djrapitops.plan.storage.database.queries.Query; import com.djrapitops.plan.storage.database.queries.QueryAllStatement; import com.djrapitops.plan.storage.database.queries.QueryStatement; import com.djrapitops.plan.storage.database.sql.tables.UserInfoTable; +import com.djrapitops.plan.utilities.java.Lists; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -64,14 +65,13 @@ public class UserInfoQueries { UUID serverUUID = UUID.fromString(set.getString(UserInfoTable.SERVER_UUID)); UUID uuid = UUID.fromString(set.getString(UserInfoTable.USER_UUID)); - List userInfos = serverMap.getOrDefault(serverUUID, new ArrayList<>()); + List userInfos = serverMap.computeIfAbsent(serverUUID, Lists::create); long registered = set.getLong(UserInfoTable.REGISTERED); boolean banned = set.getBoolean(UserInfoTable.BANNED); boolean op = set.getBoolean(UserInfoTable.OP); userInfos.add(new UserInfo(uuid, serverUUID, registered, op, banned)); - serverMap.put(serverUUID, userInfos); } return serverMap; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/PingStoreTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/PingStoreTransaction.java index 88700a5f8..a6ef9c2d4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/PingStoreTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/PingStoreTransaction.java @@ -22,10 +22,10 @@ import com.djrapitops.plan.storage.database.queries.DataStoreQueries; import com.djrapitops.plan.storage.database.transactions.Transaction; import com.djrapitops.plan.utilities.Predicates; import com.djrapitops.plan.utilities.analysis.Median; +import com.djrapitops.plan.utilities.java.Lists; import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; /** * Transaction to store player's Ping value on a server. @@ -76,6 +76,7 @@ public class PingStoreTransaction extends Transaction { // VisibleForTesting int getMeanValue() { - return (int) Median.forList(pingList.stream().map(DateObj::getValue).collect(Collectors.toList())).calculate(); + List values = Lists.map(pingList, DateObj::getValue); + return (int) Median.forList(values).calculate(); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/NicknameLastSeenPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/NicknameLastSeenPatch.java index c29d27188..9cb0b6e40 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/NicknameLastSeenPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/NicknameLastSeenPatch.java @@ -22,11 +22,15 @@ import com.djrapitops.plan.storage.database.sql.building.Select; import com.djrapitops.plan.storage.database.sql.tables.NicknamesTable; import com.djrapitops.plan.storage.database.sql.tables.ServerTable; import com.djrapitops.plan.storage.database.transactions.ExecBatchStatement; +import com.djrapitops.plan.utilities.java.Maps; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; import static com.djrapitops.plan.storage.database.sql.building.Sql.AND; import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE; @@ -96,12 +100,11 @@ public class NicknameLastSeenPatch extends Patch { int serverID = set.getInt("server_id"); UUID serverUUID = serverUUIDsByID.get(serverID); Nickname nick = new Nickname(set.getString("additional_info"), date, serverUUID); - Set nicknames1 = map.getOrDefault(userID, new HashSet<>()); - if (serverUUID == null || nicknames1.contains(nick)) { + Set foundNicknames = map.computeIfAbsent(userID, Maps::createSet); + if (serverUUID == null || foundNicknames.contains(nick)) { continue; } - nicknames1.add(nick); - map.put(userID, nicknames1); + foundNicknames.add(nick); } return map; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/Predicates.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/Predicates.java index 668e2b64f..1f212b955 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/Predicates.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/Predicates.java @@ -38,8 +38,7 @@ public class Predicates { }; } - public static boolean pingInRange(int value) { - return value > 0 && value < 4000; + public static boolean pingInRange(double value) { + return value > 0 && value <= 4000; } - } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/Lists.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/Lists.java new file mode 100644 index 000000000..3d231fd46 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/Lists.java @@ -0,0 +1,88 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.utilities.java; + +import java.util.*; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * Methods that can be used as functional interfaces when dealing with Maps. + * + * @author Rsl1122 + */ +public class Lists { + + private Lists() { + // Static method class + } + + public static List create(K key) { + return new ArrayList<>(); + } + + /** + * Efficient replacement for List#stream().filter(keep).collect(Collectors.toList()). + * + * @param original Original list + * @param keep Condition for keeping on the list + * @param Type of the list objects + * @param Supertype for T if exists, T if not + * @return List with elements in original that keep returned true for. + */ + public static List filter(Collection original, Predicate keep) { + List filtered = new ArrayList<>(); + for (T value : original) { + if (keep.test(value)) filtered.add(value); + } + return filtered; + } + + /** + * Efficient replacement for List#stream().map(mapper).collect(Collectors.toList()). + * + * @param original Original list + * @param mapper Function to change object of type A to type B + * @param Type of the old list objects + * @param Type of the new list objects + * @return List with elements in original that keep returned true for. + */ + public static List map(Collection original, Function mapper) { + List mapped = new ArrayList<>(); + for (A element : original) { + mapped.add(mapper.apply(element)); + } + return mapped; + } + + /** + * Efficient replacement for List#stream().map(mapper).collect(Collectors.toSet()). + * + * @param original Original list + * @param mapper Function to change object of type A to type B + * @param Type of the old list objects + * @param Type of the new list objects + * @return List with elements in original that keep returned true for. + */ + public static Set mapUnique(Collection original, Function mapper) { + Set mapped = new HashSet<>(); + for (A element : original) { + mapped.add(mapper.apply(element)); + } + return mapped; + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/Maps.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/Maps.java new file mode 100644 index 000000000..5466ac059 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/Maps.java @@ -0,0 +1,43 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.utilities.java; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Methods that can be used as functional interfaces when dealing with Maps. + * + * @author Rsl1122 + */ +public class Maps { + + private Maps() { + // Static method class + } + + public static Map create(K key) { + return new HashMap<>(); + } + + public static Set createSet(K key) { + return new HashSet<>(); + } + +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/version/VersionCheckSystem.java b/Plan/common/src/main/java/com/djrapitops/plan/version/VersionCheckSystem.java index da603820f..d07397822 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/version/VersionCheckSystem.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/version/VersionCheckSystem.java @@ -21,6 +21,7 @@ import com.djrapitops.plan.settings.config.PlanConfig; import com.djrapitops.plan.settings.config.paths.PluginSettings; import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.locale.lang.PluginLang; +import com.djrapitops.plan.utilities.java.Lists; import com.djrapitops.plugin.api.utility.Version; import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.console.PluginLogger; @@ -31,7 +32,6 @@ import javax.inject.Singleton; import java.io.IOException; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; /** * System for checking if new Version is available when the System initializes. @@ -73,7 +73,7 @@ public class VersionCheckSystem implements SubSystem { try { List versions = VersionInfoLoader.load(); if (config.isFalse(PluginSettings.NOTIFY_ABOUT_DEV_RELEASES)) { - versions = versions.stream().filter(VersionInfo::isRelease).collect(Collectors.toList()); + versions = Lists.filter(versions, VersionInfo::isRelease); } VersionInfo newestVersion = versions.get(0); if (Version.isNewVersionAvailable(new Version(currentVersion), newestVersion.getVersion())) { diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/DatabaseTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/DatabaseTest.java index 48d8b60b1..843b003bd 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/DatabaseTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/DatabaseTest.java @@ -67,6 +67,7 @@ import com.djrapitops.plan.storage.database.transactions.patches.Patch; import com.djrapitops.plan.storage.database.transactions.patches.RegisterDateMinimizationPatch; import com.djrapitops.plan.storage.upkeep.DBCleanTask; import com.djrapitops.plan.utilities.comparators.DateHolderRecentComparator; +import com.djrapitops.plan.utilities.java.Lists; import com.djrapitops.plugin.logging.console.TestPluginLogger; import com.djrapitops.plugin.logging.error.ConsoleErrorLogger; import com.google.common.util.concurrent.MoreExecutors; @@ -188,7 +189,7 @@ public interface DatabaseTest { saveGeoInfo(playerUUID, expected); commitTest(); - List result = db().query(GeoInfoQueries.fetchAllGeoInformation()).getOrDefault(playerUUID, new ArrayList<>()); + List result = db().query(GeoInfoQueries.fetchAllGeoInformation()).get(playerUUID); assertEquals(Collections.singletonList(expected), result); } @@ -237,7 +238,7 @@ public interface DatabaseTest { commitTest(); - Collection result = db().query(LargeFetchQueries.fetchAllWorldNames()).getOrDefault(serverUUID(), new HashSet<>()); + Collection result = db().query(LargeFetchQueries.fetchAllWorldNames()).get(serverUUID()); assertEquals(new HashSet<>(Arrays.asList(expected)), result); } @@ -968,7 +969,7 @@ public interface DatabaseTest { tpsData.sort(Comparator.comparingInt(TPS::getPlayers)); int expected = tpsData.get(tpsData.size() - 1).getPlayers(); int actual = db().query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID())).map(DateObj::getValue).orElse(-1); - assertEquals(expected, actual, () -> "Wrong return value. " + tpsData.stream().map(TPS::getPlayers).collect(Collectors.toList()).toString()); + assertEquals(expected, actual, () -> "Wrong return value. " + Lists.map(tpsData, TPS::getPlayers).toString()); } @Test diff --git a/Plan/common/src/test/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java b/Plan/common/src/test/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java index 55768e802..df7d8a6ef 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/utilities/comparators/ComparatorTest.java @@ -26,6 +26,7 @@ import com.djrapitops.plan.settings.locale.Message; import com.djrapitops.plan.settings.locale.lang.CmdHelpLang; import com.djrapitops.plan.settings.locale.lang.Lang; import com.djrapitops.plan.utilities.PassEncryptUtil; +import com.djrapitops.plan.utilities.java.Lists; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; @@ -62,7 +63,7 @@ class ComparatorTest { Collections.reverse(expected); sessions.sort(new SessionStartComparator()); - List result = sessions.stream().map(s -> s.getUnsafe(SessionKeys.START)).collect(Collectors.toList()); + List result = Lists.map(sessions, s -> s.getUnsafe(SessionKeys.START)); assertEquals(expected, result); } @@ -89,7 +90,7 @@ class ComparatorTest { Collections.reverse(expected); webUsers.sort(new WebUserComparator()); - List result = webUsers.stream().map(WebUser::getPermLevel).collect(Collectors.toList()); + List result = Lists.map(webUsers, WebUser::getPermLevel); assertEquals(expected, result); } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/utilities/java/MapsSanityTest.java b/Plan/common/src/test/java/com/djrapitops/plan/utilities/java/MapsSanityTest.java new file mode 100644 index 000000000..66a275772 --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/utilities/java/MapsSanityTest.java @@ -0,0 +1,43 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.utilities.java; + +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests for Java Map shenanigans. + * + * @author Rsl1122 + */ +@RunWith(JUnitPlatform.class) +class MapsSanityTest { + + @Test + void mapComputeIfAbsentPutSideEffect() { + Map t = new HashMap<>(); + t.computeIfAbsent("Test", key -> "Confirmed"); + assertEquals("Confirmed", t.get("Test")); + } + +} \ No newline at end of file