001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.plugins.core.sensors;
021
022import org.apache.commons.lang.NotImplementedException;
023import org.apache.commons.lang.StringUtils;
024import org.sonar.api.measures.Measure;
025import org.sonar.api.measures.Metric;
026import org.sonar.api.profiles.Alert;
027
028public final class AlertUtils {
029  private AlertUtils() {
030  }
031
032  /**
033   * Get the matching alert level for the given measure
034   */
035  public static Metric.Level getLevel(Alert alert, Measure measure) {
036    if (evaluateAlert(alert, measure, Metric.Level.ERROR)) {
037      return Metric.Level.ERROR;
038    }
039    if (evaluateAlert(alert, measure, Metric.Level.WARN)) {
040      return Metric.Level.WARN;
041    }
042    return Metric.Level.OK;
043  }
044
045  private static boolean evaluateAlert(Alert alert, Measure measure, Metric.Level alertLevel) {
046    String valueToEval;
047    if (alertLevel.equals(Metric.Level.ERROR)) {
048      valueToEval = alert.getValueError();
049
050    } else if (alertLevel.equals(Metric.Level.WARN)) {
051      valueToEval = alert.getValueWarning();
052
053    } else {
054      throw new IllegalStateException(alertLevel.toString());
055    }
056
057    if (StringUtils.isEmpty(valueToEval)) {
058      return false;
059    }
060
061    Comparable criteriaValue = getValueForComparison(alert.getMetric(), valueToEval);
062    Comparable metricValue = getMeasureValue(alert.getMetric(), measure);
063
064    int comparison = metricValue.compareTo(criteriaValue);
065    return !(// NOSONAR complexity of this boolean expression is under control
066        (alert.isNotEqualsOperator() && comparison == 0) ||
067        (alert.isGreaterOperator() && comparison != 1) ||
068        (alert.isSmallerOperator() && comparison != -1) ||
069        (alert.isEqualsOperator() && comparison != 0)
070    );
071  }
072
073
074  private static Comparable<?> getValueForComparison(Metric metric, String value) {
075    if (metric.getType() == Metric.ValueType.FLOAT ||
076        metric.getType() == Metric.ValueType.PERCENT) {
077      return Double.parseDouble(value);
078    }
079    if (metric.getType() == Metric.ValueType.INT ||
080        metric.getType() == Metric.ValueType.MILLISEC) {
081      return value.contains(".") ? Integer.parseInt(value.substring(0, value.indexOf('.'))) : Integer.parseInt(value);
082    }
083    if (metric.getType() == Metric.ValueType.STRING ||
084        metric.getType() == Metric.ValueType.LEVEL) {
085      return value;
086    }
087    if (metric.getType() == Metric.ValueType.BOOL) {
088      return Integer.parseInt(value);
089    }
090    if (metric.getType() == Metric.ValueType.RATING) {
091      return Double.parseDouble(value);
092    }
093    throw new NotImplementedException(metric.getType().toString());
094  }
095
096  private static Comparable<?> getMeasureValue(Metric metric, Measure measure) {
097    if (metric.getType() == Metric.ValueType.FLOAT ||
098        metric.getType() == Metric.ValueType.PERCENT) {
099      return measure.getValue();
100    }
101    if (metric.getType() == Metric.ValueType.INT ||
102        metric.getType() == Metric.ValueType.MILLISEC) {
103      return measure.getValue().intValue();
104    }
105    if (metric.getType() == Metric.ValueType.STRING ||
106        metric.getType() == Metric.ValueType.LEVEL) {
107      return measure.getData();
108    }
109    if (metric.getType() == Metric.ValueType.BOOL) {
110      return measure.getValue().intValue();
111    }
112    if (metric.getType() == Metric.ValueType.RATING) {
113      return measure.getValue();
114    }
115    throw new NotImplementedException(metric.getType().toString());
116  }
117}