001    package org.hackystat.projectbrowser.page.dailyprojectdata.filemetric;
002    
003    import java.io.Serializable;
004    import java.util.List;
005    import java.util.Map;
006    import java.util.TreeMap;
007    
008    import org.hackystat.dailyprojectdata.resource.filemetric.jaxb.FileData;
009    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
010    
011    /**
012     * Gets the DPD for total lines of code.
013     * @author Philip Johnson
014     */
015    public class FileMetricData implements Serializable {
016      
017      /** Support serialization. */
018      private static final long serialVersionUID = 1L;
019      
020      /** The project whose data is kept in this instance. */
021      private Project project;
022      
023      /** Holds the total LOC. */
024      private int totalLoc = 0;
025      
026      /** Maintains the (filetype, LOC) data. */
027      private Map<String, Integer> fileType2Loc = new TreeMap<String, Integer>(); 
028      
029      /**
030       * Constructs a FileMetricData instance with FileData data. 
031       * @param project The project associated with this DevTime data. 
032       * @param fileData The individual FileData data. 
033       */
034      public FileMetricData(Project project, List<FileData> fileData) {
035        this.project = project;
036        // Update fileType2Loc with the total LOC associated with this file type.
037        for (FileData data : fileData) {
038          String fileName = data.getFileUri();
039          int loc = (int) data.getSizeMetricValue();
040          this.totalLoc += loc;
041          // Files without a .suffix get file type 'unknown'.
042          String fileType = "unknown";
043          if (fileName.contains(".")) {
044            fileType = fileName.substring(fileName.lastIndexOf('.'));
045          }
046          if (fileType2Loc.get(fileType) == null) {
047            fileType2Loc.put(fileType, 0);
048          }
049          fileType2Loc.put(fileType, fileType2Loc.get(fileType) + loc);
050        }
051      }
052      
053      /**
054       * Constructs an empty FileMetricData instance.
055       * Used to initialize the session state.  
056       * @param project The project.
057       */
058      public FileMetricData(Project project) {
059        this.project = project;
060      }
061      
062      /**
063       * Returns the project associated with this instance. 
064       * @return The project. 
065       */
066      public Project getProject() {
067        return this.project;
068      }
069      
070      /**
071       * Returns the total aggregate LOC for this project on this day. 
072       * @return The total DevTime. 
073       */
074      public int getTotalLoc () {
075        return this.totalLoc;
076      }
077      
078      /**
079       * Returns a String listing each filetype and their LOC.
080       * @return A String of FileMetric info for this project and day. 
081       */
082      public String getFileMetricData() {
083        StringBuffer buff = new StringBuffer();
084        for (Map.Entry<String, Integer> entry : fileType2Loc.entrySet()) {
085          buff.append(String.format("%s(%d) ", entry.getKey(), entry.getValue()));
086        }
087        return buff.toString();
088      }
089    }