001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.model.predicate;
015
016import org.gbif.api.model.common.search.SearchParameter;
017
018import javax.annotation.Nullable;
019
020import com.fasterxml.jackson.annotation.JsonCreator;
021import com.fasterxml.jackson.annotation.JsonProperty;
022
023import io.swagger.v3.oas.annotations.media.Schema;
024
025import java.util.Objects;
026import java.util.StringJoiner;
027
028/**
029 * This predicate checks if its {@code key} is equal to its {@code value}.
030 */
031@Schema(
032  description = "This predicate checks if its `key` is equal to its `value`."
033)
034public class EqualsPredicate<S extends SearchParameter> extends SimplePredicate<S> {
035
036  private final String checklistKey;
037
038  public EqualsPredicate(S key, String value, Boolean matchCase) {
039    this(key, value, matchCase, null);
040  }
041
042  @JsonCreator
043  public EqualsPredicate(
044    @JsonProperty("key") S key,
045    @JsonProperty("value") String value,
046    @Nullable @JsonProperty(value = "matchCase") Boolean matchCase,
047    @Nullable @JsonProperty(value = "checklistKey") String checklistKey
048    ) {
049    super(false, key, value, matchCase);
050    this.checklistKey = checklistKey;
051  }
052
053  public String getChecklistKey() {
054    return checklistKey;
055  }
056
057  @Override
058  public boolean equals(Object o) {
059    if (this == o) {
060      return true;
061    }
062    if (o == null || getClass() != o.getClass()) {
063      return false;
064    }
065    SimplePredicate<S> that = (SimplePredicate<S>) o;
066    return this.getKey() == that.getKey()
067      && Objects.equals(this.getValue(), that.getValue())
068      && this.isMatchCase() == that.isMatchCase()
069      && Objects.equals(this.checklistKey, ((EqualsPredicate<?>) o).checklistKey);
070  }
071
072  @Override
073  public int hashCode() {
074    return Objects.hash(getKey(), this.getValue(), this.isMatchCase(), checklistKey);
075  }
076
077  @Override
078  public String toString() {
079    return new StringJoiner(", ", this.getClass().getSimpleName() + "[", "]")
080      .add("key=" + getKey())
081      .add("value='" + getValue() + "'")
082      .add("matchCase='" + this.isMatchCase() + "'")
083      .add("checklistKey='" + checklistKey + "'")
084      .toString();
085  }
086}