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.api.violations;
021
022import org.sonar.api.resources.Resource;
023
024/**
025 * Class that allows to query the Sonar index about violations.
026 * 
027 * @since 2.8
028 */
029public final class ViolationQuery {
030
031  public static enum SwitchMode {
032    OFF, ON, BOTH
033  }
034
035  private SwitchMode switchMode = SwitchMode.ON;
036  private Resource resource;
037
038  /**
039   * Use the factory method <code>create()</code>
040   */
041  ViolationQuery() {
042  }
043
044  /**
045   * Creates a new {@link ViolationQuery} object.
046   * 
047   * @return the new query
048   */
049  public static ViolationQuery create() {
050    return new ViolationQuery();
051  }
052
053  /**
054   * Specifies if the query should return only switched-off violations.
055   * 
056   * @param b
057   *          if true, the query will return only switched-off violations. if false, it will return only active violations.
058   * @return the current violation query
059   */
060  public ViolationQuery setSwitchedOff(boolean b) {
061    this.switchMode = (b ? SwitchMode.OFF : SwitchMode.ON);
062    return this;
063  }
064
065  /**
066   * Tells if the query should return only switched-off violations.
067   * 
068   * @return
069   */
070  public boolean isSwitchedOff() {
071    return switchMode == SwitchMode.OFF;
072  }
073
074  public SwitchMode getSwitchMode() {
075    return switchMode;
076  }
077
078  public ViolationQuery setSwitchMode(SwitchMode s) {
079    this.switchMode = s;
080    return this;
081  }
082
083  /**
084   * Specifies the resource which violations are search from.
085   * 
086   * @param resource
087   *          the resource
088   */
089  public ViolationQuery forResource(Resource resource) {
090    this.resource = resource;
091    return this;
092  }
093
094  /**
095   * Returns the resource which violations are search from.
096   * 
097   * @return the resource
098   */
099  public Resource getResource() {
100    return resource;
101  }
102}