MATSIM
QSimComponentsConfig.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * QSimComponentsConfig.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.components;
23 
24 import java.lang.annotation.Annotation;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Set;
30 
31 import com.google.inject.Key;
32 import com.google.inject.name.Names;
33 
43 final public class QSimComponentsConfig {
44  private final List<Object> components = new LinkedList<>();
45  private final Set<Key<?>> keys = new HashSet<>();
46 
47 
51  @Deprecated
52  public void addComponent(Class<? extends Annotation> annotation) {
53  addComponent(Key.get(Object.class, annotation));
54  components.add(annotation);
55  }
56 
60  @Deprecated
61  public void addComponent(Annotation annotation) {
62  addComponent(Key.get(Object.class, annotation));
63  components.add(annotation);
64  }
65 
66  private void addComponent(Key<?> componentKey) {
67  if (keys.contains(componentKey)) {
68  throw new IllegalStateException(keyToString(componentKey) + " is already registered.");
69  }
70  keys.add(componentKey);
71  }
72 
76  @Deprecated
77  public void addNamedComponent(String name) {
78  addComponent(Names.named(name));
79  }
80 
84  @Deprecated
85  public void removeComponent(Class<? extends Annotation> annotation) {
86  addComponent(Key.get(Object.class, annotation));
87  components.remove(annotation);
88  }
89 
93  @Deprecated
94  public void removeComponent(Annotation annotation) {
95  removeComponent(Key.get(Object.class, annotation));
96  components.remove(annotation);
97  }
98 
99  private void removeComponent(Key<?> componentKey) {
100  if (!keys.remove(componentKey)) {
101  throw new IllegalStateException(keyToString(componentKey) + " is not registered.");
102  }
103  }
104 
108  @Deprecated
109  public void removeNamedComponent(String name) {
110  removeComponent(Names.named(name));
111  }
112 
116  @Deprecated
117  public boolean hasComponent(Class<? extends Annotation> annotation) {
118  return hasComponent(Key.get(Object.class, annotation));
119  }
120 
124  @Deprecated
125  public boolean hasComponent(Annotation annotation) {
126  return hasComponent(Key.get(Object.class, annotation));
127  }
128 
129  private boolean hasComponent(Key<?> componentKey) {
130  return keys.contains(componentKey);
131  }
132 
136  @Deprecated
137  public boolean hasNamedComponent(String name) {
138  return hasComponent(Names.named(name));
139  }
140 
141  private String keyToString(Key<?> componentKey) {
142  return "Annotation" + componentKey.getAnnotation() != null ?
143  " " + componentKey.getAnnotation() :
144  "Type " + componentKey.getAnnotationType();
145  }
146 
150  @Deprecated
151  public void clear() {
152  components.clear();
153  keys.clear();
154  }
155 
159  @Deprecated
160  public List<Object> getActiveComponents() {
161  return Collections.unmodifiableList(components);
162  }
163 }
void addComponent(Class<? extends Annotation > annotation)
void removeComponent(Class<? extends Annotation > annotation)
boolean hasComponent(Class<? extends Annotation > annotation)