001    package org.hackystat.dailyprojectdata.resource.coverage;
002    
003    import java.util.List;
004    import java.util.Locale;
005    
006    import javax.xml.datatype.XMLGregorianCalendar;
007    
008    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
009    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
010    
011    /**
012     * The class wrapping SensorData instance, which provides easy access to the
013     * coverage specific properties.
014     * @author aito
015     * 
016     */
017    public class CoverageData {
018      /** The wrapped data instance. */
019      private final SensorData data;
020      /** The string used to mark the class level granularity of coverage data. */
021      public static final String GRANULARITY_CLASS = "class";
022      /** The string used to mark the block level granularity of coverage data. */
023      public static final String GRANULARITY_BLOCK = "block";
024      /** The string used to mark the method level granularity of coverage data. */
025      public static final String GRANULARITY_METHOD = "method";
026      /** The string used to mark the line level granularity of coverage data. */
027      public static final String GRANULARITY_LINE = "line";
028      /** The property name of the "uncovered" coverage values. */
029      private static final String UNCOVERED_NAME = "Uncovered";
030      /** The property name of the "covered" coverage values. */
031      private static final String COVERED_NAME = "Covered";
032      /**
033       * The seperator between the granularity and covered and uncovered data
034       * strings. For example, "line_Uncovered".
035       */
036      private static final String SEPERATOR = "_";
037    
038      /**
039       * Constructs this object with the specified SensorData instance.
040       * @param data the specified data instance.
041       */
042      public CoverageData(SensorData data) {
043        this.data = data;
044      }
045    
046      /**
047       * Returns the resource of the wrapped data instance.
048       * @return the data resource.
049       */
050      public String getResource() {
051        return this.data.getResource();
052      }
053    
054      /**
055       * Returns the owner of the wrapped data instance.
056       * @return the data owner.
057       */
058      public String getOwner() {
059        return this.data.getOwner();
060      }
061    
062      /**
063       * Returns the uncovered coverage value. This method assumes that the
064       * uncovered data is stored as a property in the following format:
065       * 'granularity_Uncovered' where granularity is a lower case string.
066       * @param granularity the level of uncovered data to return.
067       * @return the uncovered value.
068       */
069      public int getUncovered(String granularity) {
070        String lowerCaseGranularity = granularity.toLowerCase(Locale.ENGLISH);
071        String coverageProperty = this.getCoverageProperty(
072            lowerCaseGranularity + SEPERATOR + UNCOVERED_NAME).getValue();
073        return Double.valueOf(coverageProperty).intValue();
074      }
075    
076      /**
077       * Returns the covered coverage value. This method assumes that the covered
078       * data is stored as a property in the following format: 'granularity_Covered'
079       * where granularity is a lower case string.
080       * @param granularity the level of covered data to return.
081       * @return the covered value.
082       */
083      public int getCovered(String granularity) {
084        String lowerCaseGranularity = granularity.toLowerCase(Locale.ENGLISH);
085        String coverageProperty = this.getCoverageProperty(
086            lowerCaseGranularity + SEPERATOR + COVERED_NAME).getValue();
087        return Double.valueOf(coverageProperty).intValue();
088      }
089    
090      /**
091       * Returns true if the specified object equals this object.
092       * @param object the object to test.
093       * @return true if equal, false if not.
094       */
095      @Override
096      public boolean equals(Object object) {
097        if (this == object) {
098          return true;
099        }
100        if (!(object instanceof CoverageData)) {
101          return false;
102        }
103    
104        CoverageData otherData = (CoverageData) object;
105        return this.data.equals(otherData.data);
106      }
107    
108      /**
109       * Returns the hashcode of this object.
110       * @return the hashcode.
111       */
112      @Override
113      public int hashCode() {
114        int result = 17;
115        result = 37 * result + this.data.hashCode();
116        return result;
117      }
118    
119      /**
120       * Returns the Property instance with the specified property name. If no
121       * property exists, false is returned.
122       * @param propertyName the property name to search for.
123       * @return the property with the specified name or null.
124       */
125      public Property getCoverageProperty(String propertyName) {
126        List<Property> propertyList = this.data.getProperties().getProperty();
127        for (Property property : propertyList) {
128          if (propertyName.equals(property.getKey())) {
129            return property;
130          }
131        }
132        return null;
133      }
134    
135      /**
136       * Returns the runtime of this data instance.
137       * @return the runtime of this data.
138       */
139      public XMLGregorianCalendar getRuntime() {
140        return this.data.getRuntime();
141      }
142    
143      /**
144       * Returns the string representation of this data object, which is useful for
145       * debugging purposes.
146       * @return the string representation.
147       */
148      @Override
149      public String toString() {
150        return "Owner=" + this.getOwner() + ", Resource=" + this.getResource() + "line_covered="
151            + this.getCovered(GRANULARITY_LINE) + ", line_uncovered="
152            + this.getUncovered(GRANULARITY_LINE) + ", method_covered="
153            + this.getCovered(GRANULARITY_METHOD) + ", method_uncovered="
154            + this.getUncovered(GRANULARITY_METHOD) + ", block_covered="
155            + this.getCovered(GRANULARITY_BLOCK) + ", block_uncovered="
156            + this.getUncovered(GRANULARITY_BLOCK) + ", class_covered="
157            + this.getCovered(GRANULARITY_CLASS) + ", class_uncovered="
158            + this.getUncovered(GRANULARITY_CLASS);
159      }
160    }