001package org.gbif.api.model.common.search;
002
003import java.util.List;
004import java.util.Map;
005
006import com.google.common.base.MoreObjects;
007import com.google.common.base.Objects;
008
009/**
010 * Contains the response information of a spell check request.
011 */
012public class SpellCheckResponse {
013
014  private boolean correctlySpelled;
015
016  private Map<String,Suggestion> suggestions;
017
018  /**
019   * Information about a suggestion made for possible correct spelled version of a search term.
020   */
021  public static class Suggestion {
022
023    private int numFound;
024    private List<String> alternatives;
025
026    /**
027     * @return the number of records that matched this suggestion
028     */
029    public int getNumFound() {
030      return numFound;
031    }
032
033    /**
034     * Sets the number of documents found for this suggestion.
035     */
036    public void setNumFound(int numFound) {
037      this.numFound = numFound;
038    }
039
040    /**
041     *
042     * @return the alternatives corrections for this token
043     */
044    public List<String> getAlternatives() {
045      return alternatives;
046    }
047
048    /**
049     * Sets the alternatives for a token.
050     */
051    public void setAlternatives(List<String> alternatives) {
052      this.alternatives = alternatives;
053    }
054
055    @Override
056    public boolean equals(Object obj) {
057      if (this == obj) {
058        return true;
059      }
060      if (!(obj instanceof Suggestion)) {
061        return false;
062      }
063      Suggestion that = (Suggestion) obj;
064      return Objects.equal(this.numFound, that.numFound)
065             && Objects.equal(this.alternatives, that.alternatives);
066    }
067
068    @Override
069    public int hashCode() {
070      return Objects.hashCode(numFound,alternatives);
071    }
072
073    @Override
074    public String toString() {
075      return MoreObjects.toStringHelper(this)
076        .add("numFound", numFound)
077        .add("alternatives", alternatives)
078        .toString();
079    }
080  }
081
082  /**
083   *
084   * @return false if the response contains misspellings
085   */
086  public boolean isCorrectlySpelled() {
087    return correctlySpelled;
088  }
089
090  /**
091   * Sets the misspelling flag.
092   */
093  public void setCorrectlySpelled(boolean correctlySpelled) {
094    this.correctlySpelled = correctlySpelled;
095  }
096
097  /**
098   *
099   * @return the suggestion map
100   */
101  public Map<String, Suggestion> getSuggestions() {
102    return suggestions;
103  }
104
105  /**
106   *
107   * @param token correction of a search term
108   * @return the suggestion for a token
109   */
110  public Suggestion getSuggestion(String token) {
111    return suggestions.get(token);
112  }
113
114  /**
115   * Sets the suggestion map.
116   */
117  public void setSuggestions(Map<String, Suggestion> suggestions) {
118    this.suggestions = suggestions;
119  }
120
121
122  @Override
123  public boolean equals(Object obj) {
124    if (this == obj) {
125      return true;
126    }
127    if (!(obj instanceof SpellCheckResponse)) {
128      return false;
129    }
130    SpellCheckResponse that = (SpellCheckResponse) obj;
131    return Objects.equal(this.suggestions, that.suggestions)
132           && Objects.equal(this.correctlySpelled, that.correctlySpelled);
133  }
134
135  @Override
136  public int hashCode() {
137    return Objects.hashCode(suggestions,correctlySpelled);
138  }
139
140  @Override
141  public String toString() {
142    return MoreObjects.toStringHelper(this)
143      .add("suggestions", suggestions)
144      .add("correctlySpelled", correctlySpelled)
145      .toString();
146  }
147}