001    package org.hackystat.projectbrowser.page.dailyprojectdata.unittest;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.List;
006    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
007    
008    /**
009     * Data structure for representing unittest information about a single project. 
010     * This representation includes the Project, plus the number of passing and failing
011     * unit test invocations.
012     * 
013     * @author Philip Johnson
014     * @author Shaoxuan Zhang
015     */
016    public class UnitTestData implements Serializable {
017    
018      /** Support serialization. */
019      private static final long serialVersionUID = 1L;
020      
021      /** The project whose data is kept in this instance. */
022      private Project project;
023      
024      /** The two buckets for this data. */
025      private List<Integer> buckets = new ArrayList<Integer>();
026      
027      /** The total number of entries added across all buckets. */
028      private int total = 0;
029      
030      /**
031       * Creates a new UnitTestData instance and initializes the buckets to zero.  
032       * @param name The name of the Project associated with this instance. 
033       */
034      public UnitTestData(Project name) {
035        this.project = name;
036        for (int i = 0; i < 2; i++) {
037          buckets.add(0);
038        }
039      }
040      
041      /**
042       * Increments the given bucket by the increment value. 
043       * @param bucket The bucket number. 
044       * @param increment How much to add to that bucket.
045       */
046      private void incrementBucket(int bucket, int increment) {
047        buckets.set(bucket, buckets.get(bucket) + increment);
048        this.total += increment;
049      }
050      
051      /**
052       * Updates this UnitTestData instance with information about the numbers of passes and failures
053       * for a given instance in the Project.  
054       * @param numPasses The number of passing unit test invocations.
055       * @param numFailures The number of failing unit test invocations.
056       */
057      public void addEntry(int numPasses, int numFailures) {
058        incrementBucket(0, numPasses);
059        incrementBucket(1, numFailures);
060      }
061      
062      /**
063       * Returns the current value of the specified bucket. 
064       * @param bucket The bucket number, where 0 is the first one and 4 is the last one. 
065       * @return The value inside the given bucket. 
066       */
067      public int getBucketValue(int bucket) {
068        return buckets.get(bucket);
069      }
070      
071      /**
072       * Returns the total number of entries across all buckets. 
073       * @return The total number of entries. 
074       */
075      public int getTotal() {
076        return this.total;
077      }
078      
079      /**
080       * Returns the total number of entries across all buckets as a string. 
081       * @return The total number of entries. 
082       */
083      public String getTotalString() {
084        return String.valueOf(this.total);
085      }
086      
087      /**
088       * Returns the bucket value as a percentage of the total number of entries across all buckets.
089       * Returns zero if there are no entries. 
090       * @param bucket The bucket whose percentage is to be returned.
091       * @return The bucket as a percentage.
092       */
093      public int getBucketPercentage(int bucket) {
094        if (getTotal() == 0) {
095          return 0;
096        }
097        else {
098          double percent = (double)getBucketValue(bucket) / (double)getTotal();
099          return ((int) (percent * 100));
100        }
101      }
102        
103      /**
104       * Returns the current value of the specified bucket as a string. 
105       * @param bucket The bucket number, where 0 is the first one and 4 is the last one. 
106       * @return The value inside the given bucket. 
107       */
108      public String getBucketCountString(int bucket) {
109        return String.valueOf(getBucketValue(bucket));
110      }
111      
112      /**
113       * Returns the bucket percentage as a string.
114       * @param bucket The bucket.
115       * @return Its percentage as a string.
116       */
117      public String getBucketPercentageString(int bucket) {
118        return getBucketPercentage(bucket) + "%";
119      }
120      
121     
122      /**
123       * Return the project associated with this data. 
124       * @return The project.
125       */
126      public Project getProject() {
127        return project;
128      }
129    }