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.sensor.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.sonar.api.batch.fs.InputFile;
026    import org.sonar.api.batch.measure.Metric;
027    import org.sonar.api.batch.sensor.SensorStorage;
028    import org.sonar.api.batch.sensor.internal.DefaultStorable;
029    import org.sonar.api.batch.sensor.measure.Measure;
030    
031    import javax.annotation.CheckForNull;
032    import javax.annotation.Nullable;
033    
034    import java.io.Serializable;
035    
036    public class DefaultMeasure<G extends Serializable> extends DefaultStorable implements Measure<G> {
037    
038      private boolean onProject = false;
039      private InputFile file;
040      private Metric<G> metric;
041      private G value;
042      private boolean fromCore = false;
043    
044      public DefaultMeasure() {
045        super();
046      }
047    
048      public DefaultMeasure(@Nullable SensorStorage storage) {
049        super(storage);
050      }
051    
052      @Override
053      public DefaultMeasure<G> onFile(InputFile inputFile) {
054        Preconditions.checkState(!this.onProject, "onProject already called");
055        Preconditions.checkState(this.file == null, "onFile already called");
056        Preconditions.checkNotNull(inputFile, "InputFile should be non null");
057        this.file = inputFile;
058        return this;
059      }
060    
061      @Override
062      public DefaultMeasure<G> onProject() {
063        Preconditions.checkState(!this.onProject, "onProject already called");
064        Preconditions.checkState(this.file == null, "onFile already called");
065        this.onProject = true;
066        return this;
067      }
068    
069      @Override
070      public DefaultMeasure<G> forMetric(Metric<G> metric) {
071        Preconditions.checkState(metric != null, "Metric already defined");
072        Preconditions.checkNotNull(metric, "metric should be non null");
073        this.metric = metric;
074        return this;
075      }
076    
077      @Override
078      public DefaultMeasure<G> withValue(G value) {
079        Preconditions.checkState(this.value == null, "Measure value already defined");
080        Preconditions.checkNotNull(value, "Measure value can't be null");
081        this.value = value;
082        return this;
083      }
084    
085      /**
086       * For internal use.
087       */
088      public boolean isFromCore() {
089        return fromCore;
090      }
091    
092      /**
093       * For internal use. Used by core components to bypass check that prevent a plugin to store core measures.
094       */
095      public DefaultMeasure<G> setFromCore() {
096        this.fromCore = true;
097        return this;
098      }
099    
100      @Override
101      public void doSave() {
102        Preconditions.checkNotNull(this.value, "Measure value can't be null");
103        Preconditions.checkNotNull(this.metric, "Measure metric can't be null");
104        Preconditions.checkState(this.metric.valueType().equals(this.value.getClass()), "Measure value should be of type " + this.metric.valueType());
105        storage.store((Measure<Serializable>) this);
106      }
107    
108      @Override
109      public Metric<G> metric() {
110        return metric;
111      }
112    
113      @Override
114      @CheckForNull
115      public InputFile inputFile() {
116        return file;
117      }
118    
119      @Override
120      public G value() {
121        return value;
122      }
123    
124      // For testing purpose
125    
126      @Override
127      public boolean equals(Object obj) {
128        if (obj == null) {
129          return false;
130        }
131        if (obj == this) {
132          return true;
133        }
134        if (obj.getClass() != getClass()) {
135          return false;
136        }
137        DefaultMeasure rhs = (DefaultMeasure) obj;
138        return new EqualsBuilder()
139          .append(file, rhs.file)
140          .append(metric, rhs.metric)
141          .append(value, rhs.value)
142          .isEquals();
143      }
144    
145      @Override
146      public int hashCode() {
147        return new HashCodeBuilder(27, 45).
148          append(file).
149          append(metric).
150          append(value).
151          toHashCode();
152      }
153    
154    }