Skip to content

Commit

Permalink
SONAR-6620 remove Dto and BatchReport object from MeasureRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Jun 11, 2015
1 parent 5455fff commit 6abce51
Show file tree
Hide file tree
Showing 23 changed files with 2,412 additions and 225 deletions.
Expand Up @@ -51,6 +51,7 @@
import org.sonar.server.computation.language.PlatformLanguageRepository; import org.sonar.server.computation.language.PlatformLanguageRepository;
import org.sonar.server.computation.measure.MeasureRepositoryImpl; import org.sonar.server.computation.measure.MeasureRepositoryImpl;
import org.sonar.server.computation.measure.MetricCache; import org.sonar.server.computation.measure.MetricCache;
import org.sonar.server.computation.metric.MetricRepositoryImpl;
import org.sonar.server.computation.period.PeriodsHolderImpl; import org.sonar.server.computation.period.PeriodsHolderImpl;
import org.sonar.server.computation.step.ComputationStep; import org.sonar.server.computation.step.ComputationStep;
import org.sonar.server.computation.step.ComputationSteps; import org.sonar.server.computation.step.ComputationSteps;
Expand Down Expand Up @@ -123,18 +124,19 @@ private static List componentClasses() {
return Arrays.asList( return Arrays.asList(
ActivityManager.class, ActivityManager.class,


// holders
BatchReportDirectoryHolderImpl.class,
TreeRootHolderImpl.class, TreeRootHolderImpl.class,
PeriodsHolderImpl.class,


BatchReportReaderImpl.class, BatchReportReaderImpl.class,


BatchReportDirectoryHolderImpl.class,

// repositories // repositories
PlatformLanguageRepository.class, PlatformLanguageRepository.class,
MetricRepositoryImpl.class,
MeasureRepositoryImpl.class, MeasureRepositoryImpl.class,
EventRepositoryImpl.class, EventRepositoryImpl.class,
ProjectSettingsRepository.class, ProjectSettingsRepository.class,
PeriodsHolderImpl.class,
DbIdsRepository.class, DbIdsRepository.class,


// issues // issues
Expand Down
@@ -0,0 +1,118 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.measure;

import com.google.common.base.Optional;
import java.util.Objects;
import javax.annotation.Nullable;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.server.computation.metric.Metric;

public class BatchMeasureToMeasure {

public Optional<Measure> toMeasure(@Nullable BatchReport.Measure batchMeasure, Metric metric) {
Objects.requireNonNull(metric);
if (batchMeasure == null) {
return Optional.absent();
}

String data = batchMeasure.hasStringValue() ? batchMeasure.getStringValue() : null;
switch (metric.getMetricType().getValueType()) {
case INT:
return toIntegerMeasure(batchMeasure, data);
case LONG:
return toLongMeasure(batchMeasure, data);
case DOUBLE:
return toDoubleMeasure(batchMeasure, data);
case BOOLEAN:
return toBooleanMeasure(batchMeasure, data);
case STRING:
return toStringMeasure(batchMeasure);
case LEVEL:
return toLevelMeasure(batchMeasure);
case NO_VALUE:
return toNoValueMeasure(batchMeasure);
default:
throw new IllegalArgumentException("Unsupported Measure.ValueType " + metric.getMetricType().getValueType());
}
}

private static Optional<Measure> toIntegerMeasure(BatchReport.Measure batchMeasure, @Nullable String data) {
if (!batchMeasure.hasIntValue()) {
return toMeasure(MeasureImpl.createNoValue(), batchMeasure);
}
return toMeasure(MeasureImpl.create(batchMeasure.getIntValue(), data), batchMeasure);
}

private static Optional<Measure> toLongMeasure(BatchReport.Measure batchMeasure, @Nullable String data) {
if (!batchMeasure.hasLongValue()) {
return toMeasure(MeasureImpl.createNoValue(), batchMeasure);
}
return toMeasure(MeasureImpl.create(batchMeasure.getLongValue(), data), batchMeasure);
}

private static Optional<Measure> toDoubleMeasure(BatchReport.Measure batchMeasure, @Nullable String data) {
if (!batchMeasure.hasDoubleValue()) {
return toMeasure(MeasureImpl.createNoValue(), batchMeasure);
}
return toMeasure(MeasureImpl.create(batchMeasure.getDoubleValue(), data), batchMeasure);
}

private static Optional<Measure> toBooleanMeasure(BatchReport.Measure batchMeasure, @Nullable String data) {
if (!batchMeasure.hasBooleanValue()) {
return toMeasure(MeasureImpl.createNoValue(), batchMeasure);
}
return toMeasure(MeasureImpl.create(batchMeasure.getBooleanValue(), data), batchMeasure);
}

private static Optional<Measure> toStringMeasure(BatchReport.Measure batchMeasure) {
if (!batchMeasure.hasStringValue()) {
return toMeasure(MeasureImpl.createNoValue(), batchMeasure);
}
return toMeasure(MeasureImpl.create(batchMeasure.getStringValue()), batchMeasure);
}

private static Optional<Measure> toLevelMeasure(BatchReport.Measure batchMeasure) {
if (!batchMeasure.hasStringValue()) {
return toMeasure(MeasureImpl.createNoValue(), batchMeasure);
}
Optional<Measure.Level> level = Measure.Level.toLevel(batchMeasure.getStringValue());
if (!level.isPresent()) {
return toMeasure(MeasureImpl.createNoValue(), batchMeasure);
}
return toMeasure(MeasureImpl.create(level.get()), batchMeasure);
}

private static Optional<Measure> toNoValueMeasure(BatchReport.Measure batchMeasure) {
return toMeasure(MeasureImpl.createNoValue(), batchMeasure);
}

private static Optional<Measure> toMeasure(MeasureImpl measure, BatchReport.Measure batchMeasure) {
if (batchMeasure.hasAlertStatus() && !measure.hasQualityGateStatus()) {
Optional<Measure.Level> qualityGateStatus = Measure.Level.toLevel(batchMeasure.getAlertStatus());
if (qualityGateStatus.isPresent()) {
String text = batchMeasure.hasAlertText() ? batchMeasure.getAlertText() : null;
measure.setQualityGateStatus(new Measure.QualityGateStatus(qualityGateStatus.get(), text));
}
}
return Optional.of((Measure) measure);
}

}
@@ -0,0 +1,176 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.measure;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import static java.util.Objects.requireNonNull;

public interface Measure {

enum ValueType {
NO_VALUE, BOOLEAN, INT, LONG, DOUBLE, STRING, LEVEL
}

enum Level {
OK("Green"),
WARN("Orange"),
ERROR("Red");

private final String colorName;

Level(String colorName) {
this.colorName = colorName;
}

public String getColorName() {
return colorName;
}

public static Optional<Level> toLevel(@Nullable String level) {
if (level == null) {
return Optional.absent();
}
try {
return Optional.of(Level.valueOf(level));
} catch (IllegalArgumentException e) {
return Optional.absent();
}
}
}

/**
* A QualityGate status has a level and an optional describing text.
*/
@Immutable
final class QualityGateStatus {
private final Level status;
@CheckForNull
private final String text;

/**
* Creates a QualityGateStatus without a text.
*/
public QualityGateStatus(Level status) {
this(status, null);
}

/**
* Creates a QualityGateStatus with a optional text.
*/
public QualityGateStatus(Level status, @Nullable String text) {
this.status = requireNonNull(status);
this.text = text;
}

public Level getStatus() {
return status;
}

@CheckForNull
public String getText() {
return text;
}

@Override
public String toString() {
return Objects.toStringHelper(this)
.add("status", status)
.add("text", text)
.toString();
}
}

/**
* The type of value stored in the measure.
*/
ValueType getValueType();

/**
* The value of this measure as a boolean if the type is {@link Measure.ValueType#BOOLEAN}.
*
* @throws IllegalStateException if the value type of the measure is not {@link Measure.ValueType#BOOLEAN}
*/
boolean getBooleanValue();

/**
* The value of this measure as a int if the type is {@link Measure.ValueType#INT}.
*
* @throws IllegalStateException if the value type of the measure is not {@link Measure.ValueType#INT}
*/
int getIntValue();

/**
* The value of this measure as a long if the type is {@link Measure.ValueType#LONG}.
*
* @throws IllegalStateException if the value type of the measure is not {@link Measure.ValueType#LONG}
*/
long getLongValue();

/**
* The value of this measure as a double if the type is {@link Measure.ValueType#DOUBLE}.
*
* @throws IllegalStateException if the value type of the measure is not {@link Measure.ValueType#DOUBLE}
*/
double getDoubleValue();

/**
* The value of this measure as a String if the type is {@link Measure.ValueType#STRING}.
*
* @throws IllegalStateException if the value type of the measure is not {@link Measure.ValueType#STRING}
*/
String getStringValue();

/**
* The value of this measure as a Level if the type is {@link Measure.ValueType#LEVEL}.
*
* @throws IllegalStateException if the value type of the measure is not {@link Measure.ValueType#LEVEL}
*/
Level getLevelValue();

/**
* The data of this measure if it exists.
* <p>
* If the measure type is {@link Measure.ValueType#STRING}, the value returned by this function is the same as {@link #getStringValue()}.
* </p>
*
* @throws IllegalStateException if the value type of the measure is not {@link Measure.ValueType#BOOLEAN}
*/
@CheckForNull
String getData();

/**
* Any Measure, which ever is its value type, can have a QualityGate status.
*/
boolean hasQualityGateStatus();

/**
* The QualityGate status for this measure.
* <strong>Don't call this method unless you've checked the result of {@link #hasQualityGateStatus()} first</strong>
*
* @throws IllegalStateException if the measure has no QualityGate status
*/
QualityGateStatus getQualityGateStatus();

}

0 comments on commit 6abce51

Please sign in to comment.