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 */ 017 package org.apache.commons.functor.generator; 018 019 import org.apache.commons.functor.UnaryPredicate; 020 import org.apache.commons.functor.UnaryProcedure; 021 import org.apache.commons.functor.core.composite.ConditionalUnaryProcedure; 022 023 /** 024 * Generator that filters another Generator by only passing through those elements 025 * that are matched by a specified UnaryPredicate. 026 * 027 * @version $Revision: 1156795 $ $Date: 2011-08-11 22:08:30 +0200 (Thu, 11 Aug 2011) $ 028 */ 029 public class FilteredGenerator<E> extends BaseGenerator<E> { 030 private final UnaryPredicate<? super E> pred; 031 032 /** 033 * Create a new FilteredGenerator. 034 * @param wrapped Generator to wrap 035 * @param pred filtering UnaryPredicate 036 */ 037 public FilteredGenerator(Generator<? extends E> wrapped, UnaryPredicate<? super E> pred) { 038 super(wrapped); 039 if (wrapped == null) { 040 throw new IllegalArgumentException("Generator argument was null"); 041 } 042 if (pred == null) { 043 throw new IllegalArgumentException("UnaryPredicate argument was null"); 044 } 045 this.pred = pred; 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 public boolean equals(Object obj) { 068 if (obj == this) { 069 return true; 070 } 071 if (!(obj instanceof FilteredGenerator<?>)) { 072 return false; 073 } 074 FilteredGenerator<?> other = (FilteredGenerator<?>) obj; 075 return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.pred.equals(pred); 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 public int hashCode() { 082 int result = "FilteredGenerator".hashCode(); 083 result <<= 2; 084 Generator<?> gen = getWrappedGenerator(); 085 result ^= gen == null ? 0 : gen.hashCode(); 086 result <<= 2; 087 result ^= pred == null ? 0 : pred.hashCode(); 088 return result; 089 } 090 }