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 import org.apache.commons.functor.core.composite.UnaryNot;
25
26 /**
27 * Retain elements in left Iterator that match right UnaryPredicate.
28 *
29 * @version $Revision: 1166328 $ $Date: 2011-09-07 21:32:26 +0200 (Wed, 07 Sep 2011) $
30 */
31 public final class RetainMatching<T>
32 implements BinaryProcedure<Iterator<? extends T>, UnaryPredicate<? super T>>, Serializable {
33 /**
34 * serialVersionUID declaration.
35 */
36 private static final long serialVersionUID = 6760018011875465469L;
37
38 private static final RetainMatching<Object> INSTANCE = new RetainMatching<Object>();
39
40 private RemoveMatching<T> removeMatching = new RemoveMatching<T>();
41
42 /**
43 * {@inheritDoc}
44 * @param left {@link Iterator}
45 * @param right {@link UnaryPredicate}
46 */
47 public void run(Iterator<? extends T> left, UnaryPredicate<? super T> right) {
48 removeMatching.run(left, UnaryNot.not(right));
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 RetainMatching} instance.
67 * @return {@link RetainMatching}
68 */
69 public static RetainMatching<Object> instance() {
70 return INSTANCE;
71 }
72 }