1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections4.bloomfilter;
18
19 import java.util.function.BiPredicate;
20 import java.util.function.Predicate;
21
22 /**
23 * A predicate that applies the test {@code func} to each member of the {@code ary} in
24 * sequence for each call to {@code test()}. if the {@code ary} is exhausted,
25 * the subsequent calls to {@code test} are executed with a {@code null} value.
26 * If the calls to {@code test} do not exhaust the {@code ary} the {@code
27 * processRemaining} method can be called to execute the @{code test} with a
28 * {@code null} value for each remaining {@code idx} value.
29 *
30 * @param <T> the type of object being compared.
31 */
32 class CountingPredicate<T> implements Predicate<T> {
33 private int idx;
34 private final T[] ary;
35 private final BiPredicate<T, T> func;
36
37 /**
38 * Constructs an instance that will compare the elements in {@code ary} with the
39 * elements returned by {@code func}. function is called as {@code func.test(
40 * idxValue, otherValue )}. If there are more {@code otherValue} values than
41 * {@code idxValues} then {@code func} is called as {@code func.test(null, otherValue)}.
42 *
43 * @param ary The array of long values to compare.
44 * @param func The function to apply to the pairs of long values.
45 */
46 CountingPredicate(final T[] ary, final BiPredicate<T, T> func) {
47 this.ary = ary;
48 this.func = func;
49 }
50
51 /**
52 * Call {@code BiPredicate<T, T>} for each remaining unpaired {@code <T>} in the
53 * input array. This method should be invoked after the predicate has been
54 * passed to a {@code Extractor.forEach<T>(BiPredicate<T, T>)} to consume any
55 * unpaired {@code <T>}s. The second argument to the BiPredicate will be {@code null}.
56 *
57 * @return true if all calls to the predicate were successful
58 */
59 boolean processRemaining() {
60 // uses local references for optimization benefit.
61 int i = idx;
62 final T[] a = ary;
63 final int limit = a.length;
64 while (i != limit && func.test(a[i], null)) {
65 i++;
66 }
67 return i == limit;
68 }
69
70 @Override
71 public boolean test(final T other) {
72 return func.test(idx == ary.length ? null : ary[idx++], other);
73 }
74 }