From 22c205810a9c9aaa8508ce2066eb51dd11c79e82 Mon Sep 17 00:00:00 2001 From: Kai Nagel Date: Wed, 15 Nov 2023 10:59:47 +0100 Subject: [PATCH] reduce public footprint (more than will be useful in long run, but ...); make (non-polymorphic) factory methods static --- .../ExampleGroceryDeliveryMultipleChains.java | 4 ++-- .../ExampleMultipleMixedEchelonChains.java | 2 +- .../ExampleMultipleOneEchelonChains.java | 2 +- .../ExampleMultipleOneEchelonChainsReplanning.java | 4 ++-- .../ExampleMultipleTwoEchelonChainsReplanning.java | 6 +++--- ...lector.java => LSPWorstPlanForRemovalSelector.java} | 2 +- .../lsp/multipleChains/MultipleChainsUtils.java | 4 +++- .../lsp/multipleChains/ProximityStrategyFactory.java | 10 +++------- .../RandomDistributionAllShipmentsStrategyFactory.java | 9 +++++---- .../multipleChains/RandomShiftingStrategyFactory.java | 7 ++++--- .../lsp/multipleChains/RebalancingStrategyFactory.java | 9 ++++++--- ...ndRobinDistributionAllShipmentsStrategyFactory.java | 3 +-- .../RoundRobinLogisticChainShipmentAssigner.java | 4 ++-- .../multipleChains/MultipleChainsReplanningTest.java | 2 +- .../lsp/multipleChains/WorstPlanSelectorTest.java | 4 ++-- 15 files changed, 37 insertions(+), 35 deletions(-) rename src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/{WorstPlanForRemovalSelector.java => LSPWorstPlanForRemovalSelector.java} (95%) diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleGroceryDeliveryMultipleChains.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleGroceryDeliveryMultipleChains.java index 2b8305d2..d344390f 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleGroceryDeliveryMultipleChains.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleGroceryDeliveryMultipleChains.java @@ -34,7 +34,7 @@ import static org.matsim.freight.logistics.example.lsp.multipleChains.MultipleChainsUtils.createLSPShipmentsFromCarrierShipments; -public class ExampleGroceryDeliveryMultipleChains { +final class ExampleGroceryDeliveryMultipleChains { private static final Logger log = LogManager.getLogger(ExampleGroceryDeliveryMultipleChains.class); static double HUBCOSTS_FIX = 100; @@ -78,7 +78,7 @@ public void install() { // strategyManager.addStrategy(new RandomShiftingStrategyFactory().createStrategy(), null, 1); // strategyManager.addStrategy(new ProximityStrategyFactory(scenario.getNetwork()).createStrategy(), null, 1); // strategyManager.setMaxPlansPerAgent(5); - strategyManager.setPlanSelectorForRemoval(new WorstPlanForRemovalSelector()); + strategyManager.setPlanSelectorForRemoval(new LSPWorstPlanForRemovalSelector() ); return strategyManager; }); } diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleMixedEchelonChains.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleMixedEchelonChains.java index d6c7cc59..94e4ff27 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleMixedEchelonChains.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleMixedEchelonChains.java @@ -33,7 +33,7 @@ import java.util.*; -public class ExampleMultipleMixedEchelonChains { +final class ExampleMultipleMixedEchelonChains { private static final Logger log = LogManager.getLogger(ExampleMultipleMixedEchelonChains.class); diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChains.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChains.java index c6f65945..61f3ccae 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChains.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChains.java @@ -33,7 +33,7 @@ import java.util.Collections; import java.util.List; -public class ExampleMultipleOneEchelonChains { +final class ExampleMultipleOneEchelonChains { private static final Logger log = LogManager.getLogger(ExampleMultipleOneEchelonChains.class); diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChainsReplanning.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChainsReplanning.java index bb023f9c..eafeab53 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChainsReplanning.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChainsReplanning.java @@ -35,7 +35,7 @@ import java.util.Collections; import java.util.List; -public class ExampleMultipleOneEchelonChainsReplanning { +final class ExampleMultipleOneEchelonChainsReplanning { private static final Logger log = LogManager.getLogger(ExampleMultipleOneEchelonChainsReplanning.class); @@ -96,7 +96,7 @@ public void install() { // strategyManager.addStrategy(new RandomShiftingStrategyFactory().createStrategy(), null, 1); // strategyManager.addStrategy(new ProximityStrategyFactory(scenario.getNetwork()).createStrategy(), null, 1); strategyManager.setMaxPlansPerAgent(5); - strategyManager.setPlanSelectorForRemoval(new WorstPlanForRemovalSelector()); + strategyManager.setPlanSelectorForRemoval(new LSPWorstPlanForRemovalSelector() ); return strategyManager; }); } diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java index a312b2f4..ee7c8396 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java @@ -37,7 +37,7 @@ import java.util.Collections; import java.util.List; -public class ExampleMultipleTwoEchelonChainsReplanning { +final class ExampleMultipleTwoEchelonChainsReplanning { private static final Logger log = LogManager.getLogger(ExampleMultipleTwoEchelonChainsReplanning.class); @@ -96,9 +96,9 @@ public void install() { bind(LSPStrategyManager.class).toProvider(() -> { LSPStrategyManager strategyManager = new LSPStrategyManagerImpl(); strategyManager.addStrategy(new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())), null, 1); - strategyManager.addStrategy(new ProximityStrategyFactory(scenario.getNetwork()).createStrategy(), null, 1); + strategyManager.addStrategy( ProximityStrategyFactory.createStrategy( scenario.getNetwork() ), null, 1); strategyManager.setMaxPlansPerAgent(5); - strategyManager.setPlanSelectorForRemoval(new WorstPlanForRemovalSelector()); + strategyManager.setPlanSelectorForRemoval(new LSPWorstPlanForRemovalSelector() ); return strategyManager; }); } diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanForRemovalSelector.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java similarity index 95% rename from src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanForRemovalSelector.java rename to src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java index bd3defcd..04c8726d 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanForRemovalSelector.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java @@ -8,7 +8,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class WorstPlanForRemovalSelector implements PlanSelector { +class LSPWorstPlanForRemovalSelector implements PlanSelector { private static final String UNDEFINED_TYPE = "undefined"; diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/MultipleChainsUtils.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/MultipleChainsUtils.java index b60909ca..29bbd78a 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/MultipleChainsUtils.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/MultipleChainsUtils.java @@ -14,7 +14,9 @@ import static java.util.stream.Collectors.toMap; -public class MultipleChainsUtils { +class MultipleChainsUtils { + private MultipleChainsUtils(){ + } public static RandomLogisticChainShipmentAssigner createRandomLogisticChainShipmentAssigner() { return new RandomLogisticChainShipmentAssigner(); } diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ProximityStrategyFactory.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ProximityStrategyFactory.java index 3fbba335..ecb3411d 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ProximityStrategyFactory.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ProximityStrategyFactory.java @@ -18,15 +18,11 @@ import java.util.HashMap; import java.util.Map; -class ProximityStrategyFactory { +final class ProximityStrategyFactory { - private final Network network; + private ProximityStrategyFactory() { } // class contains only static methods; do not instantiate - ProximityStrategyFactory(Network network) { - this.network = network; - } - - GenericPlanStrategy createStrategy() { + static GenericPlanStrategy createStrategy( Network network ) { GenericPlanStrategyImpl strategy = new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())); GenericPlanStrategyModule randomModule = new GenericPlanStrategyModule<>() { diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RandomDistributionAllShipmentsStrategyFactory.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RandomDistributionAllShipmentsStrategyFactory.java index e22fc7ab..144192e6 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RandomDistributionAllShipmentsStrategyFactory.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RandomDistributionAllShipmentsStrategyFactory.java @@ -15,12 +15,13 @@ import java.util.ArrayList; import java.util.List; -class RandomDistributionAllShipmentsStrategyFactory { +final class RandomDistributionAllShipmentsStrategyFactory { - RandomDistributionAllShipmentsStrategyFactory() { - } + private RandomDistributionAllShipmentsStrategyFactory() { } // do not instantiate - GenericPlanStrategy createStrategy() { + static GenericPlanStrategy createStrategy() { + // yyyy using factory method instead of constructor is a universally accepted approach. but should be static: + // Please refactor. Thanks! kai, nov'23 GenericPlanStrategyImpl strategy = new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())); GenericPlanStrategyModule randomModule = new GenericPlanStrategyModule<>() { diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RandomShiftingStrategyFactory.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RandomShiftingStrategyFactory.java index 9c3ccd86..cc16305e 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RandomShiftingStrategyFactory.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RandomShiftingStrategyFactory.java @@ -18,10 +18,11 @@ class RandomShiftingStrategyFactory { - RandomShiftingStrategyFactory() { - } + private RandomShiftingStrategyFactory() {} // class contains only static methods; do not instantiate. - GenericPlanStrategy createStrategy() { + static GenericPlanStrategy createStrategy() { + // yyyy using factory method instead of constructor is a universally accepted approach. but should be static. + // Please refactor. Thanks! kai, nov'23 GenericPlanStrategyImpl strategy = new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())); GenericPlanStrategyModule randomModule = new GenericPlanStrategyModule<>() { diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RebalancingStrategyFactory.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RebalancingStrategyFactory.java index e27cd7a6..15d9bb8f 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RebalancingStrategyFactory.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RebalancingStrategyFactory.java @@ -19,10 +19,13 @@ class RebalancingStrategyFactory { - RebalancingStrategyFactory() { - } + private RebalancingStrategyFactory() { } // class contains only static methods; do not instantiate + + static GenericPlanStrategy createStrategy() { + // yyyy using factory method instead of constructor is a universally accepted approach. but should be static. + // Please refactor. Thanks! kai, nov'23 + - GenericPlanStrategy createStrategy() { GenericPlanStrategyImpl strategy = new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())); GenericPlanStrategyModule loadBalancingModule = new GenericPlanStrategyModule<>() { diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RoundRobinDistributionAllShipmentsStrategyFactory.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RoundRobinDistributionAllShipmentsStrategyFactory.java index bb30b34a..97bbb50f 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RoundRobinDistributionAllShipmentsStrategyFactory.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RoundRobinDistributionAllShipmentsStrategyFactory.java @@ -37,8 +37,7 @@ /*package-private*/ class RoundRobinDistributionAllShipmentsStrategyFactory { - /*package-private*/ RoundRobinDistributionAllShipmentsStrategyFactory() { - } + private RoundRobinDistributionAllShipmentsStrategyFactory() { } // class contains only static methods; do not instantiate /*package-private*/ GenericPlanStrategy createStrategy() { GenericPlanStrategyImpl strategy = new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())); diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RoundRobinLogisticChainShipmentAssigner.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RoundRobinLogisticChainShipmentAssigner.java index c1399528..7d8d0cf4 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RoundRobinLogisticChainShipmentAssigner.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/RoundRobinLogisticChainShipmentAssigner.java @@ -19,7 +19,7 @@ * Requirements: There must be at least one logisticChain in the plan */ -public class RoundRobinLogisticChainShipmentAssigner implements ShipmentAssigner { +class RoundRobinLogisticChainShipmentAssigner implements ShipmentAssigner { private LSP lsp; @@ -54,4 +54,4 @@ public void assignToPlan(LSPPlan lspPlan, LSPShipment shipment) { shipmentCountByChain.merge(minChain, 1, Integer::sum); } -} \ No newline at end of file +} diff --git a/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/MultipleChainsReplanningTest.java b/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/MultipleChainsReplanningTest.java index dc0fbb44..4eee3d5f 100644 --- a/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/MultipleChainsReplanningTest.java +++ b/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/MultipleChainsReplanningTest.java @@ -93,7 +93,7 @@ public void install() { }); bind(LSPStrategyManager.class).toProvider(() -> { LSPStrategyManager strategyManager = new LSPStrategyManagerImpl(); - strategyManager.addStrategy(new RandomShiftingStrategyFactory().createStrategy(), null, 1); + strategyManager.addStrategy( RandomShiftingStrategyFactory.createStrategy(), null, 1); return strategyManager; }); } diff --git a/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanSelectorTest.java b/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanSelectorTest.java index 5289d416..5250b1cc 100644 --- a/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanSelectorTest.java +++ b/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanSelectorTest.java @@ -93,9 +93,9 @@ public void install() { bind(LSPStrategyManager.class).toProvider(() -> { LSPStrategyManager strategyManager = new LSPStrategyManagerImpl(); strategyManager.addStrategy(new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())), null, 1); - strategyManager.addStrategy(new RandomDistributionAllShipmentsStrategyFactory().createStrategy(), null, 1); + strategyManager.addStrategy( RandomDistributionAllShipmentsStrategyFactory.createStrategy(), null, 1); strategyManager.setMaxPlansPerAgent(2); - strategyManager.setPlanSelectorForRemoval(new WorstPlanForRemovalSelector()); + strategyManager.setPlanSelectorForRemoval(new LSPWorstPlanForRemovalSelector() ); return strategyManager; }); }