Skip to content

Commit

Permalink
Merge pull request #3273 from matsim-org/guice-clean-up
Browse files Browse the repository at this point in the history
inline abstract module attributes and get rid of them
  • Loading branch information
paulheinr authored May 16, 2024
2 parents 1a75b4d + a9cdc5b commit 47369c7
Showing 1 changed file with 20 additions and 33 deletions.
53 changes: 20 additions & 33 deletions matsim/src/main/java/org/matsim/core/controler/AbstractModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,10 @@
public abstract class AbstractModule implements Module {

private Binder binder;
private Multibinder<EventHandler> eventHandlerMultibinder;
private Multibinder<ControlerListener> controlerListenerMultibinder;

/**
* Contents retrieved (I think) by injected method QSim#addQueueSimulationListeners(...). Is not public, and therefore cannot be referenced from here.
* <br/>
* I think that that method will be called every time the mobsim will be constructed. If the injected classes are singletons, they will
* presumably be re-used, otherwise they will be newly constructed.
*/
private Multibinder<MobsimListener> mobsimListenerMultibinder;

private Multibinder<SnapshotWriter> snapshotWriterMultibinder;
private MapBinder<Class<?>, AttributeConverter<?>> attributeConverterMapBinder;
private Multibinder<AbstractQSimModule> qsimModulesMultibinder;

@Inject
com.google.inject.Injector bootstrapInjector;
private Config config;
private Multibinder<AbstractQSimModule> qsimOverridingModulesMultibinder;

public AbstractModule() {
// config will be injected later
Expand All @@ -121,17 +106,13 @@ public final void configure(Binder binder) {
private void initializeMultibinders() {
// We do need to make these calls here in order to register the multi binders. Otherwise, guice doesn't know, that they exist. In particular,
// if none of the corresponding addXXXBinding methods was called, the set binder would not be registered, and guice would complain.
this.mobsimListenerMultibinder = Multibinder.newSetBinder(this.binder, MobsimListener.class);
this.snapshotWriterMultibinder = Multibinder.newSetBinder(this.binder, SnapshotWriter.class);
this.eventHandlerMultibinder = Multibinder.newSetBinder(this.binder, EventHandler.class);
this.controlerListenerMultibinder = Multibinder.newSetBinder(this.binder, ControlerListener.class);
this.attributeConverterMapBinder =
MapBinder.newMapBinder(
this.binder,
new TypeLiteral<Class<?>>(){},
new TypeLiteral<AttributeConverter<?>>() {} );
this.qsimModulesMultibinder = Multibinder.newSetBinder(this.binder, AbstractQSimModule.class);
this.qsimOverridingModulesMultibinder = Multibinder.newSetBinder( this.binder, AbstractQSimModule.class, Names.named( "overridesFromAbstractModule" ) );
Multibinder.newSetBinder(this.binder, MobsimListener.class);
Multibinder.newSetBinder(this.binder, SnapshotWriter.class);
Multibinder.newSetBinder(this.binder, EventHandler.class);
Multibinder.newSetBinder(this.binder, ControlerListener.class);
MapBinder.newMapBinder(this.binder, new TypeLiteral<Class<?>>(){}, new TypeLiteral<AttributeConverter<?>>() {} );
Multibinder.newSetBinder(this.binder, AbstractQSimModule.class);
Multibinder.newSetBinder( this.binder, AbstractQSimModule.class, Names.named( "overridesFromAbstractModule" ) );
}

public abstract void install();
Expand All @@ -146,21 +127,21 @@ protected final void install(Module module) {
}

protected final LinkedBindingBuilder<EventHandler> addEventHandlerBinding() {
return eventHandlerMultibinder.addBinding();
return Multibinder.newSetBinder(this.binder, EventHandler.class).addBinding();
}

protected final void installQSimModule(AbstractQSimModule qsimModule) {
qsimModulesMultibinder.addBinding().toInstance(qsimModule);
Multibinder.newSetBinder(this.binder, AbstractQSimModule.class).addBinding().toInstance(qsimModule);
}
protected final void installOverridingQSimModule(AbstractQSimModule qsimModule) {
qsimOverridingModulesMultibinder.addBinding().toInstance(qsimModule);
Multibinder.newSetBinder( this.binder, AbstractQSimModule.class, Names.named( "overridesFromAbstractModule" ) ).addBinding().toInstance(qsimModule);
}

/**
* @see ControlerListener
*/
protected final LinkedBindingBuilder<ControlerListener> addControlerListenerBinding() {
return controlerListenerMultibinder.addBinding();
return Multibinder.newSetBinder(this.binder, ControlerListener.class).addBinding();
}

/**
Expand All @@ -182,16 +163,22 @@ protected final com.google.inject.binder.LinkedBindingBuilder<ScoringFunctionFac
return bind(ScoringFunctionFactory.class);
}

/**
* Contents retrieved (I think) by injected method QSim#addQueueSimulationListeners(...). Is not public, and therefore cannot be referenced from here.
* <br/>
* I think that that method will be called every time the mobsim will be constructed. If the injected classes are singletons, they will
* presumably be re-used, otherwise they will be newly constructed.
*/
protected final com.google.inject.binder.LinkedBindingBuilder<MobsimListener> addMobsimListenerBinding() {
return mobsimListenerMultibinder.addBinding();
return Multibinder.newSetBinder(this.binder, MobsimListener.class).addBinding();
}

protected final com.google.inject.binder.LinkedBindingBuilder<SnapshotWriter> addSnapshotWriterBinding() {
return snapshotWriterMultibinder.addBinding();
return Multibinder.newSetBinder(this.binder, SnapshotWriter.class).addBinding();
}

protected final LinkedBindingBuilder<AttributeConverter<?>> addAttributeConverterBinding(final Class<?> clazz ) {
return attributeConverterMapBinder.addBinding( clazz );
return MapBinder.newMapBinder(this.binder, new TypeLiteral<Class<?>>(){}, new TypeLiteral<AttributeConverter<?>>() {} ).addBinding( clazz );
}
/**
* @deprecated better use {@link #addTravelDisutilityFactoryBinding(String)}.
Expand Down

0 comments on commit 47369c7

Please sign in to comment.