From e92859d8f430383fa2401ff1431a204649c32aa8 Mon Sep 17 00:00:00 2001 From: kainagel Date: Tue, 27 Aug 2019 14:08:48 +0200 Subject: [PATCH 1/2] allow addModule method that will not allow a second override of the same interface --- .../org/matsim/core/controler/Controler.java | 24 ++++- .../MultipleOverridesOfSameBinding.java | 94 +++++++++++++++++++ 2 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBinding.java diff --git a/matsim/src/main/java/org/matsim/core/controler/Controler.java b/matsim/src/main/java/org/matsim/core/controler/Controler.java index ed52385a0a0..6a2645e6289 100644 --- a/matsim/src/main/java/org/matsim/core/controler/Controler.java +++ b/matsim/src/main/java/org/matsim/core/controler/Controler.java @@ -120,6 +120,8 @@ public IterationStopWatch getStopwatch() { private List overridingQSimModules = new LinkedList<>(); + private List modulesToAdd = new LinkedList<>() ; + public static void main(final String[] args) { if ((args == null) || (args.length == 0)) { System.out.println("No argument given!"); @@ -231,9 +233,20 @@ public void install(){ // should not be necessary: created in the controler //install(new ScenarioByInstanceModule(scenario)); } - } - ); - this.injector = Injector.createInjector( config, AbstractModule.override( standardModules, overrides ) ); + }) ; + + AbstractModule modulesThatShouldNotOverrideEachOther = new AbstractModule(){ + @Override public void install(){ + for( AbstractModule module : modulesToAdd ){ + install( module ); + } + } + };; + + Set combinedStandardModule = Collections.singleton( + AbstractModule.override( standardModules , modulesThatShouldNotOverrideEachOther ) );; + + this.injector = Injector.createInjector( config, AbstractModule.override( combinedStandardModule, overrides ) ); ControlerI controler = injector.getInstance(ControlerI.class); controler.run(); } @@ -464,6 +477,11 @@ public final Controler addOverridingModule( AbstractModule abstractModule ) { return this ; } + public final Controler addModule( AbstractModule abstractModule ) { + this.modulesToAdd.add( abstractModule ) ; + return this ; + } + public final void setModules(AbstractModule... modules) { if (this.injectorCreated) { throw new RuntimeException("Too late for configuring the Controler. This can only be done before calling run."); diff --git a/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBinding.java b/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBinding.java new file mode 100644 index 00000000000..2b0e62d5ca9 --- /dev/null +++ b/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBinding.java @@ -0,0 +1,94 @@ +package org.matsim.core.controler; + +import com.google.inject.CreationException; +import org.apache.log4j.Logger; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.matsim.api.core.v01.Scenario; +import org.matsim.core.config.Config; +import org.matsim.core.config.ConfigUtils; +import org.matsim.core.mobsim.framework.Mobsim; +import org.matsim.core.scenario.ScenarioUtils; +import org.matsim.testcases.MatsimTestUtils; + +public class MultipleOverridesOfSameBinding{ + private static final Logger log = Logger.getLogger( MultipleOverridesOfSameBinding.class ) ; + + @Rule public MatsimTestUtils utils = new MatsimTestUtils() ; + + private static int mobsimVersion = 0 ; + + @Test + public void testSecondOverridesFirst() { + Controler controler = getControler(); + + controler.addOverridingModule( new Mobsim1Module() ) ; + controler.addOverridingModule( new Mobsim2Module() ) ; + + controler.run() ; + + Assert.assertEquals( 2, mobsimVersion ); + + } + @Test + public void testInstallDoesNotOverride() { + Controler controler = getControler(); + + try{ + controler.addOverridingModule( new AbstractModule(){ + @Override public void install(){ + install( new Mobsim1Module() ); + install( new Mobsim2Module() ); + } + } ); + } catch ( Exception ee ) { + Assert.assertTrue( ee instanceof CreationException ); + } + + } + @Test + public void testNewAddModueMethod() { + Controler controler = getControler(); + + try{ + controler.addModule( new Mobsim1Module() ); + controler.addModule( new Mobsim2Module() ); + } catch ( Exception ee ) { + Assert.assertTrue( ee instanceof CreationException ); + } + } + + private Controler getControler(){ + mobsimVersion = 0 ; + + Config config = ConfigUtils.createConfig() ; + config.controler().setOutputDirectory( utils.getOutputDirectory() ); + config.controler().setLastIteration( 0 ); + + Scenario scenario = ScenarioUtils.loadScenario( config ) ; + + return new Controler( scenario ); + } + + private static class Mobsim1Module extends AbstractModule{ + @Override public void install(){ + this.bindMobsim().toInstance( new Mobsim(){ + @Override public void run(){ + log.warn("running mobsim1") ; + mobsimVersion = 1 ; + } + } ); + } + } + private static class Mobsim2Module extends AbstractModule{ + @Override public void install(){ + this.bindMobsim().toInstance( new Mobsim(){ + @Override public void run(){ + log.warn("running mobsim2") ; + mobsimVersion = 2 ; + } + } ); + } + } +} From fc4069b9fb0048f518bb22934dc8a99c53ff90c9 Mon Sep 17 00:00:00 2001 From: Kai Nagel Date: Tue, 3 Jan 2023 19:09:35 +0100 Subject: [PATCH 2/2] make "addOverridingModule" accept only the overriding of the initial default, and have instead an "addFullyOverridingModule" to reflect the previous behavior. --- .../org/matsim/core/controler/Controler.java | 9 ++++++--- ... => MultipleOverridesOfSameBindingTest.java} | 17 +++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) rename matsim/src/test/java/org/matsim/core/controler/{MultipleOverridesOfSameBinding.java => MultipleOverridesOfSameBindingTest.java} (79%) diff --git a/matsim/src/main/java/org/matsim/core/controler/Controler.java b/matsim/src/main/java/org/matsim/core/controler/Controler.java index 1a7a66d6b4f..c876db024de 100755 --- a/matsim/src/main/java/org/matsim/core/controler/Controler.java +++ b/matsim/src/main/java/org/matsim/core/controler/Controler.java @@ -470,12 +470,15 @@ public final Controler addOverridingModule( AbstractModule abstractModule ) { if (this.injectorCreated) { throw new RuntimeException("Too late for configuring the Controler. This can only be done before calling run."); } - this.overrides = AbstractModule.override(Collections.singletonList(this.overrides), abstractModule); + this.modulesToAdd.add( abstractModule ) ; return this ; } - public final Controler addModule( AbstractModule abstractModule ) { - this.modulesToAdd.add( abstractModule ) ; + public final Controler addFullyOverridingModule( AbstractModule abstractModule ) { + if (this.injectorCreated) { + throw new RuntimeException("Too late for configuring the Controler. This can only be done before calling run."); + } + this.overrides = AbstractModule.override(Collections.singletonList(this.overrides), abstractModule); return this ; } diff --git a/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBinding.java b/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBindingTest.java similarity index 79% rename from matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBinding.java rename to matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBindingTest.java index 2b0e62d5ca9..f18643d472e 100644 --- a/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBinding.java +++ b/matsim/src/test/java/org/matsim/core/controler/MultipleOverridesOfSameBindingTest.java @@ -1,7 +1,8 @@ package org.matsim.core.controler; import com.google.inject.CreationException; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -12,8 +13,8 @@ import org.matsim.core.scenario.ScenarioUtils; import org.matsim.testcases.MatsimTestUtils; -public class MultipleOverridesOfSameBinding{ - private static final Logger log = Logger.getLogger( MultipleOverridesOfSameBinding.class ) ; +public class MultipleOverridesOfSameBindingTest { + private static final Logger log = LogManager.getLogger( MultipleOverridesOfSameBindingTest.class ) ; @Rule public MatsimTestUtils utils = new MatsimTestUtils() ; @@ -23,8 +24,8 @@ public class MultipleOverridesOfSameBinding{ public void testSecondOverridesFirst() { Controler controler = getControler(); - controler.addOverridingModule( new Mobsim1Module() ) ; - controler.addOverridingModule( new Mobsim2Module() ) ; + controler.addFullyOverridingModule( new Mobsim1Module() ) ; + controler.addFullyOverridingModule( new Mobsim2Module() ) ; controler.run() ; @@ -36,7 +37,7 @@ public void testInstallDoesNotOverride() { Controler controler = getControler(); try{ - controler.addOverridingModule( new AbstractModule(){ + controler.addFullyOverridingModule( new AbstractModule(){ @Override public void install(){ install( new Mobsim1Module() ); install( new Mobsim2Module() ); @@ -52,8 +53,8 @@ public void testNewAddModueMethod() { Controler controler = getControler(); try{ - controler.addModule( new Mobsim1Module() ); - controler.addModule( new Mobsim2Module() ); + controler.addOverridingModule( new Mobsim1Module() ); + controler.addOverridingModule( new Mobsim2Module() ); } catch ( Exception ee ) { Assert.assertTrue( ee instanceof CreationException ); }