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.util.Iterator; 020 021import org.apache.commons.functor.BinaryProcedure; 022import org.apache.commons.functor.Predicate; 023 024/** 025 * Remove elements from left Iterator that match right Predicate. 026 * 027 * @param <T> the procedure argument type. 028 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $ 029 */ 030public final class RemoveMatching<T> 031 implements BinaryProcedure<Iterator<? extends T>, Predicate<? super T>> { 032 /** 033 * A static {@link RemoveMatching} instance reference. 034 */ 035 private static final RemoveMatching<Object> INSTANCE = new RemoveMatching<Object>(); 036 037 /** 038 * {@inheritDoc} 039 * @param left {@link Iterator} 040 * @param right {@link Predicate} 041 */ 042 public void run(Iterator<? extends T> left, Predicate<? super T> right) { 043 while (left.hasNext()) { 044 if (right.test(left.next())) { 045 left.remove(); 046 } 047 } 048 } 049 050 /** 051 * {@inheritDoc} 052 */ 053 @Override 054 public boolean equals(Object obj) { 055 return obj == this || obj != null && obj.getClass().equals(getClass()); 056 } 057 058 /** 059 * {@inheritDoc} 060 */ 061 @Override 062 public int hashCode() { 063 return System.identityHashCode(INSTANCE); 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Override 070 public String toString() { 071 return "RemoveMatching"; 072 } 073 074 /** 075 * Get a static {@link RemoveMatching} instance. 076 * @return {@link RemoveMatching} 077 */ 078 public static RemoveMatching<Object> instance() { 079 return INSTANCE; 080 } 081 082}