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.algorithm; 018 019import java.io.Serializable; 020import java.util.Iterator; 021 022import org.apache.commons.functor.BinaryProcedure; 023import org.apache.commons.functor.UnaryPredicate; 024import org.apache.commons.functor.core.composite.UnaryNot; 025 026/** 027 * Retain elements in left Iterator that match right UnaryPredicate. 028 * 029 * @param <T> the procedure argument type 030 * @version $Revision: 1344796 $ $Date: 2012-05-31 12:12:39 -0400 (Thu, 31 May 2012) $ 031 */ 032public final class RetainMatching<T> 033 implements BinaryProcedure<Iterator<? extends T>, UnaryPredicate<? super T>>, Serializable { 034 /** 035 * serialVersionUID declaration. 036 */ 037 private static final long serialVersionUID = 6760018011875465469L; 038 /** 039 * A static {@code RetainMatching} instance reference. 040 */ 041 private static final RetainMatching<Object> INSTANCE = new RetainMatching<Object>(); 042 /** 043 * The {@code RemoveMatching} instance used to remove elements from input iterator. 044 */ 045 private final RemoveMatching<T> removeMatching = new RemoveMatching<T>(); 046 047 /** 048 * {@inheritDoc} 049 * @param left {@link Iterator} 050 * @param right {@link UnaryPredicate} 051 */ 052 public void run(Iterator<? extends T> left, UnaryPredicate<? super T> right) { 053 removeMatching.run(left, UnaryNot.not(right)); 054 } 055 056 /** 057 * {@inheritDoc} 058 */ 059 @Override 060 public boolean equals(Object obj) { 061 return obj == this || obj != null && obj.getClass().equals(getClass()); 062 } 063 064 /** 065 * {@inheritDoc} 066 */ 067 @Override 068 public int hashCode() { 069 return System.identityHashCode(INSTANCE); 070 } 071 072 /** 073 * {@inheritDoc} 074 */ 075 @Override 076 public String toString() { 077 return "RetainMatching"; 078 } 079 080 /** 081 * Get a static {@link RetainMatching} instance. 082 * @return {@link RetainMatching} 083 */ 084 public static RetainMatching<Object> instance() { 085 return INSTANCE; 086 } 087}