001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.api.batch.analyzer.measure.internal;
021    
022    import com.google.common.base.Preconditions;
023    import org.apache.commons.lang.builder.EqualsBuilder;
024    import org.apache.commons.lang.builder.HashCodeBuilder;
025    import org.apache.commons.lang.builder.ToStringBuilder;
026    import org.apache.commons.lang.builder.ToStringStyle;
027    import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
028    import org.sonar.api.batch.fs.InputFile;
029    import org.sonar.api.batch.measure.Metric;
030    
031    import javax.annotation.Nullable;
032    
033    import java.io.Serializable;
034    
035    public class DefaultAnalyzerMeasure<G extends Serializable> implements AnalyzerMeasure<G>, Serializable {
036    
037      private final InputFile inputFile;
038      private final Metric<G> metric;
039      private final G value;
040    
041      DefaultAnalyzerMeasure(DefaultAnalyzerMeasureBuilder<G> builder) {
042        Preconditions.checkNotNull(builder.value, "Measure value can't be null");
043        Preconditions.checkNotNull(builder.metric, "Measure metric can't be null");
044        Preconditions.checkState(builder.metric.valueType().equals(builder.value.getClass()), "Measure value should be of type " + builder.metric.valueType());
045        this.inputFile = builder.file;
046        this.metric = builder.metric;
047        this.value = builder.value;
048      }
049    
050      @Nullable
051      @Override
052      public InputFile inputFile() {
053        return inputFile;
054      }
055    
056      @Override
057      public Metric<G> metric() {
058        return metric;
059      }
060    
061      @Override
062      public G value() {
063        return value;
064      }
065    
066      @Override
067      public boolean equals(Object obj) {
068        if (obj == null) {
069          return false;
070        }
071        if (obj == this) {
072          return true;
073        }
074        if (obj.getClass() != getClass()) {
075          return false;
076        }
077        DefaultAnalyzerMeasure rhs = (DefaultAnalyzerMeasure) obj;
078        return new EqualsBuilder()
079          .append(inputFile, rhs.inputFile)
080          .append(metric, rhs.metric)
081          .append(value, rhs.value)
082          .isEquals();
083      }
084    
085      @Override
086      public int hashCode() {
087        return new HashCodeBuilder(27, 45).
088          append(inputFile).
089          append(metric).
090          append(value).
091          toHashCode();
092      }
093    
094      @Override
095      public String toString() {
096        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
097      }
098    
099    }