gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / LimitIterator.java @ 2649
History | View | Annotate | Download (906 Bytes)
1 | 2196 | jjdelcerro | /*
|
---|---|---|---|
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 | |||
10 | /**
|
||
11 | *
|
||
12 | * @author jjdelcerro
|
||
13 | * @param <T>
|
||
14 | */
|
||
15 | 2649 | jjdelcerro | public class LimitIterator<T> implements Iterator<T> { |
16 | 2196 | jjdelcerro | |
17 | 2649 | jjdelcerro | private final Iterator<T> it; |
18 | private final long limit; |
||
19 | private long count; |
||
20 | 2196 | jjdelcerro | |
21 | 2649 | jjdelcerro | public LimitIterator(Iterator<T> it, long limit) { |
22 | this.it = it;
|
||
23 | this.limit = limit;
|
||
24 | this.count = 0; |
||
25 | 2196 | jjdelcerro | } |
26 | |||
27 | 2649 | jjdelcerro | @Override
|
28 | public boolean hasNext() { |
||
29 | if (this.limit >= this.count) { |
||
30 | return false; |
||
31 | } |
||
32 | return this.it.hasNext(); |
||
33 | } |
||
34 | |||
35 | @Override
|
||
36 | public T next() {
|
||
37 | if (this.limit >= this.count) { |
||
38 | return null; |
||
39 | } |
||
40 | this.count++;
|
||
41 | return this.it.next(); |
||
42 | } |
||
43 | 2196 | jjdelcerro | } |