001    package org.hackystat.projectbrowser.page.dailyprojectdata.coupling;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.List;
006    import java.util.logging.Logger;
007    
008    import org.hackystat.projectbrowser.ProjectBrowserApplication;
009    import org.hackystat.projectbrowser.page.dailyprojectdata.detailspanel.DailyProjectDetailsPanel;
010    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
011    import org.hackystat.utilities.stacktrace.StackTrace;
012    
013    /**
014     * Data structure for representing coupling information about a single project. 
015     * This representation includes the Project, plus the number of classes in the project with 
016     * coupling in each of five buckets: 0-5, 6-10, 11-15, 16-20, and 20+
017     * 
018     * @author Philip Johnson
019     * @author Shaoxuan Zhang
020     */
021    public class CouplingData implements Serializable {
022    
023      /** Support serialization. */
024      private static final long serialVersionUID = 1L;
025      
026      /** The project whose data is kept in this instance. */
027      private Project project;
028      
029      /** The five buckets for this data. */
030      private List<List<org.hackystat.dailyprojectdata.resource.coupling.jaxb.CouplingData>> buckets = 
031        new ArrayList<List<org.hackystat.dailyprojectdata.resource.coupling.jaxb.CouplingData>>();
032      
033      /** The total number of entries added across all buckets. */
034      private int total = 0;
035    
036      /**
037       * Creates a new couplingData instance and initializes the buckets to zero.  
038       * @param name The name of the Project associated with this instance. 
039       */
040      public CouplingData(Project name) {
041        this.project = name;
042        for (int i = 0; i < 5; i++) {
043          buckets.add(
044              new ArrayList<org.hackystat.dailyprojectdata.resource.coupling.jaxb.CouplingData>());
045        }
046      }
047    
048      
049      /**
050       * Updates this CouplingData instance with information about the number of couplings for 
051       * a specific class. 
052       * @param couplingCount The number of couplings. 
053       * @param data The Coupling data. 
054       */
055      public void addEntry(int couplingCount, 
056          org.hackystat.dailyprojectdata.resource.coupling.jaxb.CouplingData data) {
057        try {
058          if (couplingCount <= 5) {
059            buckets.get(0).add(data);
060          }
061          else if (couplingCount <= 10) {
062            buckets.get(1).add(data);
063          }
064          else if (couplingCount <= 15) {
065            buckets.get(2).add(data);
066          }
067          else if (couplingCount <= 20) {
068            buckets.get(3).add(data);
069          }
070          else {
071            buckets.get(4).add(data);
072          }
073          total++;
074        }
075        catch (Exception e) {
076          Logger logger = ((ProjectBrowserApplication)ProjectBrowserApplication.get()).getLogger();
077          logger.info("Error adding an entry to coupling DPD: " + StackTrace.toString(e));
078        }
079      }
080      
081      /**
082       * Returns the current value of the specified bucket. 
083       * @param bucket The bucket number, where 0 is the first one and 4 is the last one. 
084       * @return The value inside the given bucket. 
085       */
086      public int getBucketValue(int bucket) {
087        return buckets.get(bucket).size();
088      }
089      
090      /**
091       * Returns the total number of entries across all buckets. 
092       * @return The total number of entries. 
093       */
094      public int getTotal() {
095        return this.total;
096      }
097      
098      /**
099       * Returns the total number of entries across all buckets as a string. 
100       * @return The total number of entries. 
101       */
102      public String getTotalString() {
103        return String.valueOf(this.total);
104      }
105      
106      /**
107       * Returns the bucket value as a percentage of the total number of entries across all buckets.
108       * @param bucket The bucket whose percentage is to be returned.
109       * @return The bucket as a percentage.
110       */
111      public int getBucketPercentage(int bucket) {
112        if (getTotal() == 0) {
113          return 0;
114        }
115        else {
116          double percent = (double)getBucketValue(bucket) / (double)getTotal();
117          return ((int) (percent * 100));
118        }
119      }
120        
121      /**
122       * Returns the current value of the specified bucket as a string. 
123       * @param bucket The bucket number, where 0 is the first one and 4 is the last one. 
124       * @return The value inside the given bucket. 
125       */
126      public String getBucketCountString(int bucket) {
127        return String.valueOf(getBucketValue(bucket));
128      }
129      
130      /**
131       * Returns the bucket percentage as a string.
132       * @param bucket The bucket.
133       * @return Its percentage as a string.
134       */
135      public String getBucketPercentageString(int bucket) {
136        return getBucketPercentage(bucket) + "%";
137      }
138      
139     
140      /**
141       * Return the project associated with this data. 
142       * @return The project.
143       */
144      public Project getProject() {
145        return project;
146      }
147      
148      /**
149       * Returns a details panel containing information about this bucket.
150       * @param id The wicket id for this panel.
151       * @param bucket The bucket of interest. 
152       * @param isCount True if the count should be returned, false if percentage. 
153       * @return The DailyProjectDetailsPanel instance. 
154       */
155      public DailyProjectDetailsPanel getPanel(String id, int bucket, boolean isCount) {
156        DailyProjectDetailsPanel dpdPanel = 
157          new DailyProjectDetailsPanel(id, "Coupling Data", 
158              ((isCount) ? this.getBucketCountString(bucket) : this.getBucketPercentageString(bucket)));
159        dpdPanel.getModalWindow().setContent(
160            new CouplingDetailsPanel(dpdPanel.getModalWindow().getContentId(),
161            buckets.get(bucket)));
162            
163        return dpdPanel;
164      }
165    }