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