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.annotation.Experimental;
017import org.gbif.api.model.common.search.SearchParameter;
018import org.gbif.api.model.occurrence.search.OccurrenceSearchParameter;
019
020import java.util.Objects;
021import java.util.StringJoiner;
022
023import javax.annotation.Nullable;
024import javax.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 the `parameter` is not null (empty)."
033)
034public class IsNotNullPredicate<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  @Experimental
045  @Nullable
046  private final String checklistKey;
047
048  public IsNotNullPredicate(S parameter) {
049    this(parameter, null);
050  }
051
052  @JsonCreator
053  public IsNotNullPredicate(@JsonProperty("parameter") S parameter, @Nullable @JsonProperty(value = "checklistKey") String checklistKey) {
054    Objects.requireNonNull(parameter, "<parameter> may not be null");
055    this.parameter = parameter;
056    this.checklistKey = checklistKey;
057    checkPredicateAllowed();
058  }
059
060  public S getParameter() {
061    return parameter;
062  }
063
064  @Experimental
065  public String getChecklistKey() {
066    return checklistKey;
067  }
068
069  /**
070   * @throws IllegalArgumentException if the key SearchParameter is Geometry
071   */
072  private void checkPredicateAllowed() {
073    if (OccurrenceSearchParameter.GEOMETRY == parameter) {
074      throw new IllegalArgumentException("IsNotNull predicate is not supported for Geometry parameter");
075    }
076  }
077
078  @Override
079  public boolean equals(Object o) {
080    if (this == o) {
081      return true;
082    }
083    if (o == null || getClass() != o.getClass()) {
084      return false;
085    }
086    IsNotNullPredicate<S> that = (IsNotNullPredicate<S>) o;
087    return parameter == that.parameter;
088  }
089
090  @Override
091  public int hashCode() {
092    return Objects.hash(parameter);
093  }
094
095  @Override
096  public String toString() {
097    return new StringJoiner(", ", IsNotNullPredicate.class.getSimpleName() + "[", "]")
098      .add("parameter=" + parameter)
099      .toString();
100  }
101}