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;
018
019import javax.annotation.Nullable;
020
021import com.fasterxml.jackson.annotation.JsonCreator;
022import com.fasterxml.jackson.annotation.JsonProperty;
023
024import io.swagger.v3.oas.annotations.media.Schema;
025
026/**
027 * This predicate checks if its {@code key} is LIKE its {@code value}.
028 * The syntax for one <code>?</code> or any <code>*</code> arbitrary matching characters
029 * is the one used by ElasticSearch, Unix/DOS shells, etc.
030 */
031@Schema(
032  description = "This predicate checks if its `key` matches a simple pattern in the `value`.\n\n" +
033    "The character `?` matches a single character, and `*` matches zero or more characters.\n" +
034    "This is similar to the matching used by ElasticSearch, Unix/DOS shells, etc."
035)
036public class LikePredicate<S extends SearchParameter> extends SimplePredicate<S> {
037
038  @Schema(
039    description = "Specify which taxonomy to use."
040  )
041  @Experimental
042  @Nullable
043  private final String checklistKey;
044
045  public LikePredicate(S key, String value, Boolean matchCase) {
046    this(key, value, null, matchCase);
047  }
048
049  @Experimental
050  public String getChecklistKey() {
051    return checklistKey;
052  }
053
054  @JsonCreator
055  public LikePredicate(
056      @JsonProperty("key") S key,
057      @JsonProperty("value") String value,
058      @Nullable @JsonProperty(value = "checklistKey") String checklistKey,
059      @Nullable @JsonProperty(value = "matchCase") Boolean matchCase) {
060    super(false, key, value, matchCase);
061    this.checklistKey = checklistKey;
062    // make sure we deal with a String type
063    if (!String.class.equals(key.type())) {
064      throw new IllegalArgumentException("Like comparisons are only allowed for strings but not parameter " + key);
065    }
066  }
067}