Revision 2649

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/LimitIterator.java
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

  
10
/**
11
 *
12
 * @author jjdelcerro
13
 * @param <T>
14
 */
15
public class LimitIterator<T> implements Iterator<T> {
16

  
17
    private final Iterator<T> it;
18
    private final long limit;
19
    private long count;
20

  
21
    public LimitIterator(Iterator<T> it, long limit) {
22
        this.it = it;
23
        this.limit = limit;
24
        this.count = 0;
25
    }
26

  
27
    @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
}

Also available in: Unified diff