bcds.phison.sim
Class SimStatsCollector

java.lang.Object
  extended by bcds.phison.sim.SimStatsCollector

public class SimStatsCollector
extends java.lang.Object

This class offers a synchronized map to store statistical collectors for Sim (and related classes). The simulator can share one instance among all the execution threads, and each of these can add/update variables to the common instance. At the end, the simulator driver (Sim.java) can print the accumulated results.

Collectors can be organized in groups based on their class. This is useful for reporting purposes: those that belong to exactly the same class can be "pretty-printed" in tabular form. Additionally, there is a group, whose name is the empty string, that can hold any collector and does not enfoce membership to a specific class.

Practically all methods in this class are syncrhonized.

Author:
Juan Segovia S.

Constructor Summary
SimStatsCollector()
          Creates a new instance and registers the default group, whose name is the empty string.
 
Method Summary
 boolean add(Collector var)
          Adds the collector var to the default group, where membership does not require being an instance of a specific class.
<T extends Collector>
boolean
add(T var, java.lang.String grp_name, java.lang.Class<T> grp_class)
          Adds a new collector and associates it to a group.
 boolean addGroup(java.lang.String grp_name, java.lang.Class<? extends Collector> grp_class)
          Registers a new group and declares that its members should have the class grp_class to be admitted.
 void collect(java.lang.String var_name, double value)
          Locates the collector with the name var_name and calls its Collector.collect(double) method.
 void collect(java.lang.String var_name, double at, double value)
          Locates the collector with the name var_name and calls its collect method under the assumption that the collector object is an instance of Series.
 Collector get(java.lang.String var_name)
          Returns the collector object with the given name, or null if such collector is not registered.
 java.util.Set<Collector> getGroupMembers(java.lang.String grp_name)
          Returns a set containing the collectors of a given group.
 java.util.Set<java.lang.String> getGroupNames()
          Returns a set containing the group names, including the default group.
 java.lang.String getGroupOf(java.lang.String var_name)
          Returns the group name of the collector var_name, or null if var_name is not registered.
 java.util.Set<java.lang.String> keySet()
          Returns an unmodifiable set view of the collector names registered.
 boolean remove(java.lang.String var_name)
          Removes a collector.
 int size()
          Returns the number of collectors present.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimStatsCollector

public SimStatsCollector()
Creates a new instance and registers the default group, whose name is the empty string.

Method Detail

add

public <T extends Collector> boolean add(T var,
                                         java.lang.String grp_name,
                                         java.lang.Class<T> grp_class)
Adds a new collector and associates it to a group. If the group does not exist, it is added by calling addGroup. Collectors are identified by their name (that is, through getName). Only the first add for a given collector name succeeds; the rest is ignored.

The actual type of the collector must match that of the group to which is to be added. Otherwise, a runtime exception is thrown.

Returns:
true if the collector was added successfully, and false if the there was already a collector by the same name.
Throws:
java.lang.IllegalArgumentException - if code var is null or the collector's class and that of its desired group are not the same.

Note that they must point to the same class reference (as in a.getClass()==b.getClass()), not just be class-compatible.

This exception may also be thrown in addGroup(java.lang.String, java.lang.Class), which is called from there.


add

public boolean add(Collector var)
Adds the collector var to the default group, where membership does not require being an instance of a specific class.


getGroupOf

public java.lang.String getGroupOf(java.lang.String var_name)
Returns the group name of the collector var_name, or null if var_name is not registered. The running time of this method is proportional to the number of collectors present.


size

public int size()
Returns the number of collectors present.


get

public Collector get(java.lang.String var_name)
Returns the collector object with the given name, or null if such collector is not registered.


keySet

public java.util.Set<java.lang.String> keySet()
Returns an unmodifiable set view of the collector names registered. The caller should use locking if a consistent state is expected in a threaded execution environment.


remove

public boolean remove(java.lang.String var_name)
Removes a collector. If the group to which it belongs ends up being empty, it is also removed. The default group, however, is never removed.


addGroup

public boolean addGroup(java.lang.String grp_name,
                        java.lang.Class<? extends Collector> grp_class)
Registers a new group and declares that its members should have the class grp_class to be admitted. Groups must be unique. only the first call to add a given group succeeds.

Returns:
true if the group was added (did not exist before); false otherwise.
Throws:
java.lang.IllegalArgumentException - if code grp_name or code grp_class is null.

collect

public void collect(java.lang.String var_name,
                    double value)
Locates the collector with the name var_name and calls its Collector.collect(double) method. Calling this method with a var_name that does not correspond to any registered collector results in an NullPointerException.


collect

public void collect(java.lang.String var_name,
                    double at,
                    double value)
Locates the collector with the name var_name and calls its collect method under the assumption that the collector object is an instance of Series. Calling this method with a var_name that does not correspond to any registered collector results in an NullPointerException.


getGroupNames

public java.util.Set<java.lang.String> getGroupNames()
Returns a set containing the group names, including the default group. The set is in ascending order (by group name); thus, the first element is always the default group (the empty string).


getGroupMembers

public java.util.Set<Collector> getGroupMembers(java.lang.String grp_name)
Returns a set containing the collectors of a given group. Members are sorted by their name (ascending order). If grp_name is not registered, an empty set is returned.