Bug 1029: Remove dead code: samples/clustersession
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / FilterIterator.java
1 /*
2  * Copyright (c) 2012 Big Switch Networks, Inc.
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the
5  * "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  *
16  * This file incorporates work covered by the following copyright and
17  * permission notice:
18  *
19  *    Originally created by David Erickson, Stanford University
20  *
21  *    Licensed under the Apache License, Version 2.0 (the "License");
22  *    you may not use this file except in compliance with the
23  *    License. You may obtain a copy of the License at
24  *
25  *         http://www.apache.org/licenses/LICENSE-2.0
26  *
27  *    Unless required by applicable law or agreed to in writing,
28  *    software distributed under the License is distributed on an "AS
29  *    IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
30  *    express or implied. See the License for the specific language
31  *    governing permissions and limitations under the License.
32  */
33
34 package org.opendaylight.controller.sal.utils;
35
36 import java.util.Iterator;
37 import java.util.NoSuchElementException;
38
39 /**
40  * An iterator that will filter values from an iterator and return only those
41  * values that match the predicate.
42  */
43 public abstract class FilterIterator<T> implements Iterator<T> {
44     protected Iterator<T> subIterator;
45     protected T next;
46
47     /**
48      * Construct a filter iterator from the given sub iterator
49      *
50      * @param subIterator
51      *            the sub iterator over which we'll filter
52      */
53     public FilterIterator(Iterator<T> subIterator) {
54         super();
55         this.subIterator = subIterator;
56     }
57
58     /**
59      * Check whether the given value should be returned by the filter
60      *
61      * @param value
62      *            the value to check
63      * @return true if the value should be included
64      */
65     protected abstract boolean matches(T value);
66
67     // ***********
68     // Iterator<T>
69     // ***********
70
71     @Override
72     public boolean hasNext() {
73         if (next != null)
74             return true;
75
76         while (subIterator.hasNext()) {
77             next = subIterator.next();
78             if (matches(next))
79                 return true;
80         }
81         next = null;
82         return false;
83     }
84
85     @Override
86     public T next() {
87         if (hasNext()) {
88             T cur = next;
89             next = null;
90             return cur;
91         }
92         throw new NoSuchElementException();
93     }
94
95     @Override
96     public void remove() {
97         throw new UnsupportedOperationException();
98     }
99
100 }