001    package org.hackystat.dailyprojectdata.resource.commit;
002    
003    import java.util.List;
004    
005    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
006    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
007    
008    /**
009     * The class wrapping SensorData instance, which provides easy access to the
010     * commit specific properties.
011     * @author aito
012     * 
013     */
014    public class CommitData {
015      /** The wrapped data instance. */
016      private final SensorData data;
017    
018      /**
019       * Constructs this object with the specified SensorData instance.
020       * @param data the specified data instance.
021       */
022      public CommitData(SensorData data) {
023        this.data = data;
024      }
025    
026      /**
027       * Returns the owner of the wrapped data instance.
028       * @return the data owner.
029       */
030      public String getOwner() {
031        return this.data.getOwner();
032      }
033    
034      /**
035       * Returns the total lines added stored in this data instance.
036       * @return the total lines added.
037       */
038      public int getLinesAdded() {
039        return Integer.valueOf(this.getCommitProperty("linesAdded").getValue());
040      }
041    
042      /**
043       * Returns the total lines deleted stored in this data instance.
044       * @return the total lines deleted.
045       */
046      public int getLinesDeleted() {
047        return Integer.valueOf(this.getCommitProperty("linesDeleted").getValue());
048      }
049    
050      /**
051       * Returns the total lines modified in this data instance.
052       * Note that linesModified is an optional property, so this will return 0 if not present. 
053       * @return the total lines modified
054       */
055      public int getLinesModified() {
056        return Integer.valueOf(this.getCommitProperty("linesModified", "0").getValue());
057      }
058    
059      /**
060       * Returns true if the specified object equals this object.
061       * @param object the object to test.
062       * @return true if equal, false if not.
063       */
064      @Override
065      public boolean equals(Object object) {
066        if (this == object) {
067          return true;
068        }
069        if (!(object instanceof CommitData)) {
070          return false;
071        }
072    
073        CommitData otherData = (CommitData) object;
074        return this.data.equals(otherData.data);
075      }
076    
077      /**
078       * Returns the hashcode of this object.
079       * @return the hashcode.
080       */
081      @Override
082      public int hashCode() {
083        int result = 17;
084        result = 37 * result + this.data.hashCode();
085        return result;
086      }
087    
088      /**
089       * Returns the Property instance with the specified property name. If no
090       * property exists, null is returned.
091       * @param propertyName the property name to search for.
092       * @return the property with the specified name or null.
093       */
094      public Property getCommitProperty(String propertyName) {
095        List<Property> propertyList = this.data.getProperties().getProperty();
096        for (Property property : propertyList) {
097          if (propertyName.equals(property.getKey())) {
098            return property;
099          }
100        }
101        return null;
102      }
103      
104      /**
105       * Returns the Property instance with the specified property name. If no
106       * property exists, defaultValue is returned. 
107       * @param propertyName the property name to search for.
108       * @param defaultValue The string to return if the property does not exist. 
109       * @return The property with the specified name or defaultValue. 
110       */
111      public Property getCommitProperty(String propertyName, String defaultValue) {
112        List<Property> propertyList = this.data.getProperties().getProperty();
113        for (Property property : propertyList) {
114          if (propertyName.equals(property.getKey())) {
115            return property;
116          }
117        }
118        // Make a new Property instance.
119        Property property = new Property();
120        property.setKey(propertyName);
121        property.setValue(defaultValue);
122        return property;
123      }
124    
125      /**
126       * Returns the string representation of this data object, which is useful for
127       * debugging purposes.
128       * @return the string representation.
129       */
130      @Override
131      public String toString() {
132        return "Owner=" + this.getOwner() + ", LinesAdded=" + this.getLinesAdded()
133            + ", LinesDeleted=" + this.getLinesDeleted() + ", LinesModified="
134            + this.getLinesModified();
135      }
136    }