001    package org.hackystat.telemetry.analyzer.model;
002    
003    import java.util.Collection;
004    import java.util.HashMap;
005    import java.util.Iterator;
006    import java.util.Map;
007    
008    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
009    import org.hackystat.utilities.time.interval.Interval;
010    
011    /**
012     * Provides a collection of telemetry streams. Note that this class does not constrain
013     * what kind of telemetry streams are present in a collection. Typically, you want to add only
014     * related streams, and each stream should contain points over the same interval.
015     * <p>
016     * Thread Safety: methods in this class are not synchronized.
017     * 
018     * @author (Cedric) Qin Zhang
019     */
020    public class TelemetryStreamCollection implements Iterable<TelemetryStream> {
021    
022      private String name;
023    
024      private Project project;
025    
026      private Interval interval;
027    
028      // key is stream tag, value is telemetry stream
029      // Must use HashMap, since key could be null. TreeMap throws null pointer exception.
030      private Map<Object, TelemetryStream> streamMap = new HashMap<Object, TelemetryStream>();
031    
032      /**
033       * Constructs this instance. The project and the interval are just tags, they
034       * are not used to constrain the kind of streams that can be added. (Maybe I
035       * should do this :=)).
036       * 
037       * @param name The name of this telemetry streams collection.
038       * @param project The project.
039       * @param interval The interval.
040       */
041      public TelemetryStreamCollection(String name, Project project, Interval interval) {
042        this.name = name;
043        this.project = project;
044        this.interval = interval;
045      }
046    
047      /**
048       * Sets the name tag.
049       * 
050       * @param name The name tag.
051       */
052      public void setName(String name) {
053        this.name = name;
054      }
055    
056      /**
057       * Gets the name tag.
058       * 
059       * @return The name tag.
060       */
061      public String getName() {
062        return this.name;
063      }
064    
065      /**
066       * Gets the project tag.
067       * 
068       * @return The project tag.
069       */
070      public Project getProject() {
071        return this.project;
072      }
073    
074      /**
075       * Gets the interval tag.
076       * 
077       * @return The interval tag.
078       */
079      public Interval getInterval() {
080        return this.interval;
081      }
082    
083      /**
084       * Adds a telemetry stream to this collection.
085       * 
086       * @param stream The telemetry stream to be added.
087       * @throws TelemetryDataModelException If there is already a stream with the same tag.
088       */
089      public void add(TelemetryStream stream) throws TelemetryDataModelException {
090        if (this.streamMap.containsKey(stream.getTag())) {
091          throw new TelemetryDataModelException("Duplicated stream tag detected.: " + stream.getTag());
092        }
093        this.streamMap.put(stream.getTag(), stream);
094      }
095    
096      /**
097       * Gets telemetry stream by tag value.
098       * 
099       * @param tag Telemetry stream tag. Null is a valid value.
100       * @return TelemetryStream The telemetry stream, or null if it does not exist.
101       */
102      public TelemetryStream get(Object tag) {
103        return this.streamMap.get(tag);
104      }
105      
106      /**
107       * Return an iterator over the TelemetryStreams in this collection.
108       * 
109       * @return An iterator over TelemetryStreams.
110       */
111      public Iterator<TelemetryStream> iterator() {
112        return this.streamMap.values().iterator();
113      }
114    
115      /**
116       * Gets all the telemetry streams contained in this collection.
117       * 
118       * @return A collection of <code>TelemetryStream</code> object.
119       */
120      public Collection<TelemetryStream> getTelemetryStreams() {
121        return this.streamMap.values();
122      }
123    
124    }