MATSIM
AbstractQSimModule.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * AbstractQSimModule.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22 package org.matsim.core.mobsim.qsim;
23 
24 import java.lang.annotation.Annotation;
25 import java.util.Collection;
26 import java.util.List;
27 
31 
32 import com.google.inject.Module;
33 import com.google.inject.binder.LinkedBindingBuilder;
34 import com.google.inject.multibindings.Multibinder;
35 import com.google.inject.name.Names;
36 import com.google.inject.util.Modules;
38 
39 public abstract class AbstractQSimModule extends AbstractMobsimModule{
40  @Override
41  protected final void configureMobsim(){
42  configureQSim();
43  }
44 
45  @Deprecated // for experts only
46  protected final LinkedBindingBuilder<QSimComponent> addQSimComponentBinding( Annotation annotation ){
47  Multibinder<QSimComponent> multibinder = Multibinder.newSetBinder( binder(), QSimComponent.class, annotation );
48  multibinder.permitDuplicates();
49  return multibinder.addBinding();
50  }
51 
52  @Deprecated // for experts only
53  protected LinkedBindingBuilder<QSimComponent> addQSimComponentBinding( Class<? extends Annotation> annotationClass ){
54  Multibinder<QSimComponent> multibinder = Multibinder.newSetBinder( binder(), QSimComponent.class, annotationClass );
55  multibinder.permitDuplicates();
56  return multibinder.addBinding();
57  }
58 
59  protected LinkedBindingBuilder<QSimComponent> addQSimComponentBinding( String name ){
60  return addQSimComponentBinding( Names.named( name ) );
61  }
62 
63  // Use methods above
64  @Deprecated
65  protected <T extends QSimComponent> void addNamedComponent( Class<T> componentClass, String name ){
66  addQSimComponentBinding( name ).to( componentClass );
67  }
68 
69  protected LinkedBindingBuilder<MobsimScopeEventHandler> addMobsimScopeEventHandlerBinding(){
70  return Multibinder.newSetBinder( binder(), MobsimScopeEventHandler.class ).addBinding();
71  }
72 
73  protected abstract void configureQSim();
74 
75  protected void install( AbstractQSimModule module ){
76  module.setParent( this );
77  super.install( module );
78  }
79 
80  public static AbstractQSimModule overrideQSimModules( Collection<AbstractQSimModule> base,
81  List<AbstractQSimModule> overrides ){
82  Module composite = Modules.override( base ).with( overrides );
83 
84  AbstractQSimModule wrapper = new AbstractQSimModule(){
85  @Override
86  protected void configureQSim(){
87  install( composite );
88  }
89  };
90 
91  base.forEach( m -> m.setParent( wrapper ) );
92  overrides.forEach( m -> m.setParent( wrapper ) );
93 
94  return wrapper;
95  }
96 
97  // the methods below are strictly speaking not necessary. But help with detectability. A bit similar to the general matsim AbstractModule.
98 
108  protected LinkedBindingBuilder<LinkSpeedCalculator> addLinkSpeedCalculatorBinding(){
109  return Multibinder.newSetBinder( this.binder(), LinkSpeedCalculator.class ).addBinding();
110  }
111 }
static AbstractQSimModule overrideQSimModules(Collection< AbstractQSimModule > base, List< AbstractQSimModule > overrides)
LinkedBindingBuilder< MobsimScopeEventHandler > addMobsimScopeEventHandlerBinding()
final LinkedBindingBuilder< QSimComponent > addQSimComponentBinding(Annotation annotation)
LinkedBindingBuilder< QSimComponent > addQSimComponentBinding(String name)
final void setParent(AbstractMobsimModule parent)
LinkedBindingBuilder< LinkSpeedCalculator > addLinkSpeedCalculatorBinding()
LinkedBindingBuilder< QSimComponent > addQSimComponentBinding(Class<? extends Annotation > annotationClass)