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.generator;
018
019import org.apache.commons.functor.UnaryPredicate;
020import org.apache.commons.functor.UnaryProcedure;
021import org.apache.commons.functor.core.composite.ConditionalUnaryProcedure;
022import org.apache.commons.lang3.Validate;
023
024/**
025 * Generator that filters another Generator by only passing through those elements
026 * that are matched by a specified UnaryPredicate.
027 *
028 * @param <E> the type of elements held in this generator.
029 * @version $Revision: 1365330 $ $Date: 2012-07-24 18:40:04 -0400 (Tue, 24 Jul 2012) $
030 */
031public class FilteredGenerator<E> extends BaseGenerator<E> {
032
033    /**
034     * The wrapped generator.
035     */
036    private final UnaryPredicate<? super E> pred;
037
038    /**
039     * Create a new FilteredGenerator.
040     * @param wrapped Generator to wrap
041     * @param pred filtering UnaryPredicate
042     */
043    public FilteredGenerator(Generator<? extends E> wrapped, UnaryPredicate<? super E> pred) {
044        super(Validate.notNull(wrapped, "Generator argument was null"));
045        this.pred = Validate.notNull(pred, "UnaryPredicate argument was null");
046    }
047
048    /**
049     * {@inheritDoc}
050     */
051    public void run(UnaryProcedure<? super E> proc) {
052        getWrappedGenerator().run(new ConditionalUnaryProcedure<E>(pred, proc));
053    }
054
055    /**
056     * {@inheritDoc}
057     */
058    @SuppressWarnings("unchecked")
059    @Override
060    protected Generator<? extends E> getWrappedGenerator() {
061        return (Generator<? extends E>) super.getWrappedGenerator();
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public boolean equals(Object obj) {
069        if (obj == this) {
070            return true;
071        }
072        if (!(obj instanceof FilteredGenerator<?>)) {
073            return false;
074        }
075        FilteredGenerator<?> other = (FilteredGenerator<?>) obj;
076        return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.pred.equals(pred);
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public int hashCode() {
084        int result = "FilteredGenerator".hashCode();
085        result <<= 2;
086        Generator<?> gen = getWrappedGenerator();
087        result ^= gen.hashCode();
088        result <<= 2;
089        result ^= pred.hashCode();
090        return result;
091    }
092}