1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.functor.core.algorithm;
18
19 import java.io.Serializable;
20 import java.util.Iterator;
21
22 import org.apache.commons.functor.BinaryProcedure;
23 import org.apache.commons.functor.UnaryPredicate;
24
25 /**
26 * Remove elements from left Iterator that match right UnaryPredicate.
27 *
28 * @version $Revision: 1166325 $ $Date: 2011-09-07 21:29:00 +0200 (Wed, 07 Sep 2011) $
29 */
30 public final class RemoveMatching<T>
31 implements BinaryProcedure<Iterator<? extends T>, UnaryPredicate<? super T>>, Serializable {
32 /**
33 * serialVersionUID declaration.
34 */
35 private static final long serialVersionUID = -8376577687898040683L;
36 private static final RemoveMatching<Object> INSTANCE = new RemoveMatching<Object>();
37
38 /**
39 * {@inheritDoc}
40 * @param left {@link Iterator}
41 * @param right {@link UnaryPredicate}
42 */
43 public void run(Iterator<? extends T> left, UnaryPredicate<? super T> right) {
44 while (left.hasNext()) {
45 if (right.test(left.next())) {
46 left.remove();
47 }
48 }
49 }
50
51 /**
52 * {@inheritDoc}
53 */
54 public boolean equals(Object obj) {
55 return obj == this || obj != null && obj.getClass().equals(getClass());
56 }
57
58 /**
59 * {@inheritDoc}
60 */
61 public int hashCode() {
62 return System.identityHashCode(INSTANCE);
63 }
64
65 /**
66 * Get a static {@link RemoveMatching} instance.
67 * @return {@link RemoveMatching}
68 */
69 public static RemoveMatching<Object> instance() {
70 return INSTANCE;
71 }
72
73 }