001    package org.hackystat.dailyprojectdata.resource.devtime;
002    
003    import java.math.BigInteger;
004    
005    import javax.xml.datatype.XMLGregorianCalendar;
006    
007    /**
008     * Provides a facility for counting up the DevTime associated with a series of timestamps
009     * associated with DevEvent sensor data.  
010     * The DevTimeCounter partitions a day into 288 five minute intervals.  If at least one 
011     * DevEvent timestamp occurs within an interval, that entire five minute interval counts
012     * as DevTime.  At any time, the getDevTime() method can be called to find the total 
013     * amount of DevTime accumulated so far.   DevTime for a day can range from 0 minutes 
014     * to 1440 minutes, in five minute increments. 
015     * 
016     * Note that the DevTimeCounter looks only at the hours, minutes, and seconds associated with 
017     * a timestamp.  The client is responsible for assuring that all of the timestamps are associated
018     * with the same day. 
019     * 
020     * @author Philip Johnson
021     */
022    public class DevTimeCounter {
023      
024      /** Array of five minute intervals, initialized to false, indicating zero DevTime. */
025      private boolean[] devTimeArray = new boolean[288];
026      
027      /** Create a new DevTimeCounter, initialized to zero DevTime. */
028      public DevTimeCounter () {
029        // Do nothing
030      }
031      
032      /**
033       * Update the DevTimeCounter with a new DevEvent.  Its five minute interval will now count
034       * as DevTime (if it didn't already). 
035       * @param timestamp The timestamp to add. 
036       */
037      public void addDevEvent(XMLGregorianCalendar timestamp) {
038        int hours = timestamp.getHour();
039        int minutes = timestamp.getMinute();
040        int seconds = timestamp.getSecond();
041        int totalSeconds = seconds + (minutes * 60) + (hours * 60 * 60);
042        int binSize = (60 * 60 * 24) / 288;
043        int index = (totalSeconds / binSize);
044        devTimeArray[index] = true;
045      }
046      
047      /**
048       * Returns the total DevTime (in minutes, as a multiple of five) associated with this
049       * DevTimeCounter.
050       * Returns a BigInteger because that's what the XML wants.  
051       * @return The total DevTime, from 0 to 1440 (5 * 288). 
052       */
053      public BigInteger getDevTime() {
054        int devTime = 0;
055        for (int index = 0; index < 288; index++) {
056          if (devTimeArray[index]) {
057            devTime += 5;
058          }
059        }
060        return BigInteger.valueOf(devTime);
061      }
062      
063    
064    }