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;
017
018import java.util.UUID;
019
020import com.fasterxml.jackson.annotation.JsonCreator;
021import com.fasterxml.jackson.annotation.JsonProperty;
022
023import io.swagger.v3.oas.annotations.media.Schema;
024import jakarta.annotation.Nullable;
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  @Nullable
042  private final UUID checklistKey;
043
044  public LikePredicate(S key, String value, Boolean matchCase) {
045    this(key, value, null, matchCase);
046  }
047
048  public UUID getChecklistKey() {
049    return checklistKey;
050  }
051
052  @JsonCreator
053  public LikePredicate(
054      @JsonProperty("key") S key,
055      @JsonProperty("value") String value,
056      @Nullable @JsonProperty(value = "checklistKey") UUID checklistKey,
057      @Nullable @JsonProperty(value = "matchCase") Boolean matchCase) {
058    super(false, key, value, matchCase);
059    this.checklistKey = checklistKey;
060    // make sure we deal with a String type
061    if (!String.class.equals(key.type())) {
062      throw new IllegalArgumentException("Like comparisons are only allowed for strings but not parameter " + key);
063    }
064  }
065}