1 package nl.tno.imb.mc;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 /**
9 * <p>
10 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
12 * <p>
13 * @version $Revision$, $LastChangedDate$, by $Author$, initial version Oct 17, 2016 <br>
14 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
16 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
17 */
18 public class StandardSettings
19 {
20 /** Map of collected switches. */
21 Map<String, String> switches = new HashMap<>();
22
23 /** Non-switch command line arguments. */
24 List<String> arguments = new ArrayList<>();
25
26 /** Index of next argument to return in <cite>nextArgument</cite>. */
27 private int argumentIndex = 0;
28
29 /**
30 * Construct StandardSettings from the command line arguments.
31 * @param commandLineArguments String[]; the command line arguments
32 */
33 public StandardSettings(final String[] commandLineArguments)
34 {
35 for (String arg : commandLineArguments)
36 {
37 if (arg.startsWith("/") || arg.startsWith("-"))
38 {
39 String[] fields = arg.substring(1).split("[=:]");
40 String value = "";
41 if (fields.length > 1)
42 {
43 value = fields[1];
44 // Silently ignoring the case where fields.length > 2
45 }
46 this.switches.put(fields[0].toLowerCase(), value);
47 }
48 else
49 {
50 this.arguments.add(arg);
51 }
52 }
53 }
54
55 /**
56 * Report if a switch is present.
57 * @param switchName String; name of the switch to report presence of
58 * @return boolean; true if the switch is present; false if the switch is not present
59 */
60 public boolean testSwitch(final String switchName)
61 {
62 return this.switches.containsKey(switchName.toLowerCase());
63 }
64
65 /**
66 * Lookup and return a switch value.
67 * @param switchName String; name of the switch to lookup
68 * @param defaultValue String; default value to return if lookup fails
69 * @return String; the value of the switch, or <cite>defaultValue</cite> if the switch is not defined
70 */
71 public String getSwitch(final String switchName, final String defaultValue)
72 {
73 String result = this.switches.get(switchName.toLowerCase());
74 if (null != result)
75 {
76 return result;
77 }
78 return defaultValue;
79 }
80
81 /**
82 * Reset the internal argument index and return the first non-switch command line argument.
83 * @return String
84 */
85 public String firstArgument()
86 {
87 this.argumentIndex = 0;
88 return nextArgument();
89 }
90
91 /**
92 * Return the next non-switch command line argument and increment the internal argument index.
93 * @return String; the next non-switch command line argument, or <cite>""</cite> if there are no more non-switch command
94 * line arguments
95 */
96 public String nextArgument()
97 {
98 if (this.argumentIndex >= this.arguments.size())
99 {
100 return "";
101 }
102 return this.arguments.get(this.argumentIndex++);
103 }
104
105 public String getSetting(final String settingName, final String defaultValue)
106 {
107 if (!testSwitch(settingName))
108 {
109 try
110 {
111 String result = ConfigurationManager.appSettings(settingName);
112 if (null == result)
113 {
114 return defaultValue;
115 }
116 return result;
117 }
118 catch (ConfigurationErrorsException cee)
119 {
120 return defaultValue;
121 }
122 }
123 else
124 {
125 return getSwitch(settingName, defaultValue);
126 }
127 }
128
129 }