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;
017import org.gbif.api.util.SearchTypeValidator;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.Objects;
023import java.util.Optional;
024import java.util.StringJoiner;
025import java.util.UUID;
026
027import com.fasterxml.jackson.annotation.JsonCreator;
028import com.fasterxml.jackson.annotation.JsonIgnore;
029import com.fasterxml.jackson.annotation.JsonProperty;
030
031import io.swagger.v3.oas.annotations.media.Schema;
032import jakarta.annotation.Nullable;
033import jakarta.validation.constraints.NotNull;
034import jakarta.validation.constraints.Size;
035
036import static org.gbif.api.util.PreconditionUtils.checkArgument;
037
038/**
039 * This predicate checks if its {@code key} contains any of its {@code values}.
040 */
041@Schema(
042  description = "This predicate checks if its `key` is equal to any of its `values`."
043)
044public class InPredicate<S extends SearchParameter> implements Predicate {
045
046  @Schema(
047    description = "The search parameter to test."
048  )
049  @NotNull private final S key;
050
051  @Schema(
052    description = "The list of value to test for."
053  )
054  @NotNull
055  @Size(min = 1)
056  private final Collection<String> values;
057
058  @Schema(
059    description = "Whether to match letter case (UPPER or lower case) on string value comparisons."
060  )
061  @Nullable
062  private final Boolean matchCase;
063
064  @Schema(
065    description = "Specify which taxonomy to use."
066  )
067  @Nullable
068  private final UUID checklistKey;
069
070  public InPredicate(
071    S key,
072    Collection<String> values,
073    Boolean matchCase
074  ) {
075    this(key, values, matchCase, null);
076  }
077
078  @JsonCreator
079  public InPredicate(
080      @JsonProperty("key") S key,
081      @JsonProperty("values") Collection<String> values,
082      @Nullable @JsonProperty(value = "matchCase") Boolean matchCase,
083      @Nullable @JsonProperty(value = "checklistKey") UUID checklistKey
084    ) {
085    this.matchCase = matchCase;
086    this.checklistKey = checklistKey;
087    Objects.requireNonNull(key, "<key> may not be null");
088    Objects.requireNonNull(values, "<values> may not be null");
089    checkArgument(!values.isEmpty(), "<values> may not be empty");
090    // make sure the value is of the right type according to the key given
091    for (String value : values) {
092      if (value == null) {
093        throw new NullPointerException();
094      }
095      SearchTypeValidator.validate(key, value);
096    }
097
098    this.key = key;
099    this.values = Collections.unmodifiableList(new ArrayList<>(values));
100  }
101
102  public S getKey() {
103    return key;
104  }
105
106  public Collection<String> getValues() {
107    return values;
108  }
109
110  /**
111   * This flag enables the use of case-sensitive matches and aggregations on certain search parameters.
112   * <p>
113   * Fields that support this feature are: occurrenceId, recordedBy, samplingProtocol, catalogNumber, collectionCode,
114   * institutionCode, eventId, parentEventId, waterBody, stateProvince, recordNumber, identifiedBy, organismId and locality.
115   */
116  @JsonIgnore
117  public Boolean isMatchCase() {
118    return Optional.ofNullable(matchCase).orElse(Boolean.FALSE);
119  }
120
121  @JsonProperty("matchCase")
122  public Boolean getMatchCase() {
123    return matchCase;
124  }
125
126  public UUID getChecklistKey() {
127    return checklistKey;
128  }
129
130  @Override
131  public boolean equals(Object o) {
132    if (this == o) {
133      return true;
134    }
135    if (o == null || getClass() != o.getClass()) {
136      return false;
137    }
138    InPredicate<S> that = (InPredicate<S>) o;
139    return key == that.key
140      && Objects.equals(values, that.values)
141      && matchCase == that.matchCase
142      && Objects.equals(checklistKey, that.checklistKey);
143  }
144
145  @Override
146  public int hashCode() {
147    return Objects.hash(key, values, matchCase, checklistKey);
148  }
149
150  @Override
151  public String toString() {
152    return new StringJoiner(", ", InPredicate.class.getSimpleName() + "[", "]")
153      .add("key=" + key)
154      .add("values=" + values)
155      .add("matchCase=" + matchCase)
156      .add("checklistKey=" + checklistKey)
157      .toString();
158  }
159}