MATSIM
QSimComponentsConfigGroup.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * QSimComponentsConfigGroup.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.util.*;
25 import java.util.stream.Collectors;
26 
35 
36 public class QSimComponentsConfigGroup extends ConfigGroup {
37  public static final String GROUP_NAME = "qsim_components";
38 
39  private static final String ACTIVE_COMPONENTS = "activeComponents";
40 
41  public static final List<String> DEFAULT_COMPONENTS = Arrays.asList(ActivityEngineModule.COMPONENT_NAME,
44 
45  private List<String> activeComponents = new LinkedList<>(DEFAULT_COMPONENTS);
46 
48  super(GROUP_NAME);
49  }
50 
51  @Override
52  public final Map<String, String> getComments() {
53  Map<String, String> map = new HashMap<>();
54 
55  map.put(ACTIVE_COMPONENTS,
56  "Defines which components are active and in which order they are registered. Depending on which extensions and contribs you use, it may be necessary to define additional components here. Default is: "
57  + String.join(", ", DEFAULT_COMPONENTS));
58 
59  return map;
60  }
61 
62  public List<String> getActiveComponents() {
63  return activeComponents;
64  }
65 
66  public void setActiveComponents(List<String> activeComponents) {
67  // the original design uses "List" here. But it fails later when keys exist twice.
68  // yyyy possibly, we should rather accept "Set" instead of "List". But I don't want to do the refactoring before we have clarified what we want.
69  // kai, nov'19
70  Set<String> activeComponentsAsSet = new LinkedHashSet<>( activeComponents ) ;
71  this.activeComponents = new ArrayList<>( activeComponentsAsSet ) ;
72  }
73 
74  public void addActiveComponent( String component ) {
75  // I need this so often that I am finally adding it here. kai, apr'23
76 
77  List<String> components = getActiveComponents();
78  components.add( component );
79  setActiveComponents( components );
80  // (doing this the indirect way because of the Set vs List discussion above. kai, apr'23
81  }
82 
83  public void removeActiveComponent( String component ) {
84  // I need this so often that I am finally adding it here. kai, apr'24
85 
86  List<String> components = getActiveComponents();
87  components.remove( component );
88  setActiveComponents( components );
89  // (doing this the indirect way because of the Set vs List discussion above. kai, apr'24
90  }
91 
92  @StringGetter(ACTIVE_COMPONENTS)
93  public String getActiveComponentsAsString() {
94  return String.join(", ", activeComponents);
95  }
96 
97  @StringSetter(ACTIVE_COMPONENTS)
99  this.activeComponents = interpretQSimComponents(activeComponents);
100  }
101 
102  private List<String> interpretQSimComponents(String config) {
103  List<String> elements = Arrays.asList(config.split(",")).stream().map(String::trim)
104  .collect(Collectors.toList());
105 
106  if (elements.size() == 1 && elements.get(0).length() == 0) {
107  return new LinkedList<>();
108  }
109 
110  return elements;
111  }
112 }