CountingLongPredicate.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.LongPredicate;

  19. /**
  20.  * A long predicate that applies the test func to each member of the {@code ary} in sequence for each call to {@code test()}.
  21.  * if the {@code ary} is exhausted, the subsequent calls to {@code test} are executed with a zero value.
  22.  * If the calls to {@code test} do not exhaust the {@code ary} the {@code processRemaining} method can be called to
  23.  * execute the @{code test} with a zero value for each remaining {@code idx} value.
  24.  */
  25. class CountingLongPredicate implements LongPredicate {

  26.     private int idx;
  27.     private final long[] ary;
  28.     private final LongBiPredicate func;

  29.     /**
  30.      * Constructs an instance that will compare the elements in {@code ary} with the elements returned by {@code func}.
  31.      * function is called as {@code func.test( idxValue, otherValue )}. If there are more {@code otherValue} values than
  32.      * {@code idxValues} then {@code func} is called as {@code func.test( 0, otherValue )}.
  33.      *
  34.      * @param ary The array of long values to compare.
  35.      * @param func The function to apply to the pairs of long values.
  36.      */
  37.     CountingLongPredicate(final long[] ary, final LongBiPredicate func) {
  38.         this.ary = ary;
  39.         this.func = func;
  40.     }

  41.     /**
  42.      * Call the long-long consuming bi-predicate for each remaining unpaired long in
  43.      * the input array. This method should be invoked after the predicate has been
  44.      * passed to {@link BitMapExtractor#processBitMaps(LongPredicate)} to consume any
  45.      * unpaired bitmaps. The second argument to the bi-predicate will be zero.
  46.      *
  47.      * @return true if all calls to the predicate were successful
  48.      */
  49.     boolean processRemaining() {
  50.         // uses local references for optimization benefit.
  51.         int i = idx;
  52.         final long[] a = ary;
  53.         final int limit = a.length;
  54.         while (i != limit && func.test(a[i], 0)) {
  55.             i++;
  56.         }
  57.         return i == limit;
  58.     }

  59.     @Override
  60.     public boolean test(final long other) {
  61.         return func.test(idx == ary.length ? 0 : ary[idx++], other);
  62.     }
  63. }