Subversion Repository Public Repository

litesoft

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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.commonfoundation.interators;

import java.util.*;

/**
 * An Iterator that will NEVER throw a NoSuchElementException
 * when next() is called.  The primary utility (discovered to date) for this
 * class is for converting a variable number (ussually <= n), but ordered
 * "stream" of things (the wrapped Iterator), into another form where you
 * do not want or care (or even want to care) if there are enough of them.<p>
 *
 * @author George Smith
 * @version 1.0 7/28/01
 */

public final class InfiniteNextIterator<T> extends Iterators.AbstractWrapping<T>
{
    /**
     * Construct an Iterator that will NEVER throw a NoSuchElementException
     * when next() is called.<p>
     *
     * @param pIterator       the wrapped Iterator (!null).
     * @param pBeyondEndValue the value to return from next() when
     *                        NoSuchElementException would have been thown (null OK).
     */
    public InfiniteNextIterator( Iterator<T> pIterator, T pBeyondEndValue )
            throws NullPointerException
    {
        super( pIterator );
        zBeyondEndValue = pBeyondEndValue;
    }

    /**
     * Construct an Iterator that will NEVER throw a NoSuchElementException
     * when next() is called.<p>
     *
     * @param pIterator the wrapped Iterator (!null).
     */
    public InfiniteNextIterator( Iterator<T> pIterator )
            throws NullPointerException
    {
        this( pIterator, null );
    }

    /**
     * Returns the next object in the wrapped Iterator while there is more
     * (hasNext), otherwise returns the BeyondEndValue (which defaults to null).<p>
     * <p/>
     * Note: This implementation of next() will NEVER throw a NoSuchElementException.<p>
     *
     * @return the next object in the stream or BeyondEndValue.<p>
     *
     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#next()">java.util.Iterator#next()</a>
     */
    @Override
    public T next()
    {
        return hasNext() ? super.next() : zBeyondEndValue;
    }

    private T zBeyondEndValue;
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/commonfoundation/interators/InfiniteNextIterator.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

50 Diff Diff GeorgeS picture GeorgeS Tue 13 Apr, 2010 11:51:38 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000