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