View Javadoc
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   * forEachRemaining} 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   * @since 4.5
32   */
33  class CountingPredicate<T> implements Predicate<T> {
34      private int idx;
35      private final T[] ary;
36      private final BiPredicate<T, T> func;
37  
38      /**
39       * Constructs an instance that will compare the elements in {@code ary} with the
40       * elements returned by {@code func}. function is called as {@code func.test(
41       * idxValue, otherValue )}. If there are more {@code otherValue} values than
42       * {@code idxValues} then {@code func} is called as {@code func.test(null, otherValue)}.
43       *
44       * @param ary  The array of long values to compare.
45       * @param func The function to apply to the pairs of long values.
46       */
47      CountingPredicate(final T[] ary, final BiPredicate<T, T> func) {
48          this.ary = ary;
49          this.func = func;
50      }
51  
52      /**
53       * Call {@code BiPredicate<T, T>} for each remaining unpaired {@code <T>} in the
54       * input array. This method should be invoked after the predicate has been
55       * passed to a {@code Producer.forEach<T>(BiPredicate<T, T>)} to consume any
56       * unpaired {@code <T>}s. The second argument to the BiPredicate will be {@code null}.
57       *
58       * @return true if all calls the predicate were successful
59       */
60      boolean forEachRemaining() {
61          // uses local references for optimization benefit.
62          int i = idx;
63          final T[] a = ary;
64          final int limit = a.length;
65          while (i != limit && func.test(a[i], null)) {
66              i++;
67          }
68          return i == limit;
69      }
70  
71      @Override
72      public boolean test(final T other) {
73          return func.test(idx == ary.length ? null : ary[idx++], other);
74      }
75  }