001/*
002 * Copyright 2020 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;
019import java.util.Map;
020import java.util.Objects;
021import java.util.StringJoiner;
022
023/**
024 * Contains the response information of a spell check request.
025 */
026public class SpellCheckResponse {
027
028  private boolean correctlySpelled;
029
030  private Map<String,Suggestion> suggestions;
031
032  /**
033   * Information about a suggestion made for possible correct spelled version of a search term.
034   */
035  public static class Suggestion {
036
037    private int numFound;
038    private List<String> alternatives;
039
040    /**
041     * @return the number of records that matched this suggestion
042     */
043    public int getNumFound() {
044      return numFound;
045    }
046
047    /**
048     * Sets the number of documents found for this suggestion.
049     */
050    public void setNumFound(int numFound) {
051      this.numFound = numFound;
052    }
053
054    /**
055     *
056     * @return the alternatives corrections for this token
057     */
058    public List<String> getAlternatives() {
059      return alternatives;
060    }
061
062    /**
063     * Sets the alternatives for a token.
064     */
065    public void setAlternatives(List<String> alternatives) {
066      this.alternatives = alternatives;
067    }
068
069    @Override
070    public boolean equals(Object o) {
071      if (this == o) {
072        return true;
073      }
074      if (o == null || getClass() != o.getClass()) {
075        return false;
076      }
077      Suggestion that = (Suggestion) o;
078      return numFound == that.numFound &&
079        Objects.equals(alternatives, that.alternatives);
080    }
081
082    @Override
083    public int hashCode() {
084      return Objects.hash(numFound, alternatives);
085    }
086
087    @Override
088    public String toString() {
089      return new StringJoiner(", ", Suggestion.class.getSimpleName() + "[", "]")
090        .add("numFound=" + numFound)
091        .add("alternatives=" + alternatives)
092        .toString();
093    }
094  }
095
096  /**
097   *
098   * @return false if the response contains misspellings
099   */
100  public boolean isCorrectlySpelled() {
101    return correctlySpelled;
102  }
103
104  /**
105   * Sets the misspelling flag.
106   */
107  public void setCorrectlySpelled(boolean correctlySpelled) {
108    this.correctlySpelled = correctlySpelled;
109  }
110
111  /**
112   *
113   * @return the suggestion map
114   */
115  public Map<String, Suggestion> getSuggestions() {
116    return suggestions;
117  }
118
119  /**
120   *
121   * @param token correction of a search term
122   * @return the suggestion for a token
123   */
124  public Suggestion getSuggestion(String token) {
125    return suggestions.get(token);
126  }
127
128  /**
129   * Sets the suggestion map.
130   */
131  public void setSuggestions(Map<String, Suggestion> suggestions) {
132    this.suggestions = suggestions;
133  }
134
135  @Override
136  public boolean equals(Object o) {
137    if (this == o) {
138      return true;
139    }
140    if (o == null || getClass() != o.getClass()) {
141      return false;
142    }
143    SpellCheckResponse that = (SpellCheckResponse) o;
144    return correctlySpelled == that.correctlySpelled &&
145      java.util.Objects.equals(suggestions, that.suggestions);
146  }
147
148  @Override
149  public int hashCode() {
150    return java.util.Objects.hash(correctlySpelled, suggestions);
151  }
152
153  @Override
154  public String toString() {
155    return new StringJoiner(", ", SpellCheckResponse.class.getSimpleName() + "[", "]")
156      .add("correctlySpelled=" + correctlySpelled)
157      .add("suggestions=" + suggestions)
158      .toString();
159  }
160}