3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.measure;
22 import com.google.common.base.Predicate;
23 import com.google.common.base.Predicates;
24 import javax.annotation.Nonnull;
25 import org.sonar.server.computation.task.projectanalysis.component.Component;
26 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
28 import static java.util.Objects.requireNonNull;
29 import static org.apache.commons.lang.math.NumberUtils.compare;
30 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.ValueType.NO_VALUE;
32 public class BestValueOptimization implements Predicate<Measure> {
33 private final Metric metric;
35 private BestValueOptimization(Metric metric) {
36 this.metric = requireNonNull(metric);
39 public static Predicate<Measure> from(Metric metric, Component component) {
40 if (isBestValueOptimized(metric) && isBestValueOptimized(component)) {
41 return new BestValueOptimization(metric);
43 return Predicates.alwaysFalse();
46 private static boolean isBestValueOptimized(Metric metric) {
47 return metric.isBestValueOptimized();
50 private static boolean isBestValueOptimized(Component component) {
51 return component.getType() == Component.Type.FILE;
55 public boolean apply(@Nonnull Measure measure) {
56 return isBestValueOptimized(measure);
59 private boolean isBestValueOptimized(Measure measure) {
60 return measure.getData() == null
61 && !measure.hasQualityGateStatus()
62 && hasNoVariation(measure, metric.getBestValue())
63 && (measure.getValueType() == NO_VALUE || isBestValue(measure, metric.getBestValue()));
66 private static boolean hasNoVariation(Measure measure, Double bestValue) {
67 return !measure.hasVariation() || isVariationEmptyOrBestValue(measure.getVariation(), bestValue);
70 private static boolean isVariationEmptyOrBestValue(double variation, Double bestValue) {
71 return compare(variation, 0d) == 0 || compare(variation, bestValue) == 0;
74 private static boolean isBestValue(Measure measure, Double bestValue) {
75 switch (measure.getValueType()) {
77 return bestValue.intValue() == 1 ? measure.getBooleanValue() : !measure.getBooleanValue();
79 return bestValue.intValue() == measure.getIntValue();
81 return bestValue.longValue() == measure.getLongValue();
83 return bestValue.compareTo(measure.getDoubleValue()) == 0;