001    package org.hackystat.telemetry.analyzer.function.impl;
002    
003    import org.hackystat.telemetry.analyzer.function.TelemetryFunctionException;
004    import org.hackystat.telemetry.analyzer.model.TelemetryDataPoint;
005    import org.hackystat.telemetry.analyzer.model.TelemetryStream;
006    import org.hackystat.telemetry.analyzer.model.TelemetryStreamCollection;
007    
008    /**
009     * Utility class handling miscellaneous tasks.
010     * 
011     * @author (Cedric) Qin ZHANG
012     */
013    class Utility {
014      
015      /**
016       * Creates a new instance of <code>TelemetryStreamCollection</code> based on supplied source,
017       * and fills it with supplied value.
018       * 
019       * @param value The value to fill the newly created instance of 
020       *        <code>TelemetryStreamCollection</code>.
021       * @param source The template <code>TelemetryStreamCollection</code> object.
022       * @return A newly created instance of <code>TelemetryStreamCollection</code>.
023       * @throws TelemetryFunctionException If anything is wrong.
024       */
025      static TelemetryStreamCollection expandNumber(Number value, TelemetryStreamCollection source) 
026          throws TelemetryFunctionException {
027        TelemetryStreamCollection target = new TelemetryStreamCollection(source.getName(),
028            source.getProject(), source.getInterval());
029        try {
030          for (TelemetryStream srcStream : source) {
031            target.add(expandNumber(value, srcStream));
032          }
033        }
034        catch (Exception ex) {
035          throw new TelemetryFunctionException(ex);
036        }   
037        return target;
038      }
039    
040      /**
041       * Creates a new instance of <code>TelemetryStream</code> based on supplied source,
042       * and fills it with supplied value.
043       * 
044       * @param value The value to fill the newly created instance of <code>TelemetryStream</code>.
045       * @param source The template <code>TelemetryStream</code> object.
046       * @return A newly created instance of <code>TelemetryStream</code>.
047       * @throws TelemetryFunctionException If anything is wrong.
048       */
049      private static TelemetryStream expandNumber(Number value, TelemetryStream source) 
050          throws TelemetryFunctionException {
051        TelemetryStream target = new TelemetryStream(source.getTag());
052        try {
053          for (TelemetryDataPoint srcDataPoint : source.getDataPoints()) {
054            target.addDataPoint(new TelemetryDataPoint(srcDataPoint.getPeriod(), value));
055          }
056        }
057        catch (Exception ex) {
058          throw new TelemetryFunctionException(ex);
059        }
060        return target;
061      }
062    }