001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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 */
020package org.sonar.api.web;
021
022import com.google.common.collect.ImmutableSortedSet;
023
024import java.util.Set;
025
026import com.google.common.base.Preconditions;
027
028/**
029 * Definition of a {@link Filter} column.
030 *
031 * @since 3.1
032 */
033public final class FilterColumn {
034  public static final String ASC = "ASC";
035  public static final String DESC = "DESC";
036  public static final Set<String> DIRECTIONS = ImmutableSortedSet.of(ASC, DESC);
037
038  private final String family;
039  private final String key;
040  private final String sortDirection;
041  private final boolean variation;
042
043  private FilterColumn(String family, String key, String sortDirection, boolean variation) {
044    Preconditions.checkArgument(DIRECTIONS.contains(sortDirection), "Valid directions are %s, not '%s'", DIRECTIONS, sortDirection);
045
046    this.family = family;
047    this.key = key;
048    this.sortDirection = sortDirection;
049    this.variation = variation;
050  }
051
052  /**
053   * Creates a new {@link FilterColumn}.
054   *
055   * <p>Valid values for the {@code sortDirection} are {@code #ASC}, {@code #DESC}</p>
056   *
057   * <p>When the @{see Filter} is persisted, a validation is made on the {@code family} and the {@code key}.
058   * They should point to a valid column description.</p>
059   *
060   * @throws IllegalArgumentException if {@code sortDirection} is not valid
061   */
062  public static FilterColumn create(String family, String key, String sortDirection, boolean variation) {
063    return new FilterColumn(family, key, sortDirection, variation);
064  }
065
066  /**
067   * Get the the column's family.
068   * 
069   * @return the family
070   */
071  public String getFamily() {
072    return family;
073  }
074
075  /**
076   * Get the the column's key.
077   * 
078   * @return the key
079   */
080  public String getKey() {
081    return key;
082  }
083
084  /**
085   * Get the the column's sort direction.
086   * 
087   * @return the sort direction
088   */
089  public String getSortDirection() {
090    return sortDirection;
091  }
092
093  /**
094   * A column can be based on the variation of a value rather than on the value itself.
095   * 
096   * @return <code>true</code> when the variation is used rather than the value
097   */
098  public boolean isVariation() {
099    return variation;
100  }
101}