Line data Source code
1 : // Copyright (C) 2014 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 com.google.gerrit.index.SchemaFieldDefs.SchemaField; 18 : import com.google.gerrit.index.query.RangeUtil.Range; 19 : 20 : public abstract class IntegerRangePredicate<T> extends IndexPredicate<T> { 21 : private final Range range; 22 : 23 : protected IntegerRangePredicate(SchemaField<T, Integer> type, String value) 24 : throws QueryParseException { 25 6 : super(type, value); 26 6 : range = RangeUtil.getRange(value, Integer.MIN_VALUE, Integer.MAX_VALUE); 27 6 : if (range == null) { 28 0 : throw new QueryParseException("Invalid range predicate: " + value); 29 : } 30 6 : } 31 : 32 : protected abstract Integer getValueInt(T object); 33 : 34 : @Override 35 : public boolean match(T object) { 36 4 : Integer valueInt = getValueInt(object); 37 4 : if (valueInt == null) { 38 0 : return false; 39 : } 40 4 : return valueInt >= range.min && valueInt <= range.max; 41 : } 42 : 43 : /** Return the minimum value of this predicate's range, inclusive. */ 44 : public int getMinimumValue() { 45 2 : return range.min; 46 : } 47 : 48 : /** Return the maximum value of this predicate's range, inclusive. */ 49 : public int getMaximumValue() { 50 2 : return range.max; 51 : } 52 : }