| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.z; |
| 9 | |
| 10 | import java.util.function.IntPredicate; |
| 11 | |
| 12 | /** |
| 13 | * An implementation of IntPredicate that tests whether a given integer is a member |
| 14 | * of some IntSet |
| 15 | */ |
| 16 | public class IntSetPredicate implements IntPredicate { |
| 17 | |
| 18 | private IntSet set; |
| 19 | |
| 20 | public IntSetPredicate(IntSet set) { |
| 21 | if (set == null) { |
| 22 | throw new NullPointerException(); |
| 23 | } |
| 24 | this.set = set; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Ask whether a given value matches this predicate |
| 29 | * |
| 30 | * @param value the value to be tested |
| 31 | * @return true if the value matches; false if it does not |
| 32 | */ |
| 33 | public boolean test(int value) { |
| 34 | return set.contains(value); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns a composed predicate that represents a short-circuiting logical |
| 39 | * OR of this predicate and another. When evaluating the composed |
| 40 | * predicate, if this predicate is {@code true}, then the {@code other} |
| 41 | * predicate is not evaluated. |
| 42 | * <p> |
| 43 | * <p>Any exceptions thrown during evaluation of either predicate are relayed |
| 44 | * to the caller; if evaluation of this predicate throws an exception, the |
| 45 | * {@code other} predicate will not be evaluated. |
| 46 | * |
| 47 | * @param other a predicate that will be logically-ORed with this |
| 48 | * predicate |
| 49 | * @return a composed predicate that represents the short-circuiting logical |
| 50 | * OR of this predicate and the {@code other} predicate |
| 51 | * @throws NullPointerException if other is null |
| 52 | */ |
| 53 | @Override |
| 54 | public IntPredicate or(IntPredicate other) { |
| 55 | if (other instanceof IntSetPredicate) { |
| 56 | return new IntSetPredicate(set.union(((IntSetPredicate)other).set)); |
| 57 | } else { |
| 58 | return IntPredicate.super.or(other); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the underlying IntSet |
| 64 | * |
| 65 | * @return the underlying IntSet |
| 66 | */ |
| 67 | |
| 68 | public IntSet getIntSet() { |
| 69 | return set; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get string representation |
| 74 | */ |
| 75 | |
| 76 | public String toString() { |
| 77 | return "in {" + set + "}"; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Convenience predicate that always matches |
| 82 | */ |
| 83 | |
| 84 | public final static IntPredicate ALWAYS_TRUE = i -> true; |
| 85 | |
| 86 | /** |
| 87 | * Convenience predicate that never matches |
| 88 | */ |
| 89 | |
| 90 | public final static IntPredicate ALWAYS_FALSE = i -> false; |
| 91 | |
| 92 | } |