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.functor.core;
18  
19  import org.apache.commons.functor.BinaryPredicate;
20  import org.apache.commons.functor.Predicate;
21  import org.apache.commons.functor.UnaryPredicate;
22  
23  /**
24   * A predicate that returns <code>false</code>
25   * the first <i>n</i> times it is invoked, and
26   * <code>true</code> thereafter.
27   *
28   * @since 1.0
29   * @version $Revision: 1160413 $ $Date: 2011-08-22 22:12:28 +0200 (Mon, 22 Aug 2011) $
30   * @author Jason Horman (jason@jhorman.org)
31   * @author Rodney Waldhoff
32   */
33  
34  public final class Offset implements Predicate, UnaryPredicate<Object>, BinaryPredicate<Object, Object> {
35      // instance variables
36      //---------------------------------------------------------------
37      /**
38       * The number of times the predicate must return {@code false}.
39       */
40      private final int min;
41      /**
42       * The current number of times the predicate has been invoked.
43       */
44      private int current;
45  
46      /**
47       * Create a new Offset.
48       * @param count offset
49       */
50      public Offset(int count) {
51          if (count < 0) {
52              throw new IllegalArgumentException("Argument must be a non-negative integer.");
53          }
54          this.min = count;
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      public synchronized boolean test() {
61          // stop incrementing when we've hit max, so we don't loop around
62          if (current < min) {
63              current++;
64              return false;
65          }
66          return true;
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      public boolean test(Object obj) {
73          return test();
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      public boolean test(Object a, Object b) {
80          return test();
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public String toString() {
87          return "Offset<" + min + ">";
88      }
89  
90      //default == equals/hashCode due to statefulness
91  }