SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
ListOfStatProbes.java
1/*
2 * Class: ListOfStatProbes
3 * Description: List of statistical probes
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author Éric Buist
9 * @since 2007
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.stat.list;
26
27import java.util.*;
28import umontreal.ssj.stat.StatProbe;
29
59public class ListOfStatProbes<E extends StatProbe> implements Cloneable, List<E>, RandomAccess {
60
61 // probes must implement RandomAccess, otherwise this class must not implement
62 // RandomAccess.
63 private List<E> probes;
64 private List<ArrayOfObservationListener> listeners = new ArrayList<ArrayOfObservationListener>();
65 protected boolean collect = true;
66 protected boolean broadcast = false;
67 protected String name;
68
73 probes = new ArrayList<E>();
74 }
75
81 public ListOfStatProbes(String name) {
82 probes = new ArrayList<E>();
83 this.name = name;
84 }
85
91 public String getName() {
92 return name;
93 }
94
100 public void setName(String name) {
101 this.name = name;
102 }
103
109 public boolean isModifiable() {
110 return probes instanceof ArrayList;
111 }
112
119 public void setUnmodifiable() {
120 if (isModifiable())
121 probes = Collections.unmodifiableList(probes);
122 }
123
128 public void init() {
129 for (StatProbe probe : probes) {
130 if (probe != null)
131 probe.init();
132 }
133 }
134
146 public void sum(double[] s) {
147 if (s.length != size())
148 throw new IllegalArgumentException(
149 "Invalid length of the given array: given length is " + s.length + ", required length is " + size());
150 int i = 0;
151 for (StatProbe probe : probes)
152 s[i++] = probe == null ? Double.NaN : probe.sum();
153 }
154
166 public void average(double[] a) {
167 if (a.length != size())
168 throw new IllegalArgumentException(
169 "Invalid length of the given array: given length is " + a.length + ", required length is " + size());
170 int i = 0;
171 for (StatProbe probe : probes)
172 a[i++] = probe == null ? Double.NaN : probe.average();
173 }
174
182 public boolean isCollecting() {
183 return collect;
184 }
185
192 public void setCollecting(boolean c) {
193 collect = c;
194 }
195
202 public boolean isBroadcasting() {
203 return broadcast;
204 }
205
212 public void setBroadcasting(boolean b) {
213 broadcast = b;
214 }
215
224 if (l == null)
225 throw new NullPointerException();
226 if (!listeners.contains(l))
227 listeners.add(l);
228 }
229
237 listeners.remove(l);
238 }
239
245 listeners.clear();
246 }
247
252 public void notifyListeners(double[] x) {
253 if (!broadcast)
254 return;
255 // We could also use the enhanced for loop here, but this is less efficient.
256 final int nl = listeners.size();
257 for (int i = 0; i < nl; i++)
258 listeners.get(i).newArrayOfObservations(this, x);
259 }
260
267 public String report() {
268 return StatProbe.report(name, this);
269 }
270
278 try {
279 sa = (ListOfStatProbes<E>) super.clone();
280 } catch (CloneNotSupportedException cne) {
281 throw new IllegalStateException("CloneNotSupportedException for a class implementing Cloneable");
282 }
283 if (probes != null)
284 sa.probes = new ArrayList<E>(probes);
285 return sa;
286 }
287
288 public boolean add(E o) {
289 return probes.add(o);
290 }
291
292 public void add(int index, E o) {
293 probes.add(index, o);
294 }
295
296 public boolean addAll(Collection<? extends E> c) {
297 return probes.addAll(c);
298 }
299
300 public boolean addAll(int index, Collection<? extends E> c) {
301 return probes.addAll(index, c);
302 }
303
304 public void clear() {
305 probes.clear();
306 }
307
308 public boolean contains(Object o) {
309 return probes.contains(o);
310 }
311
312 public boolean containsAll(Collection<?> c) {
313 return probes.containsAll(c);
314 }
315
316 public boolean equals(Object o) {
317 return probes.equals(o);
318 }
319
320 public E get(int index) {
321 return probes.get(index);
322 }
323
324 public int hashCode() {
325 return probes.hashCode();
326 }
327
328 public int indexOf(Object o) {
329 return probes.indexOf(o);
330 }
331
332 public boolean isEmpty() {
333 return probes.isEmpty();
334 }
335
336 public Iterator<E> iterator() {
337 return probes.iterator();
338 }
339
340 public int lastIndexOf(Object o) {
341 return probes.lastIndexOf(o);
342 }
343
344 public ListIterator<E> listIterator() {
345 return probes.listIterator();
346 }
347
348 public ListIterator<E> listIterator(int index) {
349 return probes.listIterator();
350 }
351
352 public E remove(int index) {
353 return probes.remove(index);
354 }
355
356 public boolean remove(Object o) {
357 return probes.remove(o);
358 }
359
360 public boolean removeAll(Collection<?> c) {
361 return probes.removeAll(c);
362 }
363
364 public boolean retainAll(Collection<?> c) {
365 return probes.retainAll(c);
366 }
367
368 public E set(int index, E element) {
369 return probes.set(index, element);
370 }
371
372 public int size() {
373 return probes.size();
374 }
375
376 public List<E> subList(int fromIndex, int toIndex) {
377 return probes.subList(fromIndex, toIndex);
378 }
379
380 public Object[] toArray() {
381 return probes.toArray();
382 }
383
384 public <T> T[] toArray(T[] a) {
385 return probes.toArray(a);
386 }
387
388}
The objects of this class are statistical probes or collectors, which are elementary devices for coll...
abstract String report()
Returns a string containing a report for this statistical collector.
void clearArrayOfObservationListeners()
Removes all observation listeners from the list of observers of this list of statistical probes.
void addArrayOfObservationListener(ArrayOfObservationListener l)
Adds the observation listener l to the list of observers of this list of statistical probes.
void setCollecting(boolean c)
Sets the status of the statistical collecting mechanism to c.
void setUnmodifiable()
Forbids any future modification to this list of statistical probes.
void setName(String name)
Sets the global name of this list to name.
String getName()
Returns the global name of this list of statistical probes.
boolean isModifiable()
Determines if this list of statistical probes is modifiable, i.e., if probes can be added or removed.
void notifyListeners(double[] x)
Notifies the observation x to all registered observers if broadcasting is ON.
String report()
Formats a report for each probe in the list of statistical probes.
void setBroadcasting(boolean b)
Sets the status of the observation broadcasting mechanism to b.
void sum(double[] s)
For each probe in the list, computes the sum by calling umontreal.ssj.stat.StatProbe....
boolean isCollecting()
Determines if this list of statistical probes is collecting values.
ListOfStatProbes()
Constructs an empty list of statistical probes.
ListOfStatProbes(String name)
Constructs an empty list of statistical probes with name name.
void init()
Initializes this list of statistical probes by calling umontreal.ssj.stat.StatProbe....
boolean isBroadcasting()
Determines if this list of statistical probes is broadcasting observations to registered observers.
void removeArrayOfObservationListener(ArrayOfObservationListener l)
Removes the observation listener l from the list of observers of this list of statistical probes.
void average(double[] a)
For each probe in this list, computes the average by calling umontreal.ssj.stat.StatProbe....
ListOfStatProbes< E > clone()
Clones this object.
Represents an object that can listen to observations broadcast by lists of statistical probes.