001/*
002 * Copyright 2014 Global Biodiversity Information Facility (GBIF)
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 * http://www.apache.org/licenses/LICENSE-2.0
007 * Unless required by applicable law or agreed to in writing, software
008 * distributed under the License is distributed on an "AS IS" BASIS,
009 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010 * See the License for the specific language governing permissions and
011 * limitations under the License.
012 */
013package org.gbif.api.model.occurrence.predicate;
014
015import org.gbif.api.model.occurrence.search.OccurrenceSearchParameter;
016
017import com.google.common.base.Objects;
018import org.codehaus.jackson.annotate.JsonCreator;
019import org.codehaus.jackson.annotate.JsonProperty;
020
021/**
022 * This predicate checks if its {@code key} is LIKE its {@code value}.
023 * The syntax for one (_) or any (%) arbitrary matching characters is the one used by Hive.
024 * See the <a href="https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#Relational_Operators">
025 *   Hive Language Reference for LIKE</a>.
026 */
027public class LikePredicate extends SimplePredicate {
028
029  @JsonCreator
030  public LikePredicate(@JsonProperty("key") OccurrenceSearchParameter key, @JsonProperty("value") String value) {
031    super(false, key, value);
032    // make sure we deal with a String type
033    if (!String.class.equals(key.type())) {
034      throw new IllegalArgumentException("Like comparisons are only allowed for strings but not parameter " + key);
035    }
036  }
037
038  @Override
039  public boolean equals(Object obj) {
040    if (this == obj) {
041      return true;
042    }
043
044    if (!(obj instanceof LikePredicate)) {
045      return false;
046    }
047
048    SimplePredicate that = (SimplePredicate) obj;
049    return Objects.equal(this.getKey(), that.getKey()) && Objects.equal(this.getValue(), that.getValue());
050  }
051
052}