MATSIM
AbstractMobsimModule.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * AbstractMobsimModule.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.framework;
23 
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Objects;
27 
28 import org.matsim.core.config.Config;
29 
30 import com.google.inject.AbstractModule;
31 import com.google.inject.Module;
32 import com.google.inject.util.Modules;
33 
34 public abstract class AbstractMobsimModule extends AbstractModule {
35  private Config config = null;
36  private Integer iterationNumber = null;
37  private AbstractMobsimModule parent = null;
38 
39  public final void setConfig(Config config) {
40  this.config = Objects.requireNonNull(config);
41  }
42 
43  public final void setIterationNumber(int iterationNumber) {
44  this.iterationNumber = iterationNumber;
45  }
46 
47  public final void setParent(AbstractMobsimModule parent) {
48  this.parent = Objects.requireNonNull(parent);
49  }
50 
51  protected final Config getConfig() {
52  if (config != null) {
53  return config;
54  }
55 
56  if (parent != null) {
57  return parent.getConfig();
58  }
59 
60  throw new IllegalStateException(
61  "No config set. Did you try to use the module outside of the QSim initialization process?");
62  }
63 
64  protected final int getIterationNumber() {
65  if (iterationNumber != null) {
66  return iterationNumber;
67  }
68 
69  if (parent != null) {
70  return parent.getIterationNumber();
71  }
72 
73  throw new IllegalStateException(
74  "No iteration number set. Did you try to use the module outside of the QSim initialization process?");
75  }
76 
77  protected final void configure() {
79  }
80 
81  protected abstract void configureMobsim();
82 
83  public static AbstractMobsimModule overrideMobsimModules(Collection<AbstractMobsimModule> base,
84  List<AbstractMobsimModule> overrides) {
85  Module composite = Modules.override(base).with(overrides);
86 
88  @Override
89  protected void configureMobsim() {
90  install(composite);
91  }
92  };
93 
94  base.forEach(m -> m.setParent(wrapper));
95  overrides.forEach(m -> m.setParent(wrapper));
96 
97  return wrapper;
98  }
99 }
static AbstractMobsimModule overrideMobsimModules(Collection< AbstractMobsimModule > base, List< AbstractMobsimModule > overrides)
final void setParent(AbstractMobsimModule parent)