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
15
16
17
18
19
20
21
22
23
24
25 public class ModelParameters implements SelfWrapper
26 {
27
28
29 private List<Parameter> parameters;
30
31
32 private Map<String, Parameter> nameMap;
33
34
35
36
37
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
59
60
61
62
63 public Parameter getParameterByName(final String name)
64 {
65 return this.nameMap.get(name);
66 }
67
68
69
70
71
72 public List<String> getParameterNames()
73 {
74 return new ArrayList<String>(this.nameMap.keySet());
75 }
76
77
78
79
80
81
82 public boolean parameterExists(final String name)
83 {
84 return this.nameMap.containsKey(name);
85 }
86
87
88
89
90
91
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
105
106
107
108
109 public Parameter.ParameterType getParameterType(final String name) throws IMBException
110 {
111 return getParameterOrThrowException(name).getType();
112 }
113
114
115
116
117
118
119
120 public Object getParameterValue(final String name) throws IMBException
121 {
122 return getParameterOrThrowException(name).getValue();
123 }
124
125
126
127
128
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
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
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
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 }