001/*
002 * Copyright 2014 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.model.common.search;
017
018import java.util.List;
019
020import com.google.common.base.Objects;
021
022/**
023 * Represents a clustering of search results into categories.
024 * Each facet shows the number of hits within the search that match that category.
025 */
026public class Facet<T extends SearchParameter> {
027
028  private T field;
029  private List<Count> counts;
030
031  /**
032   * Null/Empty constructor.
033   */
034  public Facet() {
035  }
036
037  /**
038   * Minimal constructor.
039   */
040  public Facet(T field) {
041    this.field = field;
042  }
043
044  /**
045   * Full constructor.
046   */
047  public Facet(T field, List<Count> counts) {
048    this.field = field;
049    this.counts = counts;
050  }
051
052  /**
053   * List of the different categories/count for this facet.
054   */
055  public List<Count> getCounts() {
056    return counts;
057  }
058
059  /**
060   * Sets the list of counts/categories for this faceted field.
061   */
062  public void setCounts(List<Count> counts) {
063    this.counts = counts;
064  }
065
066  /**
067   * The facet field, it's used only for identify the facet in a possible list of facets.
068   */
069  public T getField() {
070    return field;
071  }
072
073  /**
074   * Sets the field of the facet.
075   */
076  public void setField(T field) {
077    this.field = field;
078  }
079
080  @Override
081  public boolean equals(Object obj) {
082    if (this == obj) {
083      return true;
084    }
085    if (!(obj instanceof Facet)) {
086      return false;
087    }
088
089    Facet that = (Facet) obj;
090    return Objects.equal(this.counts, that.getCounts()) && Objects.equal(this.field, that.getField());
091  }
092
093  @Override
094  public int hashCode() {
095    return Objects.hashCode(super.hashCode(), counts, field);
096  }
097
098  @Override
099  public String toString() {
100    return Objects.toStringHelper(this).add("field", field).add("counts", counts).toString();
101  }
102
103  /**
104   * Nested class that represents the count of each category.
105   */
106  public static class Count {
107
108    private String name;
109    private Long count;
110
111    /**
112     * Null/empty constructor.
113     */
114    public Count() {
115    }
116
117    /**
118     * Full constructor.
119     *
120     * @param name  or label of the category
121     * @param count number of elements of this facet category
122     */
123    public Count(String name, Long count) {
124      this.name = name;
125      this.count = count;
126    }
127
128    /**
129     * Number of occurrences of the category(name).
130     */
131    public Long getCount() {
132      return count;
133    }
134
135    /**
136     * Sets the count of occurrences for this category.
137     */
138    public void setCount(Long count) {
139      this.count = count;
140    }
141
142    /**
143     * Name is the label/name/title used to distinguish this category in the list of counts.
144     * Each name should be unique in the list.
145     *
146     * @return the name or label of the category
147     */
148    public String getName() {
149      return name;
150    }
151
152    /**
153     * Sets the name value of the category/count.
154     */
155    public void setName(String name) {
156      this.name = name;
157    }
158
159    @Override
160    public boolean equals(Object obj) {
161      if (this == obj) {
162        return true;
163      }
164      if (!(obj instanceof Count)) {
165        return false;
166      }
167
168      Count that = (Count) obj;
169      return Objects.equal(this.count, that.getCount()) && Objects.equal(this.name, that.getName());
170    }
171
172    @Override
173    public int hashCode() {
174      return Objects.hashCode(super.hashCode(), count, name);
175    }
176
177    @Override
178    public String toString() {
179      return Objects.toStringHelper(this).add("name", name).add("count", count).toString();
180    }
181
182  }
183
184}