MATSIM
ConfigGroup.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Module.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.config;
22 
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.TreeMap;
31 
32 import jakarta.validation.Valid;
33 
34 import org.apache.logging.log4j.LogManager;
35 import org.apache.logging.log4j.Logger;
37 import org.matsim.core.utils.io.IOUtils;
38 
45 public class ConfigGroup implements MatsimExtensionPoint {
46  // this cannot be made final since many actual groups inherit from it
47 
49  // member variables
51 
52  private final String name;
53  private final TreeMap<String,String> params;
54  private final Map<String, Collection<@Valid ConfigGroup>> parameterSetsPerType = new HashMap<>();
55  private boolean locked = false ;
56 
57  private final static Logger log = LogManager.getLogger(ConfigGroup.class);
58 
59  public ConfigGroup(final String name) {
60  this.name = name;
61  this.params = new TreeMap<>();
62  }
63 
64  public void addParam(final String paramName, final String value) {
65  if (this.params.containsKey(paramName)) {
66  log.info(this.toString() + "[paramName=" + paramName + ",oldValue=" + this.params.get(paramName) + ",value=" + value + " value replaced]");
67  }
68  this.params.put(paramName, value);
69  }
70 
80  protected void addParameterToMap(final Map<String, String> map, final String paramName) {
81  String value = this.getValue(paramName);
82  if (!((value == null) || value.equalsIgnoreCase("null"))) {
83  map.put(paramName, value);
84  } else {
85  map.put(paramName, "null");
86  }
87  }
88 
94  protected void checkConsistency(Config config) {
95  // (I added Config as a parameter, since there are many occasions where the validity of a ConfigGroup can only be checked when other
96  // material is known. Could put all of this in the "global" config consistency checker, but if it conceptually belongs into the
97  // ConfigGroup, I think it is easier to have it more local. Wasn't a big problem, since this method is _only_ called from the global config
98  // itself, which obviously can just pass on a "this" pointer. kai, jan'17)
99 
100  // default: just call this method on parameter sets
101  for ( Collection<? extends ConfigGroup> sets : getParameterSets().values() ) {
102  for ( ConfigGroup set : sets ) set.checkConsistency(config);
103  }
104  }
105  @Deprecated // please try to use the "typed" access structures. kai, nov'16
106  public String getValue(final String paramName) {
107  return this.params.get(paramName);
108  }
109 
110  public final String getName() {
111  return this.name;
112  }
113 
115  public Map<String, String> getParams() {
116  return this.params;
117  }
118 
122  public Map<String, String> getComments() {
123  return new HashMap<>();
124  }
125 
126  @Override
127  public final String toString() {
128  StringBuilder str = new StringBuilder();
129  for ( Entry<String, String> entry : this.getParams().entrySet() ) {
130  str.append('[');
131  str.append(entry.getKey());
132  str.append('=');
133  str.append(entry.getValue());
134  str.append(']');
135  }
136  return "[name=" + this.getName() + "]" +
137  "[nOfParams=" + this.getParams().size() + "]" + str.toString();
138  }
139 
140  // /////////////////////////////////////////////////////////////////////////
141  // "Parameter sets" are nested sub-modules.
142  // They have a "type", which is the equivalent of the name of a Module,
143  // except that an arbitrary number of parameter sets per type is allowed.
144  // TODO: find a way to associate a type with a specific java type/class
145  // /////////////////////////////////////////////////////////////////////////
149  public ConfigGroup createParameterSet(final String type) {
150  return new ConfigGroup( type );
151  }
152 
153  //public final Module createAndAddParameterSet(final String type) {
154  // final Module m = createParameterSet( type );
155 
156  // if ( !m.getName().equals( type ) ) {
157  // throw new IllegalArgumentException( "the \"name\" of parameter sets should correspond to their type."+
158  // " type \""+type+"\" is different from name \""+m.getName()+"\" " );
159  // }
160 
161  // addParameterSet( m );
162  // return m;
163  //}
164 
165  public void addParameterSet(final ConfigGroup set) {
166  checkParameterSet( set );
167  Collection<ConfigGroup> parameterSets = parameterSetsPerType.get( set.getName() );
168 
169  if ( parameterSets == null ) {
170  parameterSets = new ArrayList<>();
171  parameterSetsPerType.put( set.getName() , parameterSets );
172  }
173 
174  parameterSets.add( set );
175  }
176 
177  public boolean removeParameterSet( final ConfigGroup set ) {
178  final Collection<ConfigGroup> parameterSets = parameterSetsPerType.get( set.getName() );
179  return parameterSets != null ?
180  parameterSets.remove( set ) :
181  false;
182  }
183 
191  protected void checkParameterSet(final ConfigGroup set) {
192  // empty for inheritance
193  }
194 
199  protected final Collection<? extends ConfigGroup> clearParameterSetsForType( final String type ) {
200  return parameterSetsPerType.remove( type );
201  }
202 
203  public final Collection<? extends ConfigGroup> getParameterSets(final String type) {
204  final Collection<ConfigGroup> sets = parameterSetsPerType.get( type );
205  return sets == null ?
206  Collections.<ConfigGroup>emptySet() :
207  Collections.unmodifiableCollection( sets );
208  }
209 
210  public final Map<String, ? extends Collection<? extends ConfigGroup>> getParameterSets() {
211  // TODO: immutabilize (including lists)
212  // maybe done with what I did below? kai, sep'16
213 
214  // return parameterSetsPerType;
215 
216  Map<String, Collection<ConfigGroup>> parameterSetsPerType2 = new TreeMap<>() ;
217  for ( Entry<String, Collection<ConfigGroup>> entry : parameterSetsPerType.entrySet() ) {
218  parameterSetsPerType2.put( entry.getKey(), Collections.unmodifiableCollection(entry.getValue()) ) ;
219  }
220  return Collections.unmodifiableMap( parameterSetsPerType2 ) ;
221  }
222 
223  public final boolean isLocked() {
224  return locked;
225  }
226 
227  public void setLocked() {
228  // need to have this non-final to be able to override in order to set delegates. kai, jun'15
229  this.locked = true ;
230  for ( Collection<ConfigGroup> parameterSets : this.parameterSetsPerType.values() ) {
231  for ( ConfigGroup parameterSet : parameterSets ) {
232  parameterSet.setLocked();
233  }
234  }
235  }
236 
237  public final void testForLocked() {
238  if ( locked ) {
239  throw new RuntimeException( "Too late to change this ...") ;
240  }
241  }
242 
243  public static URL getInputFileURL(URL context, String filename) {
244  if (filename.startsWith("~/")) {
245  filename = System.getProperty("user.home") + filename.substring(1);
246  return IOUtils.getFileUrl(filename) ;
247  }
248  if ( filename.startsWith("/") ) {
249  // (= filename is absolute)
250  // (yyyy this may possibly fail on win systems. kai, sep.18)
251 
252  // Absolute filename on Windows, when obtained through URL.toURI().getPath() starts with `/`, like on Unix.
253  return IOUtils.getFileUrl(filename) ;
254  }
255  return IOUtils.extendUrl(context, filename);
256  }
257 }
ConfigGroup createParameterSet(final String type)
Map< String, String > getParams()
void addParameterSet(final ConfigGroup set)
final Collection<? extends ConfigGroup > clearParameterSetsForType(final String type)
void checkParameterSet(final ConfigGroup set)
static URL getFileUrl(String filename)
Definition: IOUtils.java:501
final Collection<? extends ConfigGroup > getParameterSets(final String type)
void checkConsistency(Config config)
Map< String, String > getComments()
static URL getInputFileURL(URL context, String filename)
final Map< String, ? extends Collection<? extends ConfigGroup > > getParameterSets()
void addParameterToMap(final Map< String, String > map, final String paramName)
final Map< String, Collection< @Valid ConfigGroup > > parameterSetsPerType
final TreeMap< String, String > params
boolean removeParameterSet(final ConfigGroup set)
String getValue(final String paramName)
static URL extendUrl(URL context, String extension)
Definition: IOUtils.java:517
void addParam(final String paramName, final String value)