gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / FilteredIterator.java @ 2196
History | View | Annotate | Download (881 Bytes)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.tools.util; |
7 |
|
8 |
import java.util.Iterator; |
9 |
import java.util.function.Predicate; |
10 |
|
11 |
/**
|
12 |
*
|
13 |
* @author jjdelcerro
|
14 |
* @param <T>
|
15 |
*/
|
16 |
public class FilteredIterator<T> implements Iterator<T> { |
17 |
|
18 |
private final Iterator<T> it; |
19 |
private final Predicate<T> filter; |
20 |
private T next;
|
21 |
|
22 |
public FilteredIterator(Iterator<T> it, Predicate<T> filter) { |
23 |
this.it = it;
|
24 |
this.filter = filter;
|
25 |
this.next = null; |
26 |
} |
27 |
|
28 |
@Override
|
29 |
public boolean hasNext() { |
30 |
while( this.it.hasNext() ) { |
31 |
this.next = this.it.next(); |
32 |
if( this.filter.test(this.next) ) { |
33 |
return true; |
34 |
} |
35 |
} |
36 |
return false; |
37 |
} |
38 |
|
39 |
@Override
|
40 |
public T next() {
|
41 |
return this.next; |
42 |
} |
43 |
} |