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 org.gbif.api.model.common.paging.Pageable;
019import org.gbif.api.model.common.paging.PagingResponse;
020
021import java.util.List;
022
023import com.google.common.base.MoreObjects;
024import com.google.common.base.Objects;
025import com.google.common.collect.Lists;
026
027/**
028 * Generic response of a search operation. The result is a {@link java.util.List} of elements.
029 * The type of the elements is bounded by the type parameter T. The list of results can be a empty list.
030 * Optionally the response can contain: a count of the total returned elements, an offset, a pageSize and
031 * the list of facets (if those were requested).
032 *
033 * @param <T> the actual type of response content
034 */
035public class SearchResponse<T, P extends SearchParameter> extends PagingResponse<T> {
036
037  private final List<Facet<P>> facets = Lists.newArrayList();
038
039  private SpellCheckResponse spellCheckResponse;
040
041  /**
042   * Default constructor.
043   */
044  public SearchResponse() {
045  }
046
047  /**
048   * Minimal paging constructor.
049   */
050  public SearchResponse(Pageable page) {
051    super(page);
052  }
053
054  /**
055   * Minimal paging constructor.
056   */
057  public SearchResponse(PagingResponse<T> response) {
058    super(response, response.getCount(), response.getResults());
059  }
060
061  /**
062   * Minimal paging constructor.
063   */
064  public SearchResponse(long offset, int limit) {
065    super(offset, limit);
066  }
067
068  /**
069   * Full constructor.
070   *
071   * @param facets list of facets, never null
072   */
073  public SearchResponse(long offset, int pageSize, Long count, List<T> results, List<Facet<P>> facets) {
074    super(offset, pageSize, count, results);
075    setFacets(facets);
076  }
077
078  /**
079   * Gets the resulting facets of a search operation, the list can be empty if no facets were requested.
080   * If there are no values for a facet, it will not be included in the list, i.e.: a facet should have
081   * at least 1 occurrence.
082   */
083  public List<Facet<P>> getFacets() {
084    return facets;
085  }
086
087  /**
088   * Sets the list of facets.
089   *
090   * @param facets list of facets, never null
091   */
092  public final void setFacets(List<Facet<P>> facets) {
093    // copy entries
094    this.facets.clear();
095    if (facets != null) {
096      this.facets.addAll(facets);
097    }
098  }
099
100  /**
101   *
102   * @return the spellcheck response if it was requested
103   */
104  public SpellCheckResponse getSpellCheckResponse() {
105    return spellCheckResponse;
106  }
107
108  /**
109   * Sets the spellcheck response.
110   * @param spellCheckResponse if it was requested
111   */
112  public void setSpellCheckResponse(SpellCheckResponse spellCheckResponse) {
113    this.spellCheckResponse = spellCheckResponse;
114  }
115
116  @Override
117  public boolean equals(Object o) {
118    if (this == o) {
119      return true;
120    }
121    if (!(o instanceof SearchResponse)) {
122      return false;
123    }
124
125    SearchResponse<?, ?> that = (SearchResponse<?, ?>) o;
126    return Objects.equal(getCount(), that.getCount()) && Objects.equal(getResults(), that.getResults()) &&
127           Objects.equal(facets, that.getFacets()) && Objects.equal(getOffset(), that.getOffset()) &&
128           Objects.equal(getLimit(), that.getLimit()) &&
129           Objects.equal(spellCheckResponse, that.getSpellCheckResponse());
130  }
131
132  @Override
133  public int hashCode() {
134    return Objects.hashCode(super.hashCode(), facets, spellCheckResponse);
135  }
136
137  @Override
138  public String toString() {
139    return MoreObjects.toStringHelper(this).add("count", getCount()).add("results", getResults()).add("facets", facets)
140      .add("offset", getOffset()).add("limit", getLimit()).add("spellCheckResponse",spellCheckResponse).toString();
141  }
142
143}