From: "Enrico Weigelt, metux IT consult" <enrico.weig...@gr13.net>
--- .../freecol/client/control/InGameController.java | 4 +- .../gui/panel/colopedia/GoodsDetailPanel.java | 2 +- .../gui/panel/report/ReportCompactColonyPanel.java | 6 +-- src/net/sf/freecol/common/model/AbstractGoods.java | 63 ++++++++++++++++++---- src/net/sf/freecol/common/model/BuildQueue.java | 2 +- src/net/sf/freecol/common/model/Building.java | 2 +- src/net/sf/freecol/common/model/Colony.java | 7 ++- .../sf/freecol/common/model/ProductionCache.java | 6 +-- .../sf/freecol/common/model/ProductionInfo.java | 6 +-- src/net/sf/freecol/common/model/ProductionMap.java | 6 +-- .../sf/freecol/common/model/ProductionType.java | 2 +- src/net/sf/freecol/common/model/Role.java | 2 +- .../sf/freecol/common/model/TradeRouteStop.java | 11 ++-- src/net/sf/freecol/common/model/WorkLocation.java | 6 +-- src/net/sf/freecol/server/model/ServerColony.java | 15 ++++-- .../sf/freecol/server/model/ServerColonyTest.java | 3 +- 16 files changed, 93 insertions(+), 50 deletions(-) diff --git a/src/net/sf/freecol/client/control/InGameController.java b/src/net/sf/freecol/client/control/InGameController.java index b164c8ce020..9d2f6239f91 100644 --- a/src/net/sf/freecol/client/control/InGameController.java +++ b/src/net/sf/freecol/client/control/InGameController.java @@ -1958,7 +1958,7 @@ public final class InGameController extends FreeColClientHolder { // failed somewhere). If it is expected to load, reduce the // loading amount by what is already on board. for (Goods g : unit.getCompactGoods()) { - AbstractGoods ag = find(toLoad, AbstractGoods.matches(g.getType())); + AbstractGoods ag = AbstractGoods.findByType(toLoad, g.getType()); if (ag == null) { // Excess goods on board, failed unload? unexpected.addStringTemplate(g.getLabel()); } else { @@ -2006,7 +2006,7 @@ public final class InGameController extends FreeColClientHolder { turns += unit.getTurnsToReach(start, trs.getLocation()); int amountIn = trs.getImportAmount(type, turns), amountOut = trs.getExportAmount(type, turns); - if (none(trs.getCompactCargo(), AbstractGoods.matches(type)) + if ((AbstractGoods.findByType(trs.getCompactCargo(),type) == null) || amountIn > amountOut) { importAmount = amountIn; unload = trs; diff --git a/src/net/sf/freecol/client/gui/panel/colopedia/GoodsDetailPanel.java b/src/net/sf/freecol/client/gui/panel/colopedia/GoodsDetailPanel.java index 9bc0e48c047..22268485a75 100644 --- a/src/net/sf/freecol/client/gui/panel/colopedia/GoodsDetailPanel.java +++ b/src/net/sf/freecol/client/gui/panel/colopedia/GoodsDetailPanel.java @@ -216,7 +216,7 @@ public class GoodsDetailPanel extends ColopediaGameObjectTypePanel<GoodsType> { boolean result = false; for (T bt : input) { if (bt.needsGoodsToBuild() - && any(bt.getRequiredGoods(), AbstractGoods.matches(type))) { + && AbstractGoods.anyIsType(bt.getRequiredGoods(), type)) { output.add(bt); result = true; } 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 e9b87d002ef..9d046717dfa 100644 --- a/src/net/sf/freecol/client/gui/panel/report/ReportCompactColonyPanel.java +++ b/src/net/sf/freecol/client/gui/panel/report/ReportCompactColonyPanel.java @@ -291,8 +291,7 @@ public final class ReportCompactColonyPanel extends ReportPanel for (WorkLocation wl : colony.getWorkLocationsForProducing(goodsType)) { ProductionInfo pi = colony.getProductionInfo(wl); if (pi == null) continue; - deficit = find(pi.getConsumptionDeficit(), - AbstractGoods.matches(goodsType)); + deficit = AbstractGoods.findByType(pi.getConsumptionDeficit(), goodsType); if (deficit != null) { status = ProductionStatus.CONSUMPTION; extra = deficit.getAmount(); @@ -317,8 +316,7 @@ public final class ReportCompactColonyPanel extends ReportPanel for (WorkLocation wl : colony.getWorkLocationsForProducing(goodsType)) { ProductionInfo pi = colony.getProductionInfo(wl); if (pi == null) continue; - deficit = find(pi.getProductionDeficit(), - AbstractGoods.matches(goodsType)); + deficit = AbstractGoods.findByType(pi.getProductionDeficit(),goodsType); if (deficit != null) { status = ProductionStatus.PRODUCTION; extra = deficit.getAmount(); diff --git a/src/net/sf/freecol/common/model/AbstractGoods.java b/src/net/sf/freecol/common/model/AbstractGoods.java index 638588568ff..5d2e67ca790 100644 --- a/src/net/sf/freecol/common/model/AbstractGoods.java +++ b/src/net/sf/freecol/common/model/AbstractGoods.java @@ -19,9 +19,8 @@ package net.sf.freecol.common.model; -import java.util.Collection; import java.util.Comparator; -import java.util.function.Predicate; +import java.util.List; import net.sf.freecol.common.model.GoodsType; import static net.sf.freecol.common.util.CollectionUtils.*; @@ -207,8 +206,8 @@ public class AbstractGoods extends FreeColObject implements Named { * @return The goods count found, or zero if not found. */ public static int getCount(GoodsType type, - Collection<? extends AbstractGoods> goods) { - AbstractGoods ag = find(goods, matches(type)); + List<? extends AbstractGoods> goods) { + AbstractGoods ag = AbstractGoods.findByType(goods, type); return (ag == null) ? 0 : ag.getAmount(); } @@ -225,15 +224,61 @@ public class AbstractGoods extends FreeColObject implements Named { } /** - * A predicate maker to match by type. + * Check whether it is of the given {@link GoodsType} * - * @param key The key of type {@link GoodsType} - * @return A suitable {@code Predicate}. + * @param gt The {@link GoodsType} to match against + * @return True if matching */ - public static final Predicate<? super AbstractGoods> matches(final GoodsType key) { - return matchKey(key, AbstractGoods::getType); + public final boolean isType(GoodsType gt) { + if ((gt == null) || (type == null)) + return false; + + return (type == gt || type.equals(gt)); } + /** + * Check whether any in the list is of given {@link GoodsType} + */ + public static boolean anyIsType(List<AbstractGoods> l, GoodsType gt) { + if (l != null) + for (AbstractGoods ag : l) + if (ag.isType(gt)) + return true; + + return false; + } + + /** + * find any in list by type + */ + public static AbstractGoods findByType(List<AbstractGoods> l, GoodsType gt) { + if (l != null) + for (AbstractGoods ag : l) + if (ag.getType() == gt) + return ag; + + return false; + } + + public static AbstractGoods findByType(List<AbstractGoods> l, AbstractGoods ag) { + return findByType(l, ag.getType()); + } + + /** + * find any in list by type + */ + public static AbstractGoods findByType(List<AbstractGoods> l, GoodsType gt) { + if (l != null) + for (AbstractGoods ag : l) + if (ag.getType() == gt) + return ag; + + return null; + } + + public static AbstractGoods findByType(List<AbstractGoods> l, AbstractGoods ag) { + return findByType(l, ag.getType()); + } // Interface Named diff --git a/src/net/sf/freecol/common/model/BuildQueue.java b/src/net/sf/freecol/common/model/BuildQueue.java index 2614bcb29bb..b003844671e 100644 --- a/src/net/sf/freecol/common/model/BuildQueue.java +++ b/src/net/sf/freecol/common/model/BuildQueue.java @@ -169,7 +169,7 @@ public class BuildQueue<T extends BuildableType> implements Consumer { .getBoolean(GameOptions.SAVE_PRODUCTION_OVERFLOW); List<AbstractGoods> consumption = new ArrayList<>(); for (AbstractGoods ag : current.getRequiredGoodsList()) { - AbstractGoods available = find(input, AbstractGoods.matches(ag.getType())); + AbstractGoods available = AbstractGoods.findByType(input, ag); if (available != null && ag.getAmount() <= available.getAmount()) { int amount = (overflow || ag.getType().isStorable()) diff --git a/src/net/sf/freecol/common/model/Building.java b/src/net/sf/freecol/common/model/Building.java index b3774d3ffa9..aa6954a9085 100644 --- a/src/net/sf/freecol/common/model/Building.java +++ b/src/net/sf/freecol/common/model/Building.java @@ -473,7 +473,7 @@ public class Building extends WorkLocation return true; } else if (colony.getTotalProductionOf(goodsType) == 0 && (bt = colony.getCurrentlyBuilding()) != null - && any(bt.getRequiredGoods(), AbstractGoods.matches(goodsType))) { + && AbstractGoods.anyIsType(bt.getRequiredGoods(), goodsType)) { return true; } } diff --git a/src/net/sf/freecol/common/model/Colony.java b/src/net/sf/freecol/common/model/Colony.java index c9a05fd50cc..8d6851870b8 100644 --- a/src/net/sf/freecol/common/model/Colony.java +++ b/src/net/sf/freecol/common/model/Colony.java @@ -721,7 +721,7 @@ public class Colony extends Settlement implements Nameable, TradeLocation { */ public List<WorkLocation> getWorkLocationsForConsuming(GoodsType goodsType) { return transform(getCurrentWorkLocations(), - wl -> any(wl.getInputs(), AbstractGoods.matches(goodsType))); + wl -> AbstractGoods.anyIsType(wl.getInputs(), goodsType)); } /** @@ -733,7 +733,7 @@ public class Colony extends Settlement implements Nameable, TradeLocation { */ public List<WorkLocation> getWorkLocationsForProducing(GoodsType goodsType) { return transform(getCurrentWorkLocations(), - wl -> any(wl.getOutputs(), AbstractGoods.matches(goodsType))); + wl -> AbstractGoods.anyIsType(wl.getOutputs(), goodsType)); } /** @@ -904,8 +904,7 @@ public class Colony extends Settlement implements Nameable, TradeLocation { } int production = productionCache.getNetProductionOf(type); if (info != null) { - AbstractGoods consumption = find(info.getConsumption(), - AbstractGoods.matches(type)); + AbstractGoods consumption = AbstractGoods.findByType(info.getConsumption(), type); if (consumption != null) { // add the amount the build queue itself will consume production += consumption.getAmount(); diff --git a/src/net/sf/freecol/common/model/ProductionCache.java b/src/net/sf/freecol/common/model/ProductionCache.java index 6250481df56..38433c0329f 100644 --- a/src/net/sf/freecol/common/model/ProductionCache.java +++ b/src/net/sf/freecol/common/model/ProductionCache.java @@ -224,8 +224,7 @@ public class ProductionCache { public boolean isProducing(GoodsType goodsType) { update(); return any(productionAndConsumption.values(), - pi -> any(pi.getProduction(), - AbstractGoods.matches(goodsType))); + pi -> AbstractGoods.anyIsType(pi.getProduction(), goodsType)); } /** @@ -237,8 +236,7 @@ public class ProductionCache { public boolean isConsuming(GoodsType goodsType) { update(); return any(productionAndConsumption.values(), - pi -> any(pi.getConsumption(), - AbstractGoods.matches(goodsType))); + pi -> AbstractGoods.anyIsType(pi.getConsumption(), goodsType)); } /** diff --git a/src/net/sf/freecol/common/model/ProductionInfo.java b/src/net/sf/freecol/common/model/ProductionInfo.java index 0636cd50ac8..f330824da3c 100644 --- a/src/net/sf/freecol/common/model/ProductionInfo.java +++ b/src/net/sf/freecol/common/model/ProductionInfo.java @@ -92,8 +92,7 @@ public class ProductionInfo { */ public List<AbstractGoods> getProductionDeficit() { final Function<AbstractGoods, AbstractGoods> mapper = ag -> { - AbstractGoods agMax = find(this.maximumProduction, - AbstractGoods.matches(ag.getType())); + AbstractGoods agMax = AbstractGoods.findByType(this.maximumProduction, ag); int amount = (agMax == null) ? 0 : agMax.getAmount() - ag.getAmount(); return (amount <= 0) ? null @@ -112,8 +111,7 @@ public class ProductionInfo { */ public List<AbstractGoods> getConsumptionDeficit() { final Function<AbstractGoods, AbstractGoods> mapper = ag -> { - AbstractGoods agMax = find(this.maximumConsumption, - AbstractGoods.matches(ag.getType())); + AbstractGoods agMax = AbstractGoods.findByType(this.maximumConsumption, ag); int amount = (agMax == null) ? 0 : agMax.getAmount() - ag.getAmount(); return (amount == 0) ? null diff --git a/src/net/sf/freecol/common/model/ProductionMap.java b/src/net/sf/freecol/common/model/ProductionMap.java index 271830369f5..6ee70f33efe 100644 --- a/src/net/sf/freecol/common/model/ProductionMap.java +++ b/src/net/sf/freecol/common/model/ProductionMap.java @@ -82,7 +82,7 @@ public class ProductionMap { throw new IllegalArgumentException(goods.getType().getId() + " is not stored as " + root.getType()); } else { - AbstractGoods leaf = find(leafs, AbstractGoods.matches(goods.getType())); + AbstractGoods leaf = AbstractGoods.findByType(leafs, goods.getType()); if (leaf != null) { leaf.setAmount(leaf.getAmount() + goods.getAmount()); root.setAmount(root.getAmount() + goods.getAmount()); @@ -101,7 +101,7 @@ public class ProductionMap { leaf.setAmount(Math.min(leaf.getAmount(), root.getAmount())); } } else { - AbstractGoods leaf = find(leafs, AbstractGoods.matches(goods.getType())); + AbstractGoods leaf = AbstractGoods.findByType(leafs, goods.getType()); if (leaf != null) { leaf.setAmount(leaf.getAmount() - consumed); root.setAmount(root.getAmount() - consumed); @@ -114,7 +114,7 @@ public class ProductionMap { if (root.getType() == type) { return root; } else { - AbstractGoods leaf = find(leafs, AbstractGoods.matches(type)); + AbstractGoods leaf = AbstractGoods.findByType(leafs, type); if (leaf != null) { return new AbstractGoods(type, leaf.getAmount()); } diff --git a/src/net/sf/freecol/common/model/ProductionType.java b/src/net/sf/freecol/common/model/ProductionType.java index f6a769de9c0..a971b951d87 100644 --- a/src/net/sf/freecol/common/model/ProductionType.java +++ b/src/net/sf/freecol/common/model/ProductionType.java @@ -223,7 +223,7 @@ public class ProductionType extends FreeColSpecObject { */ public AbstractGoods getOutput(GoodsType goodsType) { return (outputs == null) ? null - : find(outputs, AbstractGoods.matches(goodsType)); + : AbstractGoods.findByType(outputs, goodsType); } /** diff --git a/src/net/sf/freecol/common/model/Role.java b/src/net/sf/freecol/common/model/Role.java index 164faf659b9..4eb33ed7e7b 100644 --- a/src/net/sf/freecol/common/model/Role.java +++ b/src/net/sf/freecol/common/model/Role.java @@ -366,7 +366,7 @@ public class Role extends BuildableType { } } result.addAll(transform(fromGoods, - ag -> !any(toGoods, AbstractGoods.matches(ag.getType())), + ag -> !AbstractGoods.anyIsType(toGoods, ag.getType()), ag -> new AbstractGoods(ag.getType(), -ag.getAmount()))); } return result; diff --git a/src/net/sf/freecol/common/model/TradeRouteStop.java b/src/net/sf/freecol/common/model/TradeRouteStop.java index 844fcb8254c..aeccb14ad33 100644 --- a/src/net/sf/freecol/common/model/TradeRouteStop.java +++ b/src/net/sf/freecol/common/model/TradeRouteStop.java @@ -164,7 +164,7 @@ public class TradeRouteStop extends FreeColGameObject implements TradeLocation { public List<AbstractGoods> getCompactCargo() { List<AbstractGoods> result = new ArrayList<>(); for (GoodsType type : getCargo()) { - AbstractGoods ag = find(result, AbstractGoods.matches(type)); + AbstractGoods ag = AbstractGoods.findByType(result, type); if (ag != null) { ag.setAmount(ag.getAmount() + GoodsContainer.CARGO_SIZE); } else { @@ -218,10 +218,11 @@ public class TradeRouteStop extends FreeColGameObject implements TradeLocation { // Look for goods to unload. // For all goods the unit has loaded, and if the type of goods // is not to be loaded here, and there is demand here, return true. - final Predicate<Goods> unloadPred = g -> - !any(stopGoods, AbstractGoods.matches(g.getType())) - && getImportAmount(g.getType(), turns) > 0; - if (any(unit.getCompactGoodsList(), unloadPred)) return true; + for (AbstractGoods g : stopGoods) { + GoodsType gt = g.getType(); + if (!AbstractGoods.anyIsType(stopGoods, gt) && getImportAmount(gt, turns) > 0) + return true; + } return false; // Otherwise no work here. } diff --git a/src/net/sf/freecol/common/model/WorkLocation.java b/src/net/sf/freecol/common/model/WorkLocation.java index c76c2f5a35c..40d69537f04 100644 --- a/src/net/sf/freecol/common/model/WorkLocation.java +++ b/src/net/sf/freecol/common/model/WorkLocation.java @@ -435,7 +435,7 @@ public abstract class WorkLocation extends UnitLocation * given {@code GoodsType}. */ public boolean produces(GoodsType goodsType) { - return any(getOutputs(), AbstractGoods.matches(goodsType)); + return AbstractGoods.anyIsType(getOutputs(), goodsType); } /** @@ -521,7 +521,7 @@ public abstract class WorkLocation extends UnitLocation if (info == null) return 0; List<AbstractGoods> production = info.getMaximumProduction(); if (production != null) { - AbstractGoods ag = find(production, AbstractGoods.matches(goodsType)); + AbstractGoods ag = AbstractGoods.findByType(production, goodsType); if (ag != null) return ag.getAmount(); } return getTotalProductionOf(goodsType); @@ -650,7 +650,7 @@ public abstract class WorkLocation extends UnitLocation public AbstractGoods getProductionDeficit(GoodsType goodsType) { ProductionInfo pi = getProductionInfo(); return (pi == null) ? null - : find(pi.getProductionDeficit(), AbstractGoods.matches(goodsType)); + : AbstractGoods.findByType(pi.getProductionDeficit(), goodsType); } diff --git a/src/net/sf/freecol/server/model/ServerColony.java b/src/net/sf/freecol/server/model/ServerColony.java index 834c8b7c6ae..5c047e3c631 100644 --- a/src/net/sf/freecol/server/model/ServerColony.java +++ b/src/net/sf/freecol/server/model/ServerColony.java @@ -135,11 +135,16 @@ public class ServerColony extends Colony implements ServerModelObject { */ private boolean neededForBuildableType(GoodsType goodsType) { final Specification spec = getSpecification(); - List<BuildableType> buildables = new ArrayList<>(); - buildables.addAll(spec.getBuildingTypeList()); - buildables.addAll(spec.getUnitTypesWithoutAbility(Ability.PERSON)); - return any(buildables, bt -> canBuild(bt) - && any(bt.getRequiredGoods(), AbstractGoods.matches(goodsType))); + + for (BuildableType bt : spec.getBuildingTypeList()) + if (canBuild(bt) && AbstractGoods.anyIsType(bt.getRequiredGoods(), goodsType)) + return true; + + for (BuildableType bt : spec.getUnitTypesWithoutAbility(Ability.PERSON)) + if (canBuild(bt) && AbstractGoods.anyIsType(bt.getRequiredGoods(), goodsType)) + return true; + + return false; } /** diff --git a/test/src/net/sf/freecol/server/model/ServerColonyTest.java b/test/src/net/sf/freecol/server/model/ServerColonyTest.java index e4c82f9f04e..b689d5a459b 100644 --- a/test/src/net/sf/freecol/server/model/ServerColonyTest.java +++ b/test/src/net/sf/freecol/server/model/ServerColonyTest.java @@ -121,8 +121,7 @@ public class ServerColonyTest extends FreeColTestCase { // Set the food production of the center tile of the colony to 2 // This will be the only food production of the colony AbstractGoods foodGoods - = first(transform(colonyTile.getType().getPossibleProduction(true), - AbstractGoods.matches(foodGoodsType))); + = AbstractGoods.findByType(colonyTile.getType().getPossibleProduction(true),foodGoodsType); if (foodGoods != null) foodGoods.setAmount(2); Unit unit = colony.getUnitList().get(0); -- 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 Freecol-developers@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/freecol-developers