From: "Enrico Weigelt, metux IT consult" <[email protected]>
This map type eases accumulation of key->integer and key-double tuples by key,
w/o the ovead of streams and callacks to BinaryOperator objects.
---
.../gui/panel/report/ReportCompactColonyPanel.java | 35 ++++------
.../sf/freecol/common/util/CollectionUtils.java | 8 ---
src/net/sf/freecol/common/util/DoubleAccMap.java | 75 ++++++++++++++++++++++
src/net/sf/freecol/common/util/IntAccMap.java | 75 ++++++++++++++++++++++
4 files changed, 163 insertions(+), 30 deletions(-)
create mode 100644 src/net/sf/freecol/common/util/DoubleAccMap.java
create mode 100644 src/net/sf/freecol/common/util/IntAccMap.java
diff --git
a/src/net/sf/freecol/client/gui/panel/report/ReportCompactColonyPanel.java
b/src/net/sf/freecol/client/gui/panel/report/ReportCompactColonyPanel.java
index a987560ec43..e9b87d002ef 100644
--- a/src/net/sf/freecol/client/gui/panel/report/ReportCompactColonyPanel.java
+++ b/src/net/sf/freecol/client/gui/panel/report/ReportCompactColonyPanel.java
@@ -71,6 +71,8 @@ import net.sf.freecol.common.model.UnitType;
import net.sf.freecol.common.model.WorkLocation;
import net.sf.freecol.common.model.WorkLocation.Suggestion;
import net.sf.freecol.common.resources.ResourceManager;
+import net.sf.freecol.common.util.IntAccMap;
+import net.sf.freecol.common.util.DoubleAccMap;
import static net.sf.freecol.common.util.CollectionUtils.*;
@@ -860,46 +862,35 @@ public final class ReportCompactColonyPanel extends
ReportPanel
"newline, span, growx");
// Accumulate all the summaries
- Map<Region, Integer> rRegionMap = new HashMap<>();
+ IntAccMap<Region> rRegionMap = new IntAccMap<>();
List<TileImprovementSuggestion> rTileSuggestions = new ArrayList<>();
int rFamine = 0, rBonus = 0, rSizeChange = 0,
teacherLen = 0, improveLen = 0;
double rNewColonist = 0.0;
Map<GoodsType, ColonySummary.GoodsProduction> rProduction
= new HashMap<>();
- Map<UnitType, Integer> rTeachers = new HashMap<>();
+ IntAccMap<UnitType> rTeachers = new IntAccMap<>();
List<Unit> rNotWorking = new ArrayList<>();
List<UnitType> rCouldWork = new ArrayList<>();
- Map<UnitType, Integer> rImprove = new HashMap<>();
- Map<GoodsType, Double> rNeeded = new HashMap<>();
+ IntAccMap<UnitType> rImprove = new IntAccMap<>();
+ DoubleAccMap<GoodsType> rNeeded = new DoubleAccMap<>();
for (ColonySummary s : summaries) {
- accumulateToMap(rRegionMap, s.colony.getTile().getRegion(), 1,
- integerAccumulator);
+ rRegionMap.inc(s.colony.getTile().getRegion());
rTileSuggestions.addAll(s.tileSuggestions);
if (s.famine) rFamine++;
if (s.newColonist > 0) rNewColonist += s.newColonist;
rBonus += s.bonus;
rSizeChange += s.sizeChange;
- accumulateMap(rProduction, s.production,
- ColonySummary.goodsProductionAccumulator);
+ ColonySummary.accumulateGoodsProduction(rProduction, s.production);
teacherLen = Math.max(teacherLen, s.teachers.size());
- for (Unit u : s.teachers.keySet()) {
- accumulateToMap(rTeachers, u.getType(), 1, integerAccumulator);
- }
+ for (Unit u : s.teachers.keySet()) rTeachers.inc(u.getType());
rNotWorking.addAll(s.notWorking);
rCouldWork.addAll(s.couldWork);
improveLen = Math.max(improveLen, s.improve.size() +
s.want.size());
- for (UnitType ut : s.improve.keySet()) {
- accumulateToMap(rImprove, ut, 1, integerAccumulator);
- }
- for (UnitType ut : s.want.keySet()) {
- accumulateToMap(rImprove, ut, 1, integerAccumulator);
- }
- if (s.needed != null && s.needed.getType().isStorable()) {
- accumulateToMap(rNeeded, s.needed.getType(),
- (double)s.needed.getAmount() / s.completeTurns,
- doubleAccumulator);
- }
+ for (UnitType ut : s.improve.keySet()) rImprove.inc(ut);
+ for (UnitType ut : s.want.keySet()) rImprove.inc(ut);
+ if (s.needed != null && s.needed.getType().isStorable())
+ rNeeded.addDouble(s.needed.getType(),
(double)s.needed.getAmount() / s.completeTurns);
}
rNewColonist = Math.round(rNewColonist / summaries.size());
diff --git a/src/net/sf/freecol/common/util/CollectionUtils.java
b/src/net/sf/freecol/common/util/CollectionUtils.java
index 7f911e72ad8..1a265516d81 100644
--- a/src/net/sf/freecol/common/util/CollectionUtils.java
+++ b/src/net/sf/freecol/common/util/CollectionUtils.java
@@ -58,14 +58,6 @@ public class CollectionUtils {
private static final double SUM_DOUBLE_DEFAULT = 0.0;
private static final double PRODUCT_DEFAULT = 1.0;
- /** Trivial integer accumulator. */
- public static final BinaryOperator<Integer> integerAccumulator
- = (i1, i2) -> i1 + i2;
-
- /** Trivial double accumulator. */
- public static final BinaryOperator<Double> doubleAccumulator
- = (d1, d2) -> d1 + d2;
-
public static final BinaryOperator<Double> doubleMultiplicator
= new BinaryOperator<Double>() {
public Double apply(Double d1, Double d2) {
diff --git a/src/net/sf/freecol/common/util/DoubleAccMap.java
b/src/net/sf/freecol/common/util/DoubleAccMap.java
new file mode 100644
index 00000000000..258b9e9d154
--- /dev/null
+++ b/src/net/sf/freecol/common/util/DoubleAccMap.java
@@ -0,0 +1,75 @@
+/**
+ * Copyright (C) 2002-2017 The FreeCol Team
+ *
+ * This file is part of FreeCol.
+ *
+ * FreeCol is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FreeCol 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with FreeCol. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.sf.freecol.common.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class DoubleAccMap<T> extends HashMap<T,Double>
+{
+ public double inc(T key) {
+ return addDouble(key, 1);
+ }
+
+ public double addDouble(T key, double value) {
+ if (!containsKey(key)) {
+ put(key, value);
+ return value;
+ }
+
+ Double oldval = get(key);
+ if (oldval == null) {
+ put(key, value);
+ return value;
+ }
+
+ double newval = oldval + value;
+ put(key, newval);
+ return newval;
+ }
+
+ public double mulDouble(T key, double value) {
+ if (!containsKey(key)) {
+ put(key, value);
+ return value;
+ }
+
+ Double oldval = get(key);
+ if (oldval == null) {
+ put(key, value);
+ return value;
+ }
+
+ double newval = oldval * value;
+ put(key, newval);
+ return newval;
+ }
+
+ public double getDouble(T key) {
+ if (!containsKey(key))
+ return 0;
+
+ Double val = get(key);
+ if (val == null)
+ return 0;
+
+ return val;
+ }
+}
diff --git a/src/net/sf/freecol/common/util/IntAccMap.java
b/src/net/sf/freecol/common/util/IntAccMap.java
new file mode 100644
index 00000000000..5325c38058f
--- /dev/null
+++ b/src/net/sf/freecol/common/util/IntAccMap.java
@@ -0,0 +1,75 @@
+/**
+ * Copyright (C) 2002-2017 The FreeCol Team
+ *
+ * This file is part of FreeCol.
+ *
+ * FreeCol is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FreeCol 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with FreeCol. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.sf.freecol.common.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class IntAccMap<T> extends HashMap<T,Integer>
+{
+ public int inc(T key) {
+ return addInt(key, 1);
+ }
+
+ public int addInt(T key, int value) {
+ if (!containsKey(key)) {
+ put(key, value);
+ return value;
+ }
+
+ Integer oldval = get(key);
+ if (oldval == null) {
+ put(key, value);
+ return value;
+ }
+
+ int newval = oldval + value;
+ put(key, newval);
+ return newval;
+ }
+
+ public int mulInt(T key, int value) {
+ if (!containsKey(key)) {
+ put(key, value);
+ return value;
+ }
+
+ Integer oldval = get(key);
+ if (oldval == null) {
+ put(key, value);
+ return value;
+ }
+
+ int newval = oldval * value;
+ put(key, newval);
+ return newval;
+ }
+
+ public int getInt(T key) {
+ if (!containsKey(key))
+ return 0;
+
+ Integer val = get(key);
+ if (val == null)
+ return 0;
+
+ return val;
+ }
+}
--
2.11.0.rc0.7.gbe5a750
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Freecol-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freecol-developers