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.sonar.api.batch.fs.InputFile;
024    import org.sonar.api.batch.measure.Metric;
025    import org.sonar.api.batch.sensor.measure.MeasureBuilder;
026    
027    import java.io.Serializable;
028    
029    public class DefaultMeasureBuilder<G extends Serializable> implements MeasureBuilder<G> {
030    
031      boolean onProject = false;
032      InputFile file;
033      Metric<G> metric;
034      G value;
035    
036      @Override
037      public DefaultMeasureBuilder<G> onFile(InputFile inputFile) {
038        Preconditions.checkState(!this.onProject, "onProject already called");
039        Preconditions.checkState(this.file == null, "onFile already called");
040        Preconditions.checkNotNull(inputFile, "InputFile should be non null");
041        this.file = inputFile;
042        return this;
043      }
044    
045      @Override
046      public DefaultMeasureBuilder<G> onProject() {
047        Preconditions.checkState(!this.onProject, "onProject already called");
048        Preconditions.checkState(this.file == null, "onFile already called");
049        this.onProject = true;
050        return this;
051      }
052    
053      @Override
054      public DefaultMeasureBuilder<G> forMetric(Metric<G> metric) {
055        Preconditions.checkState(metric != null, "Metric already defined");
056        Preconditions.checkNotNull(metric, "metric should be non null");
057        this.metric = metric;
058        return this;
059      }
060    
061      @Override
062      public DefaultMeasureBuilder<G> withValue(G value) {
063        Preconditions.checkState(this.value == null, "Measure value already defined");
064        Preconditions.checkNotNull(value, "Measure value can't be null");
065        this.value = value;
066        return this;
067      }
068    
069      @Override
070      public DefaultMeasure<G> build() {
071        DefaultMeasure<G> result = new DefaultMeasure<G>(this);
072        reset();
073        return result;
074      }
075    
076      private void reset() {
077        onProject = false;
078        file = null;
079        metric = null;
080        value = null;
081      }
082    }