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 import org.opentrafficsim.imb.IMBException;
9 import org.opentrafficsim.imb.SelfWrapper;
10
11 import nl.tno.imb.TByteBuffer;
12
13 /**
14 * Container for a list of model parameters.<br>
15 * For now this object is immutable.
16 * <p>
17 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19 * <p>
20 * @version $Revision$, $LastChangedDate$, by $Author$, initial version Oct 17, 2016 <br>
21 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24 */
25 public class ModelParameters implements SelfWrapper
26 {
27
28 /** The stored parameters. */
29 private List<Parameter> parameters;
30
31 /** Fast lookup by name. */
32 private Map<String, Parameter> nameMap;
33
34 /**
35 * Construct ModelParameters from a IMB ByteBuffer
36 * @param payload
37 * @throws IMBException
38 */
39 public ModelParameters(TByteBuffer payload) throws IMBException
40 {
41 int size = payload.readInt32();
42 this.parameters = new ArrayList<>(size);
43 this.nameMap = new HashMap<>(size);
44 for (int index = 0; index < size; index++)
45 {
46 Parameter parameter = new Parameter(payload);
47 String parameterName = parameter.getName();
48 if (this.nameMap.containsKey(parameterName))
49 {
50 throw new IMBException("Duplicate parameter name \"" + parameterName + "\")");
51 }
52 this.parameters.add(parameter);
53 this.nameMap.put(parameterName, parameter);
54 }
55 }
56
57 /**
58 * Retrieve the parameter with a specified name
59 * @param name String; name of the parameter to look up
60 * @return Parameter; the parameter with the specified name, or null if no parameter with the specified name exists in this
61 * ModelParameters object
62 */
63 public Parameter getParameterByName(final String name)
64 {
65 return this.nameMap.get(name);
66 }
67
68 /**
69 * Retrieve the names of all stored parameters. The returned object is a deep copy and may be modified by the caller.
70 * @return List<String>; the names of all stored parameters
71 */
72 public List<String> getParameterNames()
73 {
74 return new ArrayList<String>(this.nameMap.keySet());
75 }
76
77 /**
78 * Report if this ModelParameters object contains a parameter with the specified name.
79 * @param name String; name of the parameter
80 * @return boolean; true if such a parameter exists; false if such a parameter does not exist
81 */
82 public boolean parameterExists(final String name)
83 {
84 return this.nameMap.containsKey(name);
85 }
86
87 /**
88 * Look up a parameter by name and throw an IMBException if no such parameter can be found.
89 * @param name String; name of the parameter
90 * @return Parameter; the Parameter with the specified name
91 * @throws IMBException when no Parameter with the specified name is stored in this ModelParameters object
92 */
93 private Parameter getParameterOrThrowException(final String name) throws IMBException
94 {
95 Parameter parameter = this.nameMap.get(name);
96 if (null == parameter)
97 {
98 throw new IMBException("No parameter with name " + name + " stored in this ModelParameters object");
99 }
100 return parameter;
101 }
102
103 /**
104 * Retrieve the type of a parameter.
105 * @param name String; name of the parameter
106 * @return Parameter.ParameterType; the type of the parameter
107 * @throws IMBException when no parameter with the specified name exists in this ModelParameters object
108 */
109 public Parameter.ParameterType getParameterType(final String name) throws IMBException
110 {
111 return getParameterOrThrowException(name).getType();
112 }
113
114 /**
115 * Retrieve the value of a parameter.
116 * @param name String; name of the parameter
117 * @return Object; the value of the parameter
118 * @throws IMBException when no parameter with the specified name exists in this ModelParameters object
119 */
120 public Object getParameterValue(final String name) throws IMBException
121 {
122 return getParameterOrThrowException(name).getValue();
123 }
124
125 /**
126 * Add a parameter. The name of the parameter must be unique.
127 * @param parameter Parameter
128 * @return boolean; true if the parameter was added
129 */
130 public boolean addParameter(final Parameter parameter)
131 {
132 if (this.nameMap.containsKey(parameter.getName()))
133 {
134 return false;
135 }
136 this.parameters.add(parameter);
137 this.nameMap.put(parameter.getName(), parameter);
138 return true;
139 }
140
141 /** {@inheritDoc} */
142 @Override
143 public void prepare(TByteBuffer payload)
144 {
145 payload.prepare(this.parameters.size());
146 for (int index = 0; index < this.parameters.size(); index++)
147 {
148 this.parameters.get(index).prepare(payload);
149 }
150 }
151
152 /** {@inheritDoc} */
153 @Override
154 public void qWrite(TByteBuffer payload)
155 {
156 payload.qWrite(this.parameters.size());
157 for (int index = 0; index < this.parameters.size(); index++)
158 {
159 this.parameters.get(index).qWrite(payload);
160 }
161 }
162
163 /** {@inheritDoc} */
164 @Override
165 public String toString()
166 {
167 StringBuilder result = new StringBuilder();
168 result.append("ModelParameters [parameters=");
169 String separator = "";
170 for (Parameter parameter : this.parameters)
171 {
172 result.append(separator);
173 result.append(parameter.getName() + ":" + parameter.getValue());
174 separator = ", ";
175 }
176 result.append("]");
177 return result.toString();
178 }
179
180 }