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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.commonfoundation.interators;

import org.litesoft.commonfoundation.base.*;

import java.util.*;

/**
 * An Iterator helper class.
 *
 * @author George Smith
 * @version 1.0 7/28/01
 */

public class Iterators
{
    public static <T> Iterator<T> empty()
    {
        return Cast.it( NULL_INSTANCE );
    }

    public static <T> Iterator<T> deNull( Iterator<T> pIterator )
    {
        if ( pIterator != null )
        {
            return pIterator;
        }
        return empty();
    }

    /**
     * There are two points for having this class:<p>
     * <p/>
     * First, that all Iterators need to be able to throw NoSuchElementException if
     * next is inappropriately called.  The easiest way is to simply implement a
     * dumb next at a superclass level that always throws it.<p>
     * <p/>
     * Second, the SUN/Java Iterators should NOT have supported <i>remove()</i>.
     * The GOF (Gang Of Four) Iterator not only provided more functionality
     * (supporting both multiple reads of the current item AND the ability to pass
     * over the collection multiple times), but provided a nice <i>read only</i>
     * view of an arbitrarily stored collection.  To <i>restore</i> the read only
     * view aspect of an Iterator, this class provides a <i>final</i> implementation
     * of <i>remove()</i>.<p>
     *
     * @author George Smith
     * @version 1.0 7/28/01
     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html">java.util.Iterator</a>
     */
    public static abstract class AbstractReadOnly<T> implements Iterator<T>,
                                                                Disposable
    {
        /**
         * hasNext() is part of the Iterator interface.  It is abstract here
         * because there is no reasonable, common, implementation and to
         * remind the developer who extends this class that they, at a
         * minimum, need to implement it.<p>
         *
         * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#hasNext()">java.util.Iterator#hasNext()</a>
         */
        @Override
        abstract public boolean hasNext();

        /**
         * All most all Iterators will need to throw NoSuchElementException
         * at some point, so this is simply a convenience implementation.<p>
         *
         * @throws NoSuchElementException
         * @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()
        {
            throw new NoSuchElementException();
        }

        /**
         * As mentioned above, the SUN/Java Iterators should NOT have supported
         * <i>remove()</i>.<p>
         * <p/>
         * Therefor, this method is a <i>final</i> version that throws
         * UnsupportedOperationException.<p>
         *
         * @throws UnsupportedOperationException
         * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#remove()">java.util.Iterator.remove()</a>
         */
        @Override
        public final void remove()
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public void dispose()
        {
        }
    }

    /**
     * This class's purpose is to be extended by any concrete Iterator that is
     * wrapping/decorating another Iterator.<p>
     *
     * @author George Smith
     * @version 1.0 7/28/01
     */
    public static abstract class AbstractWrapping<T> extends AbstractReadOnly<T>
    {
        /**
         * Construct an Iterator wrapping Iterator.<p>
         *
         * @param pIterator Iterator to be <i>wrapped</i> (!null).<p>
         *
         * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html">java.util.Iterator</a>
         */
        protected AbstractWrapping( Iterator<T> pIterator )
        {
            mIterator = deNull( pIterator );
        }

        /**
         * Reference to the <i>wrapped</i> Iterator.
         */
        protected Iterator<T> mIterator;

        /**
         * This hasNext() simply forwards the request to the <i>wrapped</i> Iterator.<p>
         *
         * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#hasNext()">java.util.Iterator#hasNext()</a>
         */
        @Override
        public boolean hasNext()
        {
            return mIterator.hasNext();
        }

        /**
         * This next() simply forwards the request to the <i>wrapped</i> Iterator.<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 mIterator.next();
        }

        @Override
        public void dispose()
        {
            if ( mIterator instanceof Disposable )
            {
                ((Disposable) mIterator).dispose();
            }
            mIterator = empty();
            super.dispose();
        }

        @Override
        protected void finalize()
                throws Throwable
        {
            dispose();
            super.finalize();
        }
    }

    /**
     * This class's purpose is to be extended by a concrete Filtering Iterator
     * that is wrapping/decorating another Iterator.  The Filtering works
     * by looking ahead for an entry that is acceptable.  This acceptability
     * is determined by the <i>keepThis</i> method.<p>
     *
     * @author George Smith
     * @version 1.0 7/28/01
     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html">java.util.Iterator</a>
     */

    public static abstract class AbstractFiltering<T> extends AbstractWrapping<T>
    {
        /**
         * Construct a Filtering (of another Iterator) Iterator.<p>
         *
         * @param pIterator Iterator to be <i>Filtered</i> (!null).<p>
         *
         * @see AbstractWrappingIterator
         * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html">java.util.Iterator</a>
         */
        protected AbstractFiltering( Iterator<T> pIterator )
        {
            super( pIterator );
        }

        private T lookAheadObject = null;
        private boolean lookAheadValid = false;

        /**
         * This method is the <i>heart</i> of a Filtering Iterator.
         * It determines if an Object from the underlying Iterator
         * is to be kept or tossed.<p>
         * <p/>
         * It is abstract here because there is no reasonable, common,
         * implementation and to remind the developer who extends
         * this class that they need to implement it.<p>
         *
         * @param pPossibleValue Object reference to test for keeping (not tossing).<p>
         *
         * @return <tt>true</tt> if the <i>pPossibleValue</i> should be kept (not tossed).
         */
        abstract protected boolean keepThis( T pPossibleValue );

        /**
         * Returns true if the iteration has more elements.<p>
         * <p/>
         * This method relies on a look-ahead buffer and keepThis() to
         * implement the filtering.<p>
         *
         * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#hasNext()">java.util.Iterator#hasNext()</a>
         * @see #keepThis(Object)
         */
        @Override
        public final boolean hasNext()
        {
            while ( !lookAheadValid && super.hasNext() )
            {
                lookAheadValid = keepThis( lookAheadObject = super.next() );
            }
            return lookAheadValid;
        }

        /**
         * Returns the next element in the interation.
         * <p/>
         * This method relies on hasNext (and a look-ahead buffer) to
         * implement the filtering.<p>
         *
         * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#next()">java.util.Iterator#next()</a>
         * @see #next()
         */
        @Override
        public final T next()
        {
            if ( !hasNext() )
            {
                super.next(); // throw exception
            }
            lookAheadValid = false;
            return lookAheadObject;
        }

        @Override
        public void dispose()
        {
            lookAheadObject = null;
            super.dispose();
        }
    }

    /**
     * Returns a debug/human friendly <i>label</i>ed String that represents
     * the <i>iterator</i>.<p>
     *
     * @param pIterator the Iterator to dump.
     * @param pLabel    the label for the Iterator.<p>
     *
     * @return A <i>label</i>ed String that represents <i>iterator</i>.
     */
    public static String toString( Iterator pIterator, String pLabel )
    {
        StringBuilder sb = new StringBuilder();
        if ( pLabel != null )
        {
            sb.append( pLabel ).append( ": " );
        }
        if ( pIterator == null )
        {
            sb.append( "null" );
        }
        else
        {
            appendElementsWithCount( sb, pIterator );
        }
        return sb.toString();
    }

    private static void appendElementsWithCount( StringBuilder pSb, Iterator pIterator )
    {
        StringBuilder zElements = new StringBuilder();
        int elements = 0;
        if ( pIterator.hasNext() )
        {
            zElements.append( ":\n" );
            while ( pIterator.hasNext() )
            {
                elements++;
                Object entry = pIterator.next();
                zElements.append( "    [" );
                zElements.append( (entry == null) ? "?null?" : entry.toString() );
                zElements.append( "]\n" );
            }
        }
        pSb.append( elements );
        pSb.append( " elements" );
        pSb.append( zElements );
    }

    private Iterators()
    {
    }

    private static final Iterator NULL_INSTANCE = new AbstractReadOnly()
    {
        /**
         * Returns <tt>false</tt>, because there is <b>never</b> another element.<p>
         * <p/>
         * Note: <tt>next</tt> will throw an exception.<p>
         *
         * @return <tt>false</tt>.<p>
         *
         * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Util/Iterator.html#hasNext()">java.util.Iterator#hasNext()</a>
         */
        @Override
        public boolean hasNext()
        {
            return false;
        }

        /**
         * Returns a debug/human friendly String that represents this.<p>
         *
         * @return A String representation of this.
         */
        @Override
        public String toString()
        {
            return "NullIterator";
        }
    };
}

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

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

Extracting commonfoundation

151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
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