CountingPredicate.java

  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. import java.util.function.BiPredicate;
  19. import java.util.function.Predicate;

  20. /**
  21.  * A predicate that applies the test {@code func} to each member of the {@code ary} in
  22.  * sequence for each call to {@code test()}. if the {@code ary} is exhausted,
  23.  * the subsequent calls to {@code test} are executed with a {@code null} value.
  24.  * If the calls to {@code test} do not exhaust the {@code ary} the {@code
  25.  * processRemaining} method can be called to execute the @{code test} with a
  26.  * {@code null} value for each remaining {@code idx} value.
  27.  *
  28.  * @param <T> the type of object being compared.
  29.  */
  30. class CountingPredicate<T> implements Predicate<T> {
  31.     private int idx;
  32.     private final T[] ary;
  33.     private final BiPredicate<T, T> func;

  34.     /**
  35.      * Constructs an instance that will compare the elements in {@code ary} with the
  36.      * elements returned by {@code func}. function is called as {@code func.test(
  37.      * idxValue, otherValue )}. If there are more {@code otherValue} values than
  38.      * {@code idxValues} then {@code func} is called as {@code func.test(null, otherValue)}.
  39.      *
  40.      * @param ary  The array of long values to compare.
  41.      * @param func The function to apply to the pairs of long values.
  42.      */
  43.     CountingPredicate(final T[] ary, final BiPredicate<T, T> func) {
  44.         this.ary = ary;
  45.         this.func = func;
  46.     }

  47.     /**
  48.      * Call {@code BiPredicate<T, T>} for each remaining unpaired {@code <T>} in the
  49.      * input array. This method should be invoked after the predicate has been
  50.      * passed to a {@code Extractor.forEach<T>(BiPredicate<T, T>)} to consume any
  51.      * unpaired {@code <T>}s. The second argument to the BiPredicate will be {@code null}.
  52.      *
  53.      * @return true if all calls to the predicate were successful
  54.      */
  55.     boolean processRemaining() {
  56.         // uses local references for optimization benefit.
  57.         int i = idx;
  58.         final T[] a = ary;
  59.         final int limit = a.length;
  60.         while (i != limit && func.test(a[i], null)) {
  61.             i++;
  62.         }
  63.         return i == limit;
  64.     }

  65.     @Override
  66.     public boolean test(final T other) {
  67.         return func.test(idx == ary.length ? null : ary[idx++], other);
  68.     }
  69. }