001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.web;
021
022import com.google.common.base.Preconditions;
023
024import java.util.ArrayList;
025import java.util.List;
026
027/**
028 * Definition of a filter.
029 *
030 * <p>Its name can be retrieved using the i18n mechanism, using the keys "filter.&lt;id&gt;.name".
031 *
032 * @since 3.1
033 */
034public final class Filter {
035  public static final String LIST = "list";
036  public static final String TREEMAP = "treemap";
037
038  private boolean favouritesOnly;
039  private String displayAs;
040  private int pageSize;
041  private List<Criterion> criteria;
042  private List<FilterColumn> columns;
043
044  private Filter() {
045    displayAs = LIST;
046    criteria = new ArrayList<>();
047    columns = new ArrayList<>();
048  }
049
050  /**
051   * Creates a new {@link Filter}.
052   */
053  public static Filter create() {
054    return new Filter();
055  }
056
057  /**
058   * Get the list of {@link Criterion} used to narrow down the results of this {@link Filter}.
059   * 
060   * @return the criteria
061   */
062  public List<Criterion> getCriteria() {
063    return criteria;
064  }
065
066  /**
067   * Add a {@link Criterion} to the list used to narrow down the results of this {@link Filter}.
068   * 
069   * @return this filter
070   */
071  public Filter add(Criterion criterion) {
072    this.criteria.add(criterion);
073    return this;
074  }
075
076  /**
077   * Get the list of {@link FilterColumn} displayed by this {@link Filter}.
078   * 
079   * @return this columns
080   */
081  public List<FilterColumn> getColumns() {
082    return columns;
083  }
084
085  /**
086   * Add a {@link FilterColumn} to the list of columns displayed by this {@link Filter}.
087   * 
088   * @return this filter
089   */
090  public Filter add(FilterColumn column) {
091    this.columns.add(column);
092    return this;
093  }
094
095  /**
096   * The {@link Filter} can be configured to return only favourites.
097   * 
098   * @return <code>true</code> if favourites only are returned
099   */
100  public boolean isFavouritesOnly() {
101    return favouritesOnly;
102  }
103
104  /**
105   * The {@link Filter} can be configured to return only favourites.
106   */
107  public Filter setFavouritesOnly(boolean favouritesOnly) {
108    this.favouritesOnly = favouritesOnly;
109    return this;
110  }
111
112  /**
113   * Get the type of display used by this {@link Filter}.
114   * 
115   * <p>Can be either {@code #LIST} or {@code #TREEMAP}
116   * 
117   * @return the display type
118   */
119  public String getDisplayAs() {
120    return displayAs;
121  }
122
123  /**
124   * Set the type of display used by this {@link Filter}.
125   * 
126   * <p>Can be either {@code #LIST} or {@code #TREEMAP}
127   * 
128   * @return this filter
129   * @throws IllegalArgumentException if {@code displayAs} is not {@code #LIST} or {@code #TREEMAP}
130   */
131  public Filter setDisplayAs(String displayAs) {
132    Preconditions.checkArgument(LIST.equals(displayAs) || TREEMAP.equals(displayAs), "Default display should be either %s or %s, not %s", LIST, TREEMAP, displayAs);
133    this.displayAs = displayAs;
134    return this;
135  }
136
137  /**
138   * Get the size of a page displayed this {@link Filter}.
139   * 
140   * <p>The page size is between <code>20</code> and <code>200</code> (included)
141   * 
142   * @return the display type
143   */
144  public int getPageSize() {
145    return pageSize;
146  }
147
148  /**
149   * Set the size of a page displayed this {@link Filter}.
150   * 
151   * <p>The page size should be between <code>20</code> and <code>200</code> (included)
152   * 
153   * @return the display type
154   * @throws IllegalArgumentException if {@code pageSize} is not lower than {@code 20} or greater than {@code 200}
155   */
156  public Filter setPageSize(int pageSize) {
157    Preconditions.checkArgument((pageSize >= 20) && (pageSize <= 200), "page size should be between 20 and 200");
158    this.pageSize = pageSize;
159    return this;
160  }
161}