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 */ 020 021package org.sonar.squid.indexer; 022 023import org.apache.commons.lang.math.NumberUtils; 024import org.sonar.squid.api.Query; 025import org.sonar.squid.api.SourceCode; 026import org.sonar.squid.measures.Metric; 027import org.sonar.squid.measures.MetricDef; 028 029public class QueryByMeasure implements Query { 030 031 private final MetricDef metric; 032 private final Operator operator; 033 private final double value; 034 035 public enum Operator { 036 GREATER_THAN, EQUALS, GREATER_THAN_EQUALS, LESS_THAN, LESS_THAN_EQUALS 037 } 038 039 /** 040 * @deprecated use {@link #QueryByMeasure(MetricDef, Operator, double)} instead 041 */ 042 @Deprecated 043 public QueryByMeasure(Metric metric, Operator operator, double value) { 044 this((MetricDef) metric, operator, value); 045 } 046 047 public QueryByMeasure(MetricDef metric, Operator operator, double value) { 048 this.metric = metric; 049 this.operator = operator; 050 this.value = value; 051 } 052 053 public boolean match(SourceCode unit) { 054 switch (operator) { 055 case EQUALS: 056 return NumberUtils.compare(unit.getDouble(metric), value)==0; 057 case GREATER_THAN: 058 return unit.getDouble(metric) > value; 059 case GREATER_THAN_EQUALS: 060 return unit.getDouble(metric) >= value; 061 case LESS_THAN_EQUALS: 062 return unit.getDouble(metric) <= value; 063 case LESS_THAN: 064 return unit.getDouble(metric) < value; 065 default: 066 throw new IllegalStateException("The operator value '" + operator + "' is unknown."); 067 } 068 } 069 070}