From 3afc168feca7f75789da3b7016760046ee5b6c78 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 27 Aug 2019 11:36:11 +0300 Subject: [PATCH] Refactored some methods in Extension queries - Some Map management inside queries has now been delegated to QueriedTabData and QueriedTables to make the queries easier to read --- ...nData.java => DescribedExtensionData.java} | 2 +- .../results/ExtensionBooleanData.java | 2 +- .../results/ExtensionDoubleData.java | 2 +- .../results/ExtensionNumberData.java | 2 +- .../results/ExtensionStringData.java | 2 +- .../results/ExtensionTabData.java | 10 +- .../ExtensionAggregateBooleansQuery.java | 52 +++---- .../ExtensionAggregateDoublesQuery.java | 52 +++---- .../ExtensionAggregateNumbersQuery.java | 52 +++---- .../ExtensionAggregatePercentagesQuery.java | 52 +++---- .../queries/ExtensionPlayerDataQuery.java | 64 +++------ .../queries/ExtensionPlayerGroupsQuery.java | 65 +++------ .../queries/ExtensionPlayerTablesQuery.java | 136 +++++------------- .../queries/ExtensionServerDataQuery.java | 47 +++--- .../queries/ExtensionServerTablesQuery.java | 126 +++++----------- .../storage/queries/QueriedTabData.java | 89 ++++++++++++ .../storage/queries/QueriedTables.java | 96 +++++++++++++ .../plan/utilities/java/ThrowingSupplier.java | 30 ++++ 18 files changed, 448 insertions(+), 433 deletions(-) rename Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/{ExtensionData.java => DescribedExtensionData.java} (96%) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTabData.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTables.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/utilities/java/ThrowingSupplier.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionData.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/DescribedExtensionData.java similarity index 96% rename from Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionData.java rename to Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/DescribedExtensionData.java index 4b7634d1b..5dfdbc806 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionData.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/DescribedExtensionData.java @@ -21,7 +21,7 @@ package com.djrapitops.plan.extension.implementation.results; * * @author Rsl1122 */ -public interface ExtensionData { +public interface DescribedExtensionData { /** * Get Descriptive information about the data point. diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionBooleanData.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionBooleanData.java index ba0820410..4bcd50e83 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionBooleanData.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionBooleanData.java @@ -21,7 +21,7 @@ package com.djrapitops.plan.extension.implementation.results; * * @author Rsl1122 */ -public class ExtensionBooleanData implements ExtensionData { +public class ExtensionBooleanData implements DescribedExtensionData { private ExtensionDescriptive descriptive; private boolean value; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionDoubleData.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionDoubleData.java index 81e266b9b..970b4bd9f 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionDoubleData.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionDoubleData.java @@ -23,7 +23,7 @@ import com.djrapitops.plan.utilities.formatting.Formatter; * * @author Rsl1122 */ -public class ExtensionDoubleData implements ExtensionData { +public class ExtensionDoubleData implements DescribedExtensionData { private ExtensionDescriptive descriptive; private double value; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionNumberData.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionNumberData.java index a8e784325..72b3e137e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionNumberData.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionNumberData.java @@ -24,7 +24,7 @@ import com.djrapitops.plan.utilities.formatting.Formatter; * * @author Rsl1122 */ -public class ExtensionNumberData implements ExtensionData { +public class ExtensionNumberData implements DescribedExtensionData { private final ExtensionDescriptive descriptive; private final FormatType formatType; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionStringData.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionStringData.java index 71dbd6df9..190ad8a02 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionStringData.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionStringData.java @@ -24,7 +24,7 @@ import com.djrapitops.plan.utilities.html.Html; * * @author Rsl1122 */ -public class ExtensionStringData implements ExtensionData { +public class ExtensionStringData implements DescribedExtensionData { private final ExtensionDescriptive descriptive; private final boolean playerName; 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 b8a1b3de7..5a3e3c534 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 @@ -131,11 +131,11 @@ public class ExtensionTabData implements Comparable { } private void createOrderingList() { - booleanData.values().stream().map(ExtensionData::getDescriptive).forEach(descriptives::add); - doubleData.values().stream().map(ExtensionData::getDescriptive).forEach(descriptives::add); - percentageData.values().stream().map(ExtensionData::getDescriptive).forEach(descriptives::add); - numberData.values().stream().map(ExtensionData::getDescriptive).forEach(descriptives::add); - stringData.values().stream().map(ExtensionData::getDescriptive).forEach(descriptives::add); + 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); order = descriptives.stream().sorted() .map(ExtensionDescriptive::getName) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateBooleansQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateBooleansQuery.java index a801f77cf..8a6aa5a8c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateBooleansQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateBooleansQuery.java @@ -33,7 +33,6 @@ import com.djrapitops.plan.extension.implementation.results.server.ExtensionServ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -117,30 +116,40 @@ public class ExtensionAggregateBooleansQuery implements Query processResults(ResultSet set) throws SQLException { - Map> tabDataByPluginID = extractTabDataByPluginID(set); - return ExtensionServerDataQuery.flatMapToServerData(tabDataByPluginID); + return extractTabDataByPluginID(set).toServerDataByPluginID(); } }); } - private Map> extractTabDataByPluginID(ResultSet set) throws SQLException { - Map> tabDataByPluginID = new HashMap<>(); + private QueriedTabData extractTabDataByPluginID(ResultSet set) throws SQLException { + QueriedTabData tabData = new QueriedTabData(); while (set.next()) { int pluginID = set.getInt("plugin_id"); - Map tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>()); - String tabName = Optional.ofNullable(set.getString("tab_name")).orElse(""); - ExtensionTabData.Factory inMap = tabData.get(tabName); - ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData); + ExtensionTabData.Factory extensionTab = tabData.getTab(pluginID, tabName, () -> extractTabInformation(tabName, set)); ExtensionDescriptive extensionDescriptive = extractDescriptive(set); extractAndPutDataTo(extensionTab, extensionDescriptive, set); - - tabData.put(tabName, extensionTab); - tabDataByPluginID.put(pluginID, tabData); } - return tabDataByPluginID; + return tabData; + } + + private TabInformation extractTabInformation(String tabName, ResultSet set) throws SQLException { + Optional tabPriority = Optional.of(set.getInt("tab_priority")); + if (set.wasNull()) { + tabPriority = Optional.empty(); + } + Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); + + Icon tabIcon = extractTabIcon(set); + + return new TabInformation( + tabName, + tabIcon, + elementOrder.orElse(ElementOrder.values()), + tabPriority.orElse(100) + ); } private void extractAndPutDataTo(ExtensionTabData.Factory extensionTab, ExtensionDescriptive descriptive, ResultSet set) throws SQLException { @@ -169,23 +178,6 @@ public class ExtensionAggregateBooleansQuery implements Query tabData) throws SQLException { - Optional tabPriority = Optional.of(set.getInt("tab_priority")); - if (set.wasNull()) { - tabPriority = Optional.empty(); - } - Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); - - Icon tabIcon = extractTabIcon(set); - - return tabData.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation( - tabName, - tabIcon, - elementOrder.orElse(ElementOrder.values()), - tabPriority.orElse(100) - ))); - } - private Icon extractTabIcon(ResultSet set) throws SQLException { Optional iconName = Optional.ofNullable(set.getString("tab_icon_name")); if (iconName.isPresent()) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateDoublesQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateDoublesQuery.java index db5dcfdc9..8947a9bd5 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateDoublesQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateDoublesQuery.java @@ -33,7 +33,6 @@ import com.djrapitops.plan.extension.implementation.results.server.ExtensionServ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -116,30 +115,40 @@ public class ExtensionAggregateDoublesQuery implements Query processResults(ResultSet set) throws SQLException { - Map> tabDataByPluginID = extractTabDataByPluginID(set); - return ExtensionServerDataQuery.flatMapToServerData(tabDataByPluginID); + return extractTabDataByPluginID(set).toServerDataByPluginID(); } }); } - private Map> extractTabDataByPluginID(ResultSet set) throws SQLException { - Map> tabDataByPluginID = new HashMap<>(); + private QueriedTabData extractTabDataByPluginID(ResultSet set) throws SQLException { + QueriedTabData tabData = new QueriedTabData(); while (set.next()) { int pluginID = set.getInt("plugin_id"); - Map tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>()); - String tabName = Optional.ofNullable(set.getString("tab_name")).orElse(""); - ExtensionTabData.Factory inMap = tabData.get(tabName); - ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData); + ExtensionTabData.Factory extensionTab = tabData.getTab(pluginID, tabName, () -> extractTabInformation(tabName, set)); ExtensionDescriptive extensionDescriptive = extractDescriptive(set); extractAndPutDataTo(extensionTab, extensionDescriptive, set); - - tabData.put(tabName, extensionTab); - tabDataByPluginID.put(pluginID, tabData); } - return tabDataByPluginID; + return tabData; + } + + private TabInformation extractTabInformation(String tabName, ResultSet set) throws SQLException { + Optional tabPriority = Optional.of(set.getInt("tab_priority")); + if (set.wasNull()) { + tabPriority = Optional.empty(); + } + Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); + + Icon tabIcon = extractTabIcon(set); + + return new TabInformation( + tabName, + tabIcon, + elementOrder.orElse(ElementOrder.values()), + tabPriority.orElse(100) + ); } private void extractAndPutDataTo(ExtensionTabData.Factory extensionTab, ExtensionDescriptive descriptive, ResultSet set) throws SQLException { @@ -165,23 +174,6 @@ public class ExtensionAggregateDoublesQuery implements Query tabData) throws SQLException { - Optional tabPriority = Optional.of(set.getInt("tab_priority")); - if (set.wasNull()) { - tabPriority = Optional.empty(); - } - Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); - - Icon tabIcon = extractTabIcon(set); - - return tabData.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation( - tabName, - tabIcon, - elementOrder.orElse(ElementOrder.values()), - tabPriority.orElse(100) - ))); - } - private Icon extractTabIcon(ResultSet set) throws SQLException { Optional iconName = Optional.ofNullable(set.getString("tab_icon_name")); if (iconName.isPresent()) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateNumbersQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateNumbersQuery.java index 9ff611889..78a8831d6 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateNumbersQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregateNumbersQuery.java @@ -34,7 +34,6 @@ import com.djrapitops.plan.extension.implementation.results.server.ExtensionServ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -122,30 +121,40 @@ public class ExtensionAggregateNumbersQuery implements Query processResults(ResultSet set) throws SQLException { - Map> tabDataByPluginID = extractTabDataByPluginID(set); - return ExtensionServerDataQuery.flatMapToServerData(tabDataByPluginID); + return extractTabDataByPluginID(set).toServerDataByPluginID(); } }); } - private Map> extractTabDataByPluginID(ResultSet set) throws SQLException { - Map> tabDataByPluginID = new HashMap<>(); + private QueriedTabData extractTabDataByPluginID(ResultSet set) throws SQLException { + QueriedTabData tabData = new QueriedTabData(); while (set.next()) { int pluginID = set.getInt("plugin_id"); - Map tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>()); - String tabName = Optional.ofNullable(set.getString("tab_name")).orElse(""); - ExtensionTabData.Factory inMap = tabData.get(tabName); - ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData); + ExtensionTabData.Factory extensionTab = tabData.getTab(pluginID, tabName, () -> extractTabInformation(tabName, set)); ExtensionDescriptive extensionDescriptive = extractDescriptive(set); extractAndPutDataTo(extensionTab, extensionDescriptive, set); - - tabData.put(tabName, extensionTab); - tabDataByPluginID.put(pluginID, tabData); } - return tabDataByPluginID; + return tabData; + } + + private TabInformation extractTabInformation(String tabName, ResultSet set) throws SQLException { + Optional tabPriority = Optional.of(set.getInt("tab_priority")); + if (set.wasNull()) { + tabPriority = Optional.empty(); + } + Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); + + Icon tabIcon = extractTabIcon(set); + + return new TabInformation( + tabName, + tabIcon, + elementOrder.orElse(ElementOrder.values()), + tabPriority.orElse(100) + ); } private void extractAndPutDataTo(ExtensionTabData.Factory extensionTab, ExtensionDescriptive descriptive, ResultSet set) throws SQLException { @@ -172,23 +181,6 @@ public class ExtensionAggregateNumbersQuery implements Query tabData) throws SQLException { - Optional tabPriority = Optional.of(set.getInt("tab_priority")); - if (set.wasNull()) { - tabPriority = Optional.empty(); - } - Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); - - Icon tabIcon = extractTabIcon(set); - - return tabData.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation( - tabName, - tabIcon, - elementOrder.orElse(ElementOrder.values()), - tabPriority.orElse(100) - ))); - } - private Icon extractTabIcon(ResultSet set) throws SQLException { Optional iconName = Optional.ofNullable(set.getString("tab_icon_name")); if (iconName.isPresent()) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregatePercentagesQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregatePercentagesQuery.java index ebac42ed3..866b65780 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregatePercentagesQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionAggregatePercentagesQuery.java @@ -33,7 +33,6 @@ import com.djrapitops.plan.extension.implementation.results.server.ExtensionServ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -107,30 +106,40 @@ public class ExtensionAggregatePercentagesQuery implements Query processResults(ResultSet set) throws SQLException { - Map> tabDataByPluginID = extractTabDataByPluginID(set); - return ExtensionServerDataQuery.flatMapToServerData(tabDataByPluginID); + return extractTabDataByPluginID(set).toServerDataByPluginID(); } }); } - private Map> extractTabDataByPluginID(ResultSet set) throws SQLException { - Map> tabDataByPluginID = new HashMap<>(); + private QueriedTabData extractTabDataByPluginID(ResultSet set) throws SQLException { + QueriedTabData tabData = new QueriedTabData(); while (set.next()) { int pluginID = set.getInt("plugin_id"); - Map tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>()); - String tabName = Optional.ofNullable(set.getString("tab_name")).orElse(""); - ExtensionTabData.Factory inMap = tabData.get(tabName); - ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData); + ExtensionTabData.Factory extensionTab = tabData.getTab(pluginID, tabName, () -> extractTabInformation(tabName, set)); ExtensionDescriptive extensionDescriptive = extractDescriptive(set); extractAndPutDataTo(extensionTab, extensionDescriptive, set); - - tabData.put(tabName, extensionTab); - tabDataByPluginID.put(pluginID, tabData); } - return tabDataByPluginID; + return tabData; + } + + private TabInformation extractTabInformation(String tabName, ResultSet set) throws SQLException { + Optional tabPriority = Optional.of(set.getInt("tab_priority")); + if (set.wasNull()) { + tabPriority = Optional.empty(); + } + Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); + + Icon tabIcon = extractTabIcon(set); + + return new TabInformation( + tabName, + tabIcon, + elementOrder.orElse(ElementOrder.values()), + tabPriority.orElse(100) + ); } private void extractAndPutDataTo(ExtensionTabData.Factory extensionTab, ExtensionDescriptive descriptive, ResultSet set) throws SQLException { @@ -155,23 +164,6 @@ public class ExtensionAggregatePercentagesQuery implements Query tabData) throws SQLException { - Optional tabPriority = Optional.of(set.getInt("tab_priority")); - if (set.wasNull()) { - tabPriority = Optional.empty(); - } - Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); - - Icon tabIcon = extractTabIcon(set); - - return tabData.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation( - tabName, - tabIcon, - elementOrder.orElse(ElementOrder.values()), - tabPriority.orElse(100) - ))); - } - private Icon extractTabIcon(ResultSet set) throws SQLException { Optional iconName = Optional.ofNullable(set.getString("tab_icon_name")); if (iconName.isPresent()) { 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 a2617903f..8b4e8dc40 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 @@ -149,43 +149,40 @@ public class ExtensionPlayerDataQuery implements Query processResults(ResultSet set) throws SQLException { - Map> tabDataByPluginID = extractTabDataByPluginID(set); - return flatMapToPlayerData(tabDataByPluginID); + return extractTabDataByPluginID(set).toPlayerDataByPluginID(); } }; } - private Map flatMapToPlayerData(Map> tabDataByPluginID) { - Map dataByPluginID = new HashMap<>(); - for (Map.Entry> entry : tabDataByPluginID.entrySet()) { - Integer pluginID = entry.getKey(); - ExtensionPlayerData.Factory data = dataByPluginID.getOrDefault(pluginID, new ExtensionPlayerData.Factory(pluginID)); - for (ExtensionTabData.Factory tabData : entry.getValue().values()) { - data.addTab(tabData.build()); - } - dataByPluginID.put(pluginID, data); - } - return dataByPluginID; - } - - private Map> extractTabDataByPluginID(ResultSet set) throws SQLException { - Map> tabDataByPluginID = new HashMap<>(); + private QueriedTabData extractTabDataByPluginID(ResultSet set) throws SQLException { + QueriedTabData tabData = new QueriedTabData(); while (set.next()) { int pluginID = set.getInt("plugin_id"); - Map tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>()); - String tabName = Optional.ofNullable(set.getString("tab_name")).orElse(""); - ExtensionTabData.Factory inMap = tabData.get(tabName); - ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData); + ExtensionTabData.Factory extensionTab = tabData.getTab(pluginID, tabName, () -> extractTabInformation(tabName, set)); ExtensionDescriptive extensionDescriptive = extractDescriptive(set); extractAndPutDataTo(extensionTab, extensionDescriptive, set); - - tabData.put(tabName, extensionTab); - tabDataByPluginID.put(pluginID, tabData); } - return tabDataByPluginID; + return tabData; + } + + private TabInformation extractTabInformation(String tabName, ResultSet set) throws SQLException { + Optional tabPriority = Optional.of(set.getInt("tab_priority")); + if (set.wasNull()) { + tabPriority = Optional.empty(); + } + Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); + + Icon tabIcon = extractTabIcon(set); + + return new TabInformation( + tabName, + tabIcon, + elementOrder.orElse(ElementOrder.values()), + tabPriority.orElse(100) + ); } private void extractAndPutDataTo(ExtensionTabData.Factory extensionTab, ExtensionDescriptive descriptive, ResultSet set) throws SQLException { @@ -235,23 +232,6 @@ public class ExtensionPlayerDataQuery implements Query tabData) throws SQLException { - Optional tabPriority = Optional.of(set.getInt("tab_priority")); - if (set.wasNull()) { - tabPriority = Optional.empty(); - } - Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); - - Icon tabIcon = extractTabIcon(set); - - return tabData.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation( - tabName, - tabIcon, - elementOrder.orElse(ElementOrder.values()), - tabPriority.orElse(100) - ))); - } - private Icon extractTabIcon(ResultSet set) throws SQLException { Optional iconName = Optional.ofNullable(set.getString("tab_icon_name")); if (iconName.isPresent()) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerGroupsQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerGroupsQuery.java index 65df1095b..9aa623c15 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerGroupsQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerGroupsQuery.java @@ -33,7 +33,6 @@ import com.djrapitops.plan.extension.implementation.results.player.ExtensionPlay import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -96,45 +95,42 @@ public class ExtensionPlayerGroupsQuery implements Query processResults(ResultSet set) throws SQLException { - Map> tabDataByPluginID = extractTabDataByPluginID(set); - return flatMapToPlayerData(tabDataByPluginID); + return extractTabDataByPluginID(set).toPlayerDataByPluginID(); } }; } - private Map flatMapToPlayerData(Map> tabDataByPluginID) { - Map dataByPluginID = new HashMap<>(); - for (Map.Entry> entry : tabDataByPluginID.entrySet()) { - Integer pluginID = entry.getKey(); - ExtensionPlayerData.Factory data = dataByPluginID.getOrDefault(pluginID, new ExtensionPlayerData.Factory(pluginID)); - for (ExtensionTabData.Factory tabData : entry.getValue().values()) { - data.addTab(tabData.build()); - } - dataByPluginID.put(pluginID, data); - } - return dataByPluginID; - } - - private Map> extractTabDataByPluginID(ResultSet set) throws SQLException { - Map> tabDataByPluginID = new HashMap<>(); + private QueriedTabData extractTabDataByPluginID(ResultSet set) throws SQLException { + QueriedTabData tabData = new QueriedTabData(); while (set.next()) { int pluginID = set.getInt("plugin_id"); - Map tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>()); - String tabName = Optional.ofNullable(set.getString("tab_name")).orElse(""); - ExtensionTabData.Factory inMap = tabData.get(tabName); - ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData); + ExtensionTabData.Factory extensionTab = tabData.getTab(pluginID, tabName, () -> extractTabInformation(tabName, set)); ExtensionDescriptive extensionDescriptive = extractDescriptive(set); String groupName = set.getString(ExtensionGroupsTable.GROUP_NAME); extensionTab.putGroupData(new ExtensionStringData(extensionDescriptive, false, groupName)); - - tabData.put(tabName, extensionTab); - tabDataByPluginID.put(pluginID, tabData); } - return tabDataByPluginID; + return tabData; + } + + private TabInformation extractTabInformation(String tabName, ResultSet set) throws SQLException { + Optional tabPriority = Optional.of(set.getInt("tab_priority")); + if (set.wasNull()) { + tabPriority = Optional.empty(); + } + Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); + + Icon tabIcon = extractTabIcon(set); + + return new TabInformation( + tabName, + tabIcon, + elementOrder.orElse(ElementOrder.values()), + tabPriority.orElse(100) + ); } private ExtensionDescriptive extractDescriptive(ResultSet set) throws SQLException { @@ -149,23 +145,6 @@ public class ExtensionPlayerGroupsQuery implements Query tabData) throws SQLException { - Optional tabPriority = Optional.of(set.getInt("tab_priority")); - if (set.wasNull()) { - tabPriority = Optional.empty(); - } - Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); - - Icon tabIcon = extractTabIcon(set); - - return tabData.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation( - tabName, - tabIcon, - elementOrder.orElse(ElementOrder.values()), - tabPriority.orElse(100) - ))); - } - private Icon extractTabIcon(ResultSet set) throws SQLException { Optional iconName = Optional.ofNullable(set.getString("tab_icon_name")); if (iconName.isPresent()) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerTablesQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerTablesQuery.java index d106195c8..549010e12 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerTablesQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionPlayerTablesQuery.java @@ -27,9 +27,6 @@ import com.djrapitops.plan.extension.ElementOrder; import com.djrapitops.plan.extension.icon.Color; import com.djrapitops.plan.extension.icon.Family; import com.djrapitops.plan.extension.icon.Icon; -import com.djrapitops.plan.extension.implementation.TabInformation; -import com.djrapitops.plan.extension.implementation.results.ExtensionTabData; -import com.djrapitops.plan.extension.implementation.results.ExtensionTableData; import com.djrapitops.plan.extension.implementation.results.player.ExtensionPlayerData; import com.djrapitops.plan.extension.table.Table; import com.djrapitops.plan.extension.table.TableAccessor; @@ -65,63 +62,13 @@ public class ExtensionPlayerTablesQuery implements Query flatMapByPluginID(Map> tabDataByPluginID) { - Map dataByPluginID = new HashMap<>(); - for (Map.Entry> entry : tabDataByPluginID.entrySet()) { - Integer pluginID = entry.getKey(); - ExtensionPlayerData.Factory data = dataByPluginID.getOrDefault(pluginID, new ExtensionPlayerData.Factory(pluginID)); - for (ExtensionTabData.Factory tabData : entry.getValue().values()) { - data.addTab(tabData.build()); - } - dataByPluginID.put(pluginID, data); - } - return dataByPluginID; - } - @Override public Map executeQuery(SQLDB db) { - Map> tablesByPluginIDAndTableID = db.query(queryTableProviders()); - Map> tablesWithValues = db.query(queryTableValues(tablesByPluginIDAndTableID)); - - Map> tabDataByPluginID = mapToTabsByPluginID(tablesWithValues); - return flatMapByPluginID(tabDataByPluginID); + QueriedTables tablesWithValues = db.query(placeValuesToTables(db.query(queryTableProviders()))); + return tablesWithValues.toQueriedTabs().toPlayerDataByPluginID(); } - /** - * @param tablesByPluginIDAndTableID {@code >} - * @return {@code } - */ - private Map> mapToTabsByPluginID(Map> tablesByPluginIDAndTableID) { - Map> byPluginID = new HashMap<>(); - - for (Map.Entry> entry : tablesByPluginIDAndTableID.entrySet()) { - Integer pluginID = entry.getKey(); - Map byTabName = byPluginID.getOrDefault(pluginID, new HashMap<>()); - for (Table.Factory table : entry.getValue().values()) { - // Extra Table information - String tableName = TableAccessor.getTableName(table); - Color tableColor = TableAccessor.getColor(table); - - // Extra tab information - String tabName = TableAccessor.getTabName(table); - int tabPriority = TableAccessor.getTabPriority(table); - ElementOrder[] tabOrder = TableAccessor.getTabOrder(table); - Icon tabIcon = TableAccessor.getTabIcon(table); - - ExtensionTabData.Factory tab = byTabName.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation(tabName, tabIcon, tabOrder, tabPriority))); - tab.putTableData(new ExtensionTableData( - tableName, table.build(), tableColor - )); - byTabName.put(tabName, tab); - } - byPluginID.put(pluginID, byTabName); - } - - return byPluginID; - } - - // Map: > - private Query>> queryTableValues(Map> tables) { + private Query placeValuesToTables(QueriedTables tables) { String selectTableValues = SELECT + ExtensionTableProviderTable.PLUGIN_ID + ',' + ExtensionPlayerTableValueTable.TABLE_ID + ',' + @@ -133,37 +80,23 @@ public class ExtensionPlayerTablesQuery implements Query>>(selectTableValues, 10000) { + return new QueryStatement(selectTableValues, 10000) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, playerUUID.toString()); } @Override - public Map> processResults(ResultSet set) throws SQLException { + public QueriedTables processResults(ResultSet set) throws SQLException { while (set.next()) { - Table.Factory table = getTable(set); - if (table == null) { - continue; - } - - Object[] row = extractTableRow(set); - if (row.length > 0) { - table.addRow(row); - } + tables.addRow( + set.getInt(ExtensionTableProviderTable.PLUGIN_ID), + set.getInt(ExtensionPlayerTableValueTable.TABLE_ID), + extractTableRow(set) + ); } return tables; } - - private Table.Factory getTable(ResultSet set) throws SQLException { - int pluginID = set.getInt(ExtensionTableProviderTable.PLUGIN_ID); - Map byTableID = tables.get(pluginID); - if (byTableID == null) { - return null; - } - int tableID = set.getInt(ExtensionPlayerTableValueTable.TABLE_ID); - return byTableID.get(tableID); - } }; } @@ -180,8 +113,7 @@ public class ExtensionPlayerTablesQuery implements Query> - private Query>> queryTableProviders() { + private Query queryTableProviders() { String selectTables = SELECT + "p1." + ExtensionTableProviderTable.ID + " as table_id," + "p1." + ExtensionTableProviderTable.PLUGIN_ID + " as plugin_id," + @@ -219,41 +151,41 @@ public class ExtensionPlayerTablesQuery implements Query>>(selectTables, 100) { + return new QueryStatement(selectTables, 100) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, playerUUID.toString()); } @Override - public Map> processResults(ResultSet set) throws SQLException { - Map> byPluginID = new HashMap<>(); - + public QueriedTables processResults(ResultSet set) throws SQLException { + QueriedTables tables = new QueriedTables(); while (set.next()) { - int pluginID = set.getInt("plugin_id"); - Map byTableID = byPluginID.getOrDefault(pluginID, new HashMap<>()); - - int tableID = set.getInt("table_id"); - Table.Factory table = Table.builder(); - - extractColumns(set, table); - - TableAccessor.setColor(table, Color.getByName(set.getString("table_color")).orElse(Color.NONE)); - TableAccessor.setTableName(table, set.getString("table_name")); - TableAccessor.setTabName(table, Optional.ofNullable(set.getString("tab_name")).orElse("")); - TableAccessor.setTabPriority(table, Optional.of(set.getInt("tab_priority")).orElse(100)); - TableAccessor.setTabOrder(table, Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize).orElse(ElementOrder.values())); - TableAccessor.setTabIcon(table, extractIcon(set, "tab_icon")); - - byTableID.put(tableID, table); - byPluginID.put(pluginID, byTableID); + tables.put( + set.getInt(ExtensionTableProviderTable.PLUGIN_ID), + set.getInt(ExtensionPlayerTableValueTable.TABLE_ID), + extractTable(set) + ); } - - return byPluginID; + return tables; } }; } + private Table.Factory extractTable(ResultSet set) throws SQLException { + Table.Factory table = Table.builder(); + + extractColumns(set, table); + + TableAccessor.setColor(table, Color.getByName(set.getString("table_color")).orElse(Color.NONE)); + TableAccessor.setTableName(table, set.getString("table_name")); + TableAccessor.setTabName(table, Optional.ofNullable(set.getString("tab_name")).orElse("")); + TableAccessor.setTabPriority(table, Optional.of(set.getInt("tab_priority")).orElse(100)); + TableAccessor.setTabOrder(table, Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize).orElse(ElementOrder.values())); + TableAccessor.setTabIcon(table, extractIcon(set, "tab_icon")); + return table; + } + private void extractColumns(ResultSet set, Table.Factory table) throws SQLException { String col1 = set.getString(ExtensionTableProviderTable.COL_1); if (col1 != null) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionServerDataQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionServerDataQuery.java index a88d91ced..60c0f8006 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionServerDataQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionServerDataQuery.java @@ -61,19 +61,6 @@ public class ExtensionServerDataQuery implements Query this.serverUUID = serverUUID; } - static Map flatMapToServerData(Map> tabDataByPluginID) { - Map dataByPluginID = new HashMap<>(); - for (Map.Entry> entry : tabDataByPluginID.entrySet()) { - Integer pluginID = entry.getKey(); - ExtensionServerData.Factory data = dataByPluginID.getOrDefault(pluginID, new ExtensionServerData.Factory(pluginID)); - for (ExtensionTabData.Factory tabData : entry.getValue().values()) { - data.addTab(tabData.build()); - } - dataByPluginID.put(pluginID, data); - } - return dataByPluginID; - } - @Override public List executeQuery(SQLDB db) { List extensionsOfServer = db.query(ExtensionInformationQueries.extensionsOfServer(serverUUID)); @@ -162,30 +149,40 @@ public class ExtensionServerDataQuery implements Query @Override public Map processResults(ResultSet set) throws SQLException { - Map> tabDataByPluginID = extractTabDataByPluginID(set); - return flatMapToServerData(tabDataByPluginID); + return extractTabDataByPluginID(set).toServerDataByPluginID(); } }; } - private Map> extractTabDataByPluginID(ResultSet set) throws SQLException { - Map> tabDataByPluginID = new HashMap<>(); + private QueriedTabData extractTabDataByPluginID(ResultSet set) throws SQLException { + QueriedTabData tabData = new QueriedTabData(); while (set.next()) { int pluginID = set.getInt("plugin_id"); - Map tabData = tabDataByPluginID.getOrDefault(pluginID, new HashMap<>()); - String tabName = Optional.ofNullable(set.getString("tab_name")).orElse(""); - ExtensionTabData.Factory inMap = tabData.get(tabName); - ExtensionTabData.Factory extensionTab = inMap != null ? inMap : extractTab(tabName, set, tabData); + ExtensionTabData.Factory extensionTab = tabData.getTab(pluginID, tabName, () -> extractTabInformation(tabName, set)); ExtensionDescriptive extensionDescriptive = extractDescriptive(set); extractAndPutDataTo(extensionTab, extensionDescriptive, set); - - tabData.put(tabName, extensionTab); - tabDataByPluginID.put(pluginID, tabData); } - return tabDataByPluginID; + return tabData; + } + + private TabInformation extractTabInformation(String tabName, ResultSet set) throws SQLException { + Optional tabPriority = Optional.of(set.getInt("tab_priority")); + if (set.wasNull()) { + tabPriority = Optional.empty(); + } + Optional elementOrder = Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize); + + Icon tabIcon = extractTabIcon(set); + + return new TabInformation( + tabName, + tabIcon, + elementOrder.orElse(ElementOrder.values()), + tabPriority.orElse(100) + ); } private void extractAndPutDataTo(ExtensionTabData.Factory extensionTab, ExtensionDescriptive descriptive, ResultSet set) throws SQLException { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionServerTablesQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionServerTablesQuery.java index 047b7b2be..930563e14 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionServerTablesQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionServerTablesQuery.java @@ -19,17 +19,11 @@ package com.djrapitops.plan.extension.implementation.storage.queries; import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.QueryStatement; -import com.djrapitops.plan.db.sql.tables.ExtensionIconTable; -import com.djrapitops.plan.db.sql.tables.ExtensionServerTableValueTable; -import com.djrapitops.plan.db.sql.tables.ExtensionTabTable; -import com.djrapitops.plan.db.sql.tables.ExtensionTableProviderTable; +import com.djrapitops.plan.db.sql.tables.*; import com.djrapitops.plan.extension.ElementOrder; import com.djrapitops.plan.extension.icon.Color; import com.djrapitops.plan.extension.icon.Family; import com.djrapitops.plan.extension.icon.Icon; -import com.djrapitops.plan.extension.implementation.TabInformation; -import com.djrapitops.plan.extension.implementation.results.ExtensionTabData; -import com.djrapitops.plan.extension.implementation.results.ExtensionTableData; import com.djrapitops.plan.extension.implementation.results.server.ExtensionServerData; import com.djrapitops.plan.extension.table.Table; import com.djrapitops.plan.extension.table.TableAccessor; @@ -67,48 +61,11 @@ public class ExtensionServerTablesQuery implements Query executeQuery(SQLDB db) { - Map> tablesByPluginIDAndTableID = db.query(queryTableProviders()); - Map> tablesWithValues = db.query(queryTableValues(tablesByPluginIDAndTableID)); - - Map> tabDataByPluginID = mapToTabsByPluginID(tablesWithValues); - return ExtensionServerDataQuery.flatMapToServerData(tabDataByPluginID); + QueriedTables tablesWithValues = db.query(queryTableValues(db.query(queryTableProviders()))); + return tablesWithValues.toQueriedTabs().toServerDataByPluginID(); } - /** - * @param tablesByPluginIDAndTableID {@code >} - * @return {@code } - */ - private Map> mapToTabsByPluginID(Map> tablesByPluginIDAndTableID) { - Map> byPluginID = new HashMap<>(); - - for (Map.Entry> entry : tablesByPluginIDAndTableID.entrySet()) { - Integer pluginID = entry.getKey(); - Map byTabName = byPluginID.getOrDefault(pluginID, new HashMap<>()); - for (Table.Factory table : entry.getValue().values()) { - // Extra Table information - String tableName = TableAccessor.getTableName(table); - Color tableColor = TableAccessor.getColor(table); - - // Extra tab information - String tabName = TableAccessor.getTabName(table); - int tabPriority = TableAccessor.getTabPriority(table); - ElementOrder[] tabOrder = TableAccessor.getTabOrder(table); - Icon tabIcon = TableAccessor.getTabIcon(table); - - ExtensionTabData.Factory tab = byTabName.getOrDefault(tabName, new ExtensionTabData.Factory(new TabInformation(tabName, tabIcon, tabOrder, tabPriority))); - tab.putTableData(new ExtensionTableData( - tableName, table.build(), tableColor - )); - byTabName.put(tabName, tab); - } - byPluginID.put(pluginID, byTabName); - } - - return byPluginID; - } - - // Map: > - private Query>> queryTableValues(Map> tables) { + private Query queryTableValues(QueriedTables tables) { String selectTableValues = SELECT + ExtensionTableProviderTable.PLUGIN_ID + ',' + ExtensionServerTableValueTable.TABLE_ID + ',' + @@ -121,37 +78,23 @@ public class ExtensionServerTablesQuery implements Query>>(selectTableValues, 10000) { + return new QueryStatement(selectTableValues, 10000) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, serverUUID.toString()); } @Override - public Map> processResults(ResultSet set) throws SQLException { + public QueriedTables processResults(ResultSet set) throws SQLException { while (set.next()) { - Table.Factory table = getTable(set); - if (table == null) { - continue; - } - - Object[] row = extractTableRow(set); - if (row.length > 0) { - table.addRow(row); - } + tables.addRow( + set.getInt(ExtensionTableProviderTable.PLUGIN_ID), + set.getInt(ExtensionPlayerTableValueTable.TABLE_ID), + extractTableRow(set) + ); } return tables; } - - private Table.Factory getTable(ResultSet set) throws SQLException { - int pluginID = set.getInt(ExtensionTableProviderTable.PLUGIN_ID); - Map byTableID = tables.get(pluginID); - if (byTableID == null) { - return null; - } - int tableID = set.getInt(ExtensionServerTableValueTable.TABLE_ID); - return byTableID.get(tableID); - } }; } @@ -168,8 +111,7 @@ public class ExtensionServerTablesQuery implements Query> - private Query>> queryTableProviders() { + private Query queryTableProviders() { String selectTables = SELECT + "p1." + ExtensionTableProviderTable.ID + " as table_id," + "p1." + ExtensionTableProviderTable.PLUGIN_ID + " as plugin_id," + @@ -212,41 +154,43 @@ public class ExtensionServerTablesQuery implements Query>>(selectTables, 100) { + return new QueryStatement(selectTables, 100) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, serverUUID.toString()); } @Override - public Map> processResults(ResultSet set) throws SQLException { - Map> byPluginID = new HashMap<>(); + public QueriedTables processResults(ResultSet set) throws SQLException { + QueriedTables tables = new QueriedTables(); while (set.next()) { - int pluginID = set.getInt("plugin_id"); - Map byTableID = byPluginID.getOrDefault(pluginID, new HashMap<>()); - - int tableID = set.getInt("table_id"); - Table.Factory table = Table.builder(); - - extractColumns(set, table); - - TableAccessor.setColor(table, Color.getByName(set.getString("table_color")).orElse(Color.NONE)); - TableAccessor.setTableName(table, set.getString("table_name")); - TableAccessor.setTabName(table, Optional.ofNullable(set.getString("tab_name")).orElse("")); - TableAccessor.setTabPriority(table, Optional.of(set.getInt("tab_priority")).orElse(100)); - TableAccessor.setTabOrder(table, Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize).orElse(ElementOrder.values())); - TableAccessor.setTabIcon(table, extractIcon(set, "tab_icon")); - - byTableID.put(tableID, table); - byPluginID.put(pluginID, byTableID); + tables.put( + set.getInt(ExtensionTableProviderTable.PLUGIN_ID), + set.getInt(ExtensionPlayerTableValueTable.TABLE_ID), + extractTable(set) + ); } - return byPluginID; + return tables; } }; } + private Table.Factory extractTable(ResultSet set) throws SQLException { + Table.Factory table = Table.builder(); + + extractColumns(set, table); + + TableAccessor.setColor(table, Color.getByName(set.getString("table_color")).orElse(Color.NONE)); + TableAccessor.setTableName(table, set.getString("table_name")); + TableAccessor.setTabName(table, Optional.ofNullable(set.getString("tab_name")).orElse("")); + TableAccessor.setTabPriority(table, Optional.of(set.getInt("tab_priority")).orElse(100)); + TableAccessor.setTabOrder(table, Optional.ofNullable(set.getString(ExtensionTabTable.ELEMENT_ORDER)).map(ElementOrder::deserialize).orElse(ElementOrder.values())); + TableAccessor.setTabIcon(table, extractIcon(set, "tab_icon")); + return table; + } + private void extractColumns(ResultSet set, Table.Factory table) throws SQLException { String col1 = set.getString(ExtensionTableProviderTable.COL_1); if (col1 != null) { 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 new file mode 100644 index 000000000..64174dc9b --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTabData.java @@ -0,0 +1,89 @@ +/* + * 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.extension.implementation.storage.queries; + +import com.djrapitops.plan.extension.implementation.TabInformation; +import com.djrapitops.plan.extension.implementation.results.ExtensionTabData; +import com.djrapitops.plan.extension.implementation.results.player.ExtensionPlayerData; +import com.djrapitops.plan.extension.implementation.results.server.ExtensionServerData; +import com.djrapitops.plan.utilities.java.ThrowingSupplier; + +import java.util.HashMap; +import java.util.Map; + +/** + * Query utility for extracting Tabs. + * + * @author Rsl1122 + */ +public class QueriedTabData { + + private final Map> byPluginID; + + public QueriedTabData() { + byPluginID = new HashMap<>(); + } + + public ExtensionTabData.Factory getTab(int pluginID, String tabName, ThrowingSupplier newDefault) throws K { + Map byTabName = byPluginID.getOrDefault(pluginID, new HashMap<>()); + + ExtensionTabData.Factory tab = byTabName.get(tabName); + if (tab == null) { + tab = new ExtensionTabData.Factory(newDefault.get()); + } + + byTabName.put(tabName, tab); + byPluginID.put(pluginID, byTabName); + return tab; + } + + public Map toServerDataByPluginID() { + Map dataByPluginID = new HashMap<>(); + for (Map.Entry> entry : byPluginID.entrySet()) { + Integer pluginID = entry.getKey(); + + ExtensionServerData.Factory data = dataByPluginID.get(pluginID); + if (data == null) { + data = new ExtensionServerData.Factory(pluginID); + } + + for (ExtensionTabData.Factory tabData : entry.getValue().values()) { + data.addTab(tabData.build()); + } + dataByPluginID.put(pluginID, data); + } + return dataByPluginID; + } + + public Map toPlayerDataByPluginID() { + Map dataByPluginID = new HashMap<>(); + for (Map.Entry> entry : byPluginID.entrySet()) { + Integer pluginID = entry.getKey(); + + ExtensionPlayerData.Factory data = dataByPluginID.get(pluginID); + if (data == null) { + data = new ExtensionPlayerData.Factory(pluginID); + } + + for (ExtensionTabData.Factory tabData : entry.getValue().values()) { + data.addTab(tabData.build()); + } + dataByPluginID.put(pluginID, data); + } + return dataByPluginID; + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..176800243 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/queries/QueriedTables.java @@ -0,0 +1,96 @@ +/* + * 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.extension.implementation.storage.queries; + +import com.djrapitops.plan.extension.ElementOrder; +import com.djrapitops.plan.extension.icon.Color; +import com.djrapitops.plan.extension.icon.Icon; +import com.djrapitops.plan.extension.implementation.TabInformation; +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 java.util.HashMap; +import java.util.Map; + +/** + * Query utility for extracting Tables. + * + * @author Rsl1122 + */ +public class QueriedTables { + + // Map: > + private final Map> byPluginID; + + public QueriedTables() { + byPluginID = new HashMap<>(); + } + + public void put(int pluginID, int tableID, Table.Factory table) { + Map byTableID = byPluginID.getOrDefault(pluginID, new HashMap<>()); + byTableID.put(tableID, table); + byPluginID.put(pluginID, byTableID); + } + + public void addRow(int pluginID, int tableID, Object[] row) { + if (row.length <= 0) return; + + Map byTableID = byPluginID.get(pluginID); + if (byTableID == null) return; + + Table.Factory table = byTableID.get(tableID); + if (table == null) return; + + table.addRow(row); + } + + public QueriedTabData toQueriedTabs() { + QueriedTabData tabData = new QueriedTabData(); + + for (Map.Entry> entry : byPluginID.entrySet()) { + Integer pluginID = entry.getKey(); + + for (Table.Factory table : entry.getValue().values()) { + // Extra Table information + String tableName = TableAccessor.getTableName(table); + Color tableColor = TableAccessor.getColor(table); + + // Extra tab information + String tabName = TableAccessor.getTabName(table); + + ExtensionTabData.Factory tab = tabData.getTab(pluginID, tabName, () -> extractTabInformation(table)); + tab.putTableData(new ExtensionTableData( + tableName, table.build(), tableColor + )); + } + } + + return tabData; + } + + private TabInformation extractTabInformation(Table.Factory table) { + String tabName = TableAccessor.getTabName(table); + int tabPriority = TableAccessor.getTabPriority(table); + ElementOrder[] tabOrder = TableAccessor.getTabOrder(table); + Icon tabIcon = TableAccessor.getTabIcon(table); + + return new TabInformation(tabName, tabIcon, tabOrder, tabPriority); + } + +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/ThrowingSupplier.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/ThrowingSupplier.java new file mode 100644 index 000000000..7e85de597 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/ThrowingSupplier.java @@ -0,0 +1,30 @@ +/* + * 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; + +/** + * Functional interface that performs an operation that might throw an exception. + *

+ * Follows naming scheme of Java 8 functional interfaces. + * + * @author Rsl1122 + */ +public interface ThrowingSupplier { + + T get() throws K; + +} \ No newline at end of file