001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.functor.core;
018
019import java.util.concurrent.atomic.AtomicInteger;
020
021import org.apache.commons.functor.BinaryPredicate;
022import org.apache.commons.functor.NullaryPredicate;
023import org.apache.commons.functor.Predicate;
024import org.apache.commons.lang3.Validate;
025
026/**
027 * A predicate that returns <code>false</code>
028 * the first <i>n</i> times it is invoked, and
029 * <code>true</code> thereafter.
030 *
031 * @since 1.0
032 * @version $Revision: 1539877 $ $Date: 2013-11-08 01:20:07 +0100 (Fr, 08 Nov 2013) $
033 */
034public final class Offset implements NullaryPredicate, Predicate<Object>,
035        BinaryPredicate<Object, Object> {
036    // instance variables
037    //---------------------------------------------------------------
038    /**
039     * The number of times the predicate must return {@code false}.
040     */
041    private final int min;
042    /**
043     * The current number of times the predicate has been invoked.
044     */
045    private AtomicInteger state = new AtomicInteger();
046
047    /**
048     * Create a new Offset.
049     * @param count offset
050     */
051    public Offset(int count) {
052        Validate.isTrue(count >= 0, "Argument must be a non-negative integer.");
053        this.min = count;
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    public boolean test() {
060        // stop incrementing when we've hit max, so we don't loop around
061        if (state.get() < min) {
062            state.incrementAndGet();
063            return false;
064        }
065        return true;
066    }
067
068    /**
069     * {@inheritDoc}
070     */
071    public boolean test(Object obj) {
072        return test();
073    }
074
075    /**
076     * {@inheritDoc}
077     */
078    public boolean test(Object a, Object b) {
079        return test();
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public boolean equals(Object obj) {
087        if (obj == this) {
088            return true;
089        }
090        if (!(obj instanceof Offset)) {
091            return false;
092        }
093        Offset other = (Offset) obj;
094        return other.min == min;
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public int hashCode() {
102        int result = "Offset".hashCode();
103        result <<= 2;
104        result ^= min;
105        return result;
106    }
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public String toString() {
113        return "Offset<" + min + ">";
114    }
115
116    //default == equals/hashCode due to statefulness
117}