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.model.occurrence.search.OccurrenceSearchParameter;
018
019import java.util.Objects;
020import java.util.StringJoiner;
021import java.util.UUID;
022
023import jakarta.annotation.Nullable;
024import jakarta.validation.constraints.NotNull;
025
026import com.fasterxml.jackson.annotation.JsonCreator;
027import com.fasterxml.jackson.annotation.JsonProperty;
028
029import io.swagger.v3.oas.annotations.media.Schema;
030
031@Schema(
032  description = "This predicate checks if its `parameter` is null (empty)."
033)
034public class IsNullPredicate<S extends SearchParameter> implements Predicate {
035
036  @Schema(
037    description = "The search parameter to test."
038  )
039  @NotNull private final S parameter;
040
041  @Schema(
042    description = "Specify which taxonomy to use."
043  )
044  @Nullable
045  private final UUID checklistKey;
046
047  public IsNullPredicate(S parameter) {
048    this(parameter, null);
049  }
050
051  @JsonCreator
052  public IsNullPredicate(@JsonProperty("parameter") S parameter, @Nullable @JsonProperty(value = "checklistKey") UUID checklistKey) {
053    Objects.requireNonNull(parameter, "<parameter> may not be null");
054    this.parameter = parameter;
055    this.checklistKey = checklistKey;
056    checkPredicateAllowed();
057  }
058
059  public S getParameter() {
060    return parameter;
061  }
062
063  public UUID getChecklistKey() {
064    return checklistKey;
065  }
066
067  /**
068   * @throws IllegalArgumentException if the key SearchParameter is Geometry
069   */
070  private void checkPredicateAllowed() {
071    if (OccurrenceSearchParameter.GEOMETRY == parameter) {
072      throw new IllegalArgumentException("IsNull predicate is not supported for Geometry parameter");
073    }
074  }
075
076  @Override
077  public boolean equals(Object o) {
078    if (this == o) {
079      return true;
080    }
081    if (o == null || getClass() != o.getClass()) {
082      return false;
083    }
084    IsNullPredicate<S> that = (IsNullPredicate<S>) o;
085    return parameter == that.parameter;
086  }
087
088  @Override
089  public int hashCode() {
090    return Objects.hash(parameter);
091  }
092
093  @Override
094  public String toString() {
095    return new StringJoiner(", ", IsNullPredicate.class.getSimpleName() + "[", "]")
096      .add("parameter=" + parameter)
097      .toString();
098  }
099}