summaryrefslogtreecommitdiffstats
path: root/parent/org.jvnet.hudson/src/main/java/org/jvnet/hudson/annotation_indexer/FilterIterator.java
blob: 6e0035ddeb0a8c5a4fec2a052c7318d3cd093992 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/*
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at http://www.sun.com/cddl/cddl.html
 * or http://www.netbeans.org/cddl.txt.
 *
 * When distributing Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://www.netbeans.org/cddl.txt.
 * If applicable, add the following below the CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * The Original Software is SezPoz. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 2008 Sun
 * Microsystems, Inc. All Rights Reserved.
 */package org.jvnet.hudson.annotation_indexer;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @author Kohsuke Kawaguchi
 */
abstract class FilterIterator<T> implements Iterator<T> {
    private final Iterator<? extends T> core;
    private T next;
    private boolean fetched;

    protected FilterIterator(Iterator<? extends T> core) {
        this.core = core;
    }

    protected FilterIterator(Iterable<? extends T> core) {
        this(core.iterator());
    }

    private void fetch() {
        while(!fetched && core.hasNext()) {
            T n = core.next();
            if(filter(n)) {
                next = n;
                fetched = true;
            }
        }
    }

    /**
     * Filter out items in the original collection.
     *
     * @return
     *      true to leave this item and return this item from this iterator.
     *      false to hide this item.
     */
    protected abstract boolean filter(T t);

    public boolean hasNext() {
        fetch();
        return fetched;
    }

    public T next() {
        fetch();
        if(!fetched)  throw new NoSuchElementException();
        fetched = false;
        return next;
    }

    public void remove() {
        core.remove();
    }
}