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