Yura Yaroshevich | 68478b8 | 2018-11-28 15:41:17 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | /** |
| 14 | * Represents a predicate (boolean-valued function) of one argument. |
| 15 | */ |
| 16 | public interface Predicate<T> { |
| 17 | /** |
| 18 | * Evaluates this predicate on the given argument. |
| 19 | * |
| 20 | * @param arg the input argument |
| 21 | * @return true if the input argument matches the predicate, otherwise false |
| 22 | */ |
| 23 | boolean test(T arg); |
| 24 | |
| 25 | /** |
| 26 | * Returns a composed predicate that represents a short-circuiting logical OR of this predicate |
| 27 | * and another. When evaluating the composed predicate, if this predicate is true, then the other |
| 28 | * predicate is not evaluated. |
| 29 | * |
| 30 | * @param other a predicate that will be logically-ORed with this predicate |
| 31 | * @return a composed predicate that represents the short-circuiting logical OR of this predicate |
| 32 | * and the other predicate |
| 33 | */ |
| 34 | default Predicate<T> or(Predicate<? super T> other) { |
| 35 | return new Predicate<T>() { |
| 36 | @Override |
| 37 | public boolean test(T arg) { |
| 38 | return Predicate.this.test(arg) || other.test(arg); |
| 39 | } |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns a composed predicate that represents a short-circuiting logical AND of this predicate |
| 45 | * and another. |
| 46 | * |
| 47 | * @param other a predicate that will be logically-ANDed with this predicate |
| 48 | * @return a composed predicate that represents the short-circuiting logical AND of this predicate |
| 49 | * and the other predicate |
| 50 | */ |
| 51 | default Predicate<T> and(Predicate<? super T> other) { |
| 52 | return new Predicate<T>() { |
| 53 | @Override |
| 54 | public boolean test(T arg) { |
| 55 | return Predicate.this.test(arg) && other.test(arg); |
| 56 | } |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns a predicate that represents the logical negation of this predicate. |
| 62 | * |
| 63 | * @return a predicate that represents the logical negation of this predicate |
| 64 | */ |
| 65 | default Predicate<T> negate() { |
| 66 | return new Predicate<T>() { |
| 67 | @Override |
| 68 | public boolean test(T arg) { |
| 69 | return !Predicate.this.test(arg); |
| 70 | } |
| 71 | }; |
| 72 | } |
| 73 | } |