Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.index.query; 16 : 17 : import java.util.Collection; 18 : 19 : /** Predicate to filter a field by matching value. */ 20 : public abstract class OperatorPredicate<T> extends Predicate<T> { 21 : protected final String name; 22 : protected final String value; 23 : 24 153 : public OperatorPredicate(String name, String value) { 25 153 : this.name = name; 26 153 : this.value = value; 27 153 : } 28 : 29 : public String getOperator() { 30 152 : return name; 31 : } 32 : 33 : public String getValue() { 34 42 : return value; 35 : } 36 : 37 : @Override 38 : public Predicate<T> copy(Collection<? extends Predicate<T>> children) { 39 8 : if (!children.isEmpty()) { 40 1 : throw new IllegalArgumentException("Expected 0 children"); 41 : } 42 8 : return this; 43 : } 44 : 45 : @Override 46 : public int hashCode() { 47 8 : return getOperator().hashCode() * 31 + getValue().hashCode(); 48 : } 49 : 50 : // Suppress the EqualsGetClass warning as this is legacy code. 51 : @SuppressWarnings("EqualsGetClass") 52 : @Override 53 : public boolean equals(Object other) { 54 7 : if (other == null) { 55 0 : return false; 56 : } 57 7 : if (getClass() == other.getClass()) { 58 7 : final OperatorPredicate<?> p = (OperatorPredicate<?>) other; 59 7 : return getOperator().equals(p.getOperator()) && getValue().equals(p.getValue()); 60 : } 61 5 : return false; 62 : } 63 : 64 : @Override 65 : public String toString() { 66 28 : final String val = getValue(); 67 28 : if (QueryParser.isSingleWord(val)) { 68 27 : return getOperator() + ":" + val; 69 : } 70 22 : return getOperator() + ":\"" + val + "\""; 71 : } 72 : }