This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 41166
Collapse All | Expand All

(-)src/org/openide/util/Enumerations.java (+488 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
package org.openide.util;
14
15
import java.util.*;
16
import java.util.Enumeration;
17
import java.util.Map;
18
import java.util.Set;
19
20
/** Factory methods for various types of java.util.Enumeration. Allows composition
21
 * of existing enumerations, filtering their content and/or modifying them. 
22
 * All of this is designed to be done in lazy way - e.g. at the latest time 
23
 * when needed.
24
 *
25
 * @since JST-PENDING
26
 * @author Jaroslav Tulach
27
 */
28
public final class Enumerations extends Object {
29
    /** No instances */
30
    private Enumerations () {
31
    }
32
33
    /** An empty enumeration. Always returns <code>false</code> when
34
     * <code>EMPTY.hasMoreElements()</code> and throws <code>NoSuchElementException</code>
35
     * from the <code>EMPTY.nextElement()</code> method.
36
     */
37
    public static final Enumeration EMPTY = Collections.enumeration (Collections.EMPTY_LIST);
38
39
    /** Creates enumeration with one element.
40
     * @param obj the element to be present in the enumeration.
41
     * @return enumeration
42
     */
43
    public static Enumeration singleton (Object obj) {
44
        return Collections.enumeration (Collections.singleton (obj));
45
    }
46
    /** Concatenates the content of two enumerations into one. Until the 
47
     * end of <code>en1</code> is reached its elements are being served. 
48
     * As soon as the <code>en1</code> has no more elements, the content
49
     * of <code>en2</code> is being returned.
50
     *
51
     * @param en1 first enumeration
52
     * @param en2 second enumeration
53
     * @return enumeration
54
     */
55
    public static Enumeration concat (Enumeration en1, Enumeration en2) {
56
        return new SeqEn (en1, en2);
57
    }
58
    /** Concatenates the content of many enumerations. The input value
59
     * is enumeration of Enumeration elements and the result is composed 
60
     * all their content. Each of the provided enumeration is fully read
61
     * and their content returned before the next enumeration is asked for 
62
     * their elements.
63
     *
64
     * @param enumOfEnums Enumeration of Enumeration elements
65
     * @return enumeration 
66
     */
67
    public static Enumeration concat (Enumeration enumOfEnums) {
68
        return new SeqEn (enumOfEnums);
69
    }
70
    /** Filters the input enumeration to new one that should contain
71
     * each of the provided elements just once. The elements are compared
72
     * using their default <code>equals</code> and <code>hashCode</code> methods.
73
     *
74
     * @param en enumeration to filter
75
     * @return enumeration without duplicated items
76
     */
77
    public static Enumeration removeDuplicates (Enumeration en) {
78
        class RDupls implements Processor {
79
            private org.openide.util.WeakSet set = new org.openide.util.WeakSet();
80
            public Object process (Object o, Collection nothing) {
81
                return set.add (o) ? o : null;
82
            }
83
        }
84
        return filter (en, new RDupls ());
85
    }
86
87
    /** Returns an enumeration that iterates over provided array.
88
     * @param arr the array of object
89
     * @return enumeration of those objects
90
     */
91
    public static Enumeration array (Object[] arr) {
92
        return Collections.enumeration (Arrays.asList (arr));
93
    }
94
    /** Removes all <code>null</code>s from the input enumeration
95
     * @param en enumeration that can contain nulls
96
     * @return new enumeration without null values
97
     */
98
    public static Enumeration removeNulls (Enumeration en) {
99
        return filter (en, new RNulls());
100
    }
101
    
102
    /** For each element of the input enumeration <code>en</code> asks the 
103
     * {@link Processor} to provide a replacement. The <code>toAdd</code> 
104
     * argument of the processor is always null.
105
     * <p>
106
     * Example to convert any objects into strings:
107
     * <pre>
108
     * Processor convertToString = new Process () {
109
     *     public Object process (Object obj, Collection toAdd) { // toAdd is always null
110
     *        return obj.toString (): // converts to string
111
     *     }
112
     * };
113
     * Enumeration strings = Enumerations.convert (elems, convertToString);
114
     * </pre> 
115
     *
116
     * @param en enumeration of any objects
117
     * @param process a callback processor for the elements (its toAdd arguments is always null)
118
     * @return new enumeration where all elements has been processed
119
     */
120
    public static Enumeration convert (Enumeration en, Processor process) {
121
        return new AltEn (en, process);
122
    }
123
    /** Allows to filter out some elements from the input enumeration. Just 
124
     * make the 
125
     * {@link Processor} return <code>null</code>. Btw. the <code>toAdd</code>
126
     * argument of the processor is always null.
127
     * <p>
128
     * Example to remove all objects that are not strings:
129
     * <pre>
130
     * Processor onlyString = new Process () {
131
     *     public Object process (Object obj, Collection toAdd) { // toAdd is always null
132
     *        if (obj instanceof String) {
133
     *            return obj;
134
     *        } else {
135
     *            return null;
136
     *        }
137
     *     }
138
     * };
139
     * Enumeration strings = Enumerations.filter (elems, onlyString);
140
     * </pre> 
141
     *
142
     * @param en enumeration of any objects
143
     * @param process a callback processor for the elements (its toAdd arguments is always null)
144
     * @return new enumeration which does not include non-processed (returned null from processor) elements
145
     */
146
    public static Enumeration filter (Enumeration en, Processor filter) {
147
        return new FilEn (en, filter);
148
    }
149
    
150
    /** Support for breadth-first enumerating. Before any element is returned
151
     * for the resulting enumeration it is processed in the {@link Processor} and 
152
     * the processor is allowed to modify it and also add additional elements
153
     * at the (current) end of the <q>queue</q> by calling <code>toAdd.add</code>
154
     * or <code>toAdd.addAll</code>. No other methods can be called on the 
155
     * provided <code>toAdd</code> collection.
156
     * <p>
157
     * Example of doing breadth-first walk thru a tree:
158
     * <pre>
159
     * Processor queueSubnodes = new Process () {
160
     *     public Object process (Object obj, Collection toAdd) { 
161
     *        Node n = (Node)obj;
162
     *        toAdd.addAll (n.getChildrenList ());
163
     *        return n;
164
     *     }
165
     * };
166
     * Enumeration strings = Enumerations.queue (elems, queueSubnodes);
167
     * </pre>
168
     * 
169
     * @param en initial content of the resulting enumeration
170
     * @param filter the processor that is called for each element and can 
171
     *        add and addAll elements to its toAdd Collection argument
172
     * @return enumeration with the initial and queued content
173
     */
174
    public static Enumeration queue (Enumeration en, Processor filter) {
175
        QEn q = new QEn (filter);
176
        while (en.hasMoreElements ()) {
177
            q.put (en.nextElement ());
178
        }
179
        return q;
180
    }
181
    
182
    /** Processor interface that can filter out objects from the enumeration,
183
     * change them or add aditional objects to the end of the current enumeration.
184
     */
185
    public static interface Processor {
186
        /** @param original the object that is going to be returned from the enumeration right now
187
         * @return a replacement for this object
188
         * @param toAdd can be non-null if one can add new objects at the end of the enumeration
189
         */
190
        public Object process (Object original, Collection toAdd);
191
    }
192
    
193
194
    /** Altering enumeration implementation */
195
    private static final class AltEn extends Object implements Enumeration {
196
        /** enumeration to filter */
197
        private Enumeration en;
198
        /** map to alter */
199
        private Processor process;
200
201
        /**
202
        * @param en enumeration to filter
203
        */
204
        public AltEn (Enumeration en, Processor process) {
205
            this.en = en;
206
            this.process = process;
207
        }
208
209
        /** @return true if there is more elements in the enumeration
210
        */
211
        public boolean hasMoreElements () {
212
            return en.hasMoreElements ();
213
        }
214
215
        /** @return next object in the enumeration
216
        * @exception NoSuchElementException can be thrown if there is no next object
217
        *   in the enumeration
218
        */
219
        public Object nextElement () {
220
            return process.process (en.nextElement (), null);
221
        }
222
    } // end of AltEn
223
224
    /** Sequence of enumerations */
225
    private static final class SeqEn extends Object implements Enumeration {
226
        /** enumeration of Enumerations */
227
        private Enumeration en;
228
        /** current enumeration */
229
        private Enumeration current;
230
231
        /** is {@link #current} up-to-date and has more elements?
232
        * The combination <CODE>current == null</CODE> and
233
        * <CODE>checked == true means there are no more elements
234
        * in this enumeration.
235
        */
236
        private boolean checked = false;
237
238
        /** Constructs new enumeration from already existing. The elements
239
        * of <CODE>en</CODE> should be also enumerations. The resulting
240
        * enumeration contains elements of such enumerations.
241
        *
242
        * @param en enumeration of Enumerations that should be sequenced
243
        */
244
        public SeqEn (Enumeration en) {
245
            this.en = en;
246
        }
247
248
        /** Composes two enumerations into one.
249
        * @param first first enumeration
250
        * @param second second enumeration
251
        */
252
        public SeqEn (Enumeration first, Enumeration second) {
253
            this (array (new Enumeration[] { first, second }));
254
        }
255
256
        /** Ensures that current enumeration is set. If there aren't more
257
        * elements in the Enumerations, sets the field <CODE>current</CODE> to null.
258
        */
259
        private void ensureCurrent () {
260
            while (current == null || !current.hasMoreElements ()) {
261
                if (en.hasMoreElements ()) {
262
                    current = (Enumeration)en.nextElement ();
263
                } else {
264
                    // no next valid enumeration
265
                    current = null;
266
                    return;
267
                }
268
            }
269
        }
270
271
        /** @return true if we have more elements */
272
        public boolean hasMoreElements () {
273
            if( !checked ) {
274
                ensureCurrent ();
275
                checked = true;
276
            }
277
            return current != null;
278
        }
279
280
        /** @return next element
281
        * @exception NoSuchElementException if there is no next element
282
        */
283
        public synchronized Object nextElement () {
284
            if( !checked ) {
285
                ensureCurrent ();
286
            }
287
            if( current != null ) {
288
                checked = false;
289
                return current.nextElement ();
290
            } else {
291
                checked = true;
292
                throw new java.util.NoSuchElementException ();
293
            }
294
        }
295
    } // end of SeqEn
296
    
297
    /** QueueEnumeration
298
     */
299
    private static class QEn extends Object implements Enumeration {
300
        /** item in linked list of Objects */
301
        private static final class ListItem {
302
            Object object;
303
            ListItem next;
304
305
            /** @param o the object for this item */
306
            ListItem (Object o) {
307
                object = o;
308
            }
309
        }
310
        /** next object to be returned */
311
        private ListItem next = null;
312
        /** last object in the queue */
313
        private ListItem last = null;
314
        /** processor to use */
315
        private Processor processor;
316
317
        public QEn (Processor p) {
318
            this.processor = p;
319
        }
320
        
321
        /** Put adds new object to the end of queue.
322
        * @param o the object to add
323
        */
324
        public synchronized void put (Object o) {
325
            if (last != null) {
326
                ListItem li = new ListItem (o);
327
                last.next = li;
328
                last = li;
329
            } else {
330
                next = last = new ListItem (o);
331
            }
332
        }
333
334
        /** Adds array of objects into the queue.
335
        * @param arr array of objects to put into the queue
336
        */
337
        public synchronized void put (Object[] arr) {
338
            for (int i = 0; i < arr.length; i++) {
339
                put (arr[i]);
340
            }
341
        }
342
343
        /** Is there any next object?
344
        * @return true if there is next object, false otherwise
345
        */
346
        public boolean hasMoreElements () {
347
            return next != null;
348
        }
349
350
        /** @return next object in enumeration
351
        * @exception NoSuchElementException if there is no next object
352
        */
353
        public synchronized Object nextElement () {
354
            if (next == null) {
355
                throw new NoSuchElementException ();
356
            }
357
            Object res = next.object;
358
359
            if ((next = next.next) == null) {
360
                last = null;
361
            };
362
            ToAdd toAdd = new ToAdd (this);
363
            res = processor.process (res, toAdd);
364
            toAdd.finish ();
365
            return res;
366
        }
367
368
        /** Temporary collection that supports only add and addAll operations*/
369
        private static final class ToAdd extends Object implements Collection {
370
            private QEn q;
371
            
372
            public ToAdd (QEn q) {
373
                this.q = q;
374
            }
375
            
376
            public void finish () {
377
                this.q = null;
378
            }
379
            
380
            public boolean add (Object o) {
381
                q.put (o);
382
                return true;
383
            }
384
            
385
            public boolean addAll (Collection c) {
386
                q.put (c.toArray ());
387
                return true;
388
            }
389
            
390
            public void clear () {
391
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
392
            }
393
            public boolean contains (Object o) {
394
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
395
            }
396
            public boolean containsAll (Collection c) {
397
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
398
            }
399
            public boolean isEmpty () {
400
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
401
            }
402
            public Iterator iterator () {
403
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
404
            }
405
            public boolean remove (Object o) {
406
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
407
            }
408
            public boolean removeAll (Collection c) {
409
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
410
            }
411
            public boolean retainAll (Collection c) {
412
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
413
            }
414
            public int size () {
415
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
416
            }
417
            public Object[] toArray () {
418
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
419
            }
420
            public Object[] toArray (Object[] a) {
421
                throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N
422
            }
423
        } // end of ToAdd
424
    } // end of QEn
425
    
426
    /** Filtering enumeration */
427
    private static final class FilEn extends Object implements Enumeration {
428
        /** marker object stating there is no nexte element prepared */
429
        private static final Object EMPTY = new Object();
430
431
        /** enumeration to filter */
432
        private Enumeration en;
433
434
        /** element to be returned next time or {@link #EMPTY} if there is
435
        * no such element prepared */
436
        private Object next = EMPTY;
437
        
438
        /** the set to use as filter */
439
        private Processor filter;
440
        
441
        /**
442
        * @param en enumeration to filter
443
        */
444
        public FilEn (Enumeration en, Processor filter) {
445
            this.en = en;
446
            this.filter = filter;
447
        }
448
449
        /** @return true if there is more elements in the enumeration
450
        */
451
        public boolean hasMoreElements () {
452
            if (next != EMPTY) {
453
                // there is a object already prepared
454
                return true;
455
            }
456
            while (en.hasMoreElements ()) {
457
                // read next
458
                next = en.nextElement ();
459
                if (filter.process (next, null) != null) {
460
                    // if the object is accepted
461
                    return true;
462
                };
463
            }
464
            next = EMPTY;
465
            return false;
466
        }
467
468
        /** @return next object in the enumeration
469
        * @exception NoSuchElementException can be thrown if there is no next object
470
        *   in the enumeration
471
        */
472
        public Object nextElement () {
473
            if( next == EMPTY && !hasMoreElements() ) {
474
                throw new NoSuchElementException ();
475
            }
476
            Object res = next;
477
            next = EMPTY;
478
            return res;
479
        }
480
    } // end of FilEn
481
    
482
    /** Returns true from contains if object is not null */
483
    private static class RNulls implements Processor  {
484
        public Object process (Object original, Collection toAdd) {
485
            return original;
486
        }
487
    } // end of RNulls
488
}
(-)src/org/openide/util/enum/AlterEnumeration.java (-1 / +5 lines)
Lines 15-21 Link Here
15
15
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
17
18
/** Abstract class that takes an enumeration and alter their elements
18
/**
19
* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be 
20
 *  replaced by {@link org.openide.util.Enumerations#convert}.
21
 *
22
* Abstract class that takes an enumeration and alter their elements
19
* to new objects.
23
* to new objects.
20
* To get this class fully work one must override <CODE>alter</CODE> method.
24
* To get this class fully work one must override <CODE>alter</CODE> method.
21
* Objects in the input and resulting enumeration must not be <CODE>null</CODE>.
25
* Objects in the input and resulting enumeration must not be <CODE>null</CODE>.
(-)src/org/openide/util/enum/ArrayEnumeration.java (-1 / +5 lines)
Lines 16-22 Link Here
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
import java.util.NoSuchElementException;
17
import java.util.NoSuchElementException;
18
18
19
/** The class that presents specifiED (in constructor) array
19
/** 
20
* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be 
21
*  replaced by {@link org.openide.util.Enumerations#array}.
22
*
23
* The class that presents specifiED (in constructor) array
20
* as an Enumeration.
24
* as an Enumeration.
21
*
25
*
22
* @author   Ian Formanek
26
* @author   Ian Formanek
(-)src/org/openide/util/enum/EmptyEnumeration.java (-1 / +5 lines)
Lines 16-22 Link Here
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
import java.util.NoSuchElementException;
17
import java.util.NoSuchElementException;
18
18
19
/** The class that represents empty enumeration.
19
/** 
20
* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be 
21
*  replaced by {@link org.openide.util.Enumerations#EMPTY}.
22
*
23
* The class that represents empty enumeration.
20
*
24
*
21
* @author  Petr Hamernik
25
* @author  Petr Hamernik
22
* @version  0.11, May 12, 1998
26
* @version  0.11, May 12, 1998
(-)src/org/openide/util/enum/FilterEnumeration.java (-1 / +5 lines)
Lines 16-22 Link Here
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
import java.util.NoSuchElementException;
17
import java.util.NoSuchElementException;
18
18
19
/** Abstract class that takes an enumeration and filters its elements.
19
/** 
20
* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be 
21
*  replaced by {@link org.openide.util.Enumerations#filter}.
22
*  
23
* Abstract class that takes an enumeration and filters its elements.
20
* To get this class fully work one must override <CODE>accept</CODE> method.
24
* To get this class fully work one must override <CODE>accept</CODE> method.
21
* Objects in the enumeration must not be <CODE>null</CODE>.
25
* Objects in the enumeration must not be <CODE>null</CODE>.
22
*
26
*
(-)src/org/openide/util/enum/QueueEnumeration.java (-1 / +5 lines)
Lines 16-22 Link Here
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
import java.util.NoSuchElementException;
17
import java.util.NoSuchElementException;
18
18
19
/** Enumeration that represents a queue. It allows by redefining
19
/** 
20
* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be 
21
*  replaced by {@link org.openide.util.Enumerations#queue}.
22
*
23
* Enumeration that represents a queue. It allows by redefining
20
* method <CODE>process</CODE> each outputed object to add other to the end of
24
* method <CODE>process</CODE> each outputed object to add other to the end of
21
* queue of waiting objects by a call to <CODE>put</CODE>.
25
* queue of waiting objects by a call to <CODE>put</CODE>.
22
*
26
*
(-)src/org/openide/util/enum/RemoveDuplicatesEnumeration.java (-1 / +4 lines)
Lines 16-22 Link Here
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
import java.util.HashSet;
17
import java.util.HashSet;
18
18
19
/** Enumeration that scans through another one and removes duplicates.
19
/** 
20
* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be 
21
 *  replaced by {@link org.openide.util.Enumerations#removeDuplicates}.
22
* Enumeration that scans through another one and removes duplicates.
20
* Two objects are duplicate if <CODE>one.equals (another)</CODE>.
23
* Two objects are duplicate if <CODE>one.equals (another)</CODE>.
21
*
24
*
22
* @author Jaroslav Tulach
25
* @author Jaroslav Tulach
(-)src/org/openide/util/enum/SequenceEnumeration.java (-1 / +5 lines)
Lines 15-21 Link Here
15
15
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
17
18
/** Composes more enumerations into one.
18
/** 
19
* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be 
20
*  replaced by {@link org.openide.util.Enumerations#concat}.
21
*
22
* Composes more enumerations into one.
19
*
23
*
20
* @author Jaroslav Tulach
24
* @author Jaroslav Tulach
21
* @author Petr Nejedly
25
* @author Petr Nejedly
(-)src/org/openide/util/enum/SingletonEnumeration.java (-1 / +5 lines)
Lines 16-22 Link Here
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
import java.util.NoSuchElementException;
17
import java.util.NoSuchElementException;
18
18
19
/** The class that encapsulates one object into one element enumeration.
19
/** 
20
* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be 
21
*  replaced by {@link org.openide.util.Enumerations#singleton}.
22
*
23
* The class that encapsulates one object into one element enumeration.
20
*
24
*
21
* @author   Jaroslav Tulach
25
* @author   Jaroslav Tulach
22
* @version  0.10, Apr 10, 1998
26
* @version  0.10, Apr 10, 1998
(-)test/unit/src/org/openide/util/EnumerationsTest.java (+376 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
package org.openide.util;
14
15
import java.util.*;
16
import java.util.Enumeration;
17
import java.util.Map;
18
import java.util.Set;
19
20
/** This is the base test for new and old enumerations. It contains 
21
 * factory methods for various kinds of enumerations and set of tests
22
 * that use them. Factory methods are overriden in OldEnumerationsTest
23
 *
24
 * @author Jaroslav Tulach
25
 */
26
public class EnumerationsTest extends org.netbeans.junit.NbTestCase {
27
    
28
    /** Creates a new instance of EnumerationsTest */
29
    public EnumerationsTest (String testName) {
30
        super(testName);
31
    }
32
33
    public static void main(java.lang.String[] args) {
34
        junit.textui.TestRunner.run(new org.netbeans.junit.NbTestSuite(EnumerationsTest.class));
35
    }
36
37
    //
38
    // Factory methods
39
    //
40
    
41
    protected Enumeration singleton (Object obj) {
42
        return Enumerations.singleton (obj);
43
    }
44
    protected Enumeration concat (Enumeration en1, Enumeration en2) {
45
        return Enumerations.concat (en1, en2);
46
    }
47
    protected Enumeration concat (Enumeration enumOfEnums) {
48
        return Enumerations.concat (enumOfEnums);
49
    }
50
    protected Enumeration removeDuplicates (Enumeration en) {
51
        return Enumerations.removeDuplicates (en);
52
    }
53
    protected Enumeration empty () {
54
        return Enumerations.EMPTY;
55
    }
56
    protected Enumeration array (Object[] arr) {
57
        return Enumerations.array (arr);
58
    }
59
    protected Enumeration convert (Enumeration en, final Map map) {
60
        class P implements Enumerations.Processor {
61
            public Object process (Object obj, Collection nothing) {
62
                return map.get (obj);
63
            }
64
        }
65
        
66
        
67
        return Enumerations.convert (en, new P ());
68
    }
69
    protected Enumeration removeNulls (Enumeration en) {
70
        return Enumerations.removeNulls (en);
71
    }
72
    protected Enumeration filter (Enumeration en, final Set filter) {
73
        class P implements Enumerations.Processor {
74
            public Object process (Object obj, Collection nothing) {
75
                return filter.contains (obj) ? obj : null;
76
            }
77
        }
78
        
79
        return Enumerations.filter (en, new P ());
80
    }
81
    /**
82
     * @param filter the set.contains (...) is called before each object is produced
83
     * @return Enumeration
84
     */
85
    protected Enumeration queue (Collection initContent, final QueueProcess process) {
86
        class C implements Enumerations.Processor {
87
            public Object process (Object object, Collection toAdd) {
88
                return process.process (object, toAdd);
89
            }
90
        }
91
        return Enumerations.queue (
92
            Collections.enumeration (initContent), 
93
            new C ()
94
        );
95
    }
96
    
97
    /** Processor interface.
98
     */
99
    public static interface QueueProcess {
100
        public Object process (Object object, Collection toAdd);
101
    }
102
103
    //
104
    // The tests
105
    // 
106
    
107
    public void testEmptyIsEmpty () {
108
        Enumeration e = empty ();
109
        assertFalse (e.hasMoreElements ());
110
        try {
111
            e.nextElement ();
112
            fail ("No elements");
113
        } catch (java.util.NoSuchElementException ex) {
114
            // ok
115
        }
116
    }
117
    
118
    public void testSingleIsSingle () {
119
        Enumeration e = singleton (this);
120
        assertTrue (e.hasMoreElements ());
121
        assertEquals ("Returns me", this, e.nextElement ());
122
        assertFalse ("Now it is empty", e.hasMoreElements ());
123
        try {
124
            e.nextElement ();
125
            fail ("No elements");
126
        } catch (java.util.NoSuchElementException ex) {
127
            // ok
128
        }
129
    }
130
131
    public void testConcatTwoAndArray () {
132
        Object[] one = { new Integer (1), new Integer (2), new Integer (3) };
133
        Object[] two = { "1", "2", "3" };
134
        
135
        ArrayList list = new ArrayList (Arrays.asList (one));
136
        list.addAll (Arrays.asList (two));
137
        
138
        assertEnums (
139
            concat (array (one), array (two)),
140
            Collections.enumeration (list)
141
        );
142
    }
143
    
144
    public void testConcatTwoAndArrayAndTakeOnlyStrings () {
145
        Object[] one = { new Integer (1), new Integer (2), new Integer (3) };
146
        Object[] two = { "1", "2", "3" };
147
        Object[] three = { new Long (1) };
148
        Object[] four = { "Kuk" };
149
        
150
        ArrayList list = new ArrayList (Arrays.asList (two));
151
        list.addAll (Arrays.asList (four));
152
        
153
        Enumeration[] alls = {
154
            array (one), array (two), array (three), array (four)
155
        };
156
        
157
        assertEnums (
158
            filter (concat (array (alls)), new OnlyStrings()),
159
            Collections.enumeration (list)
160
        );
161
    }
162
    
163
    public void testRemoveDuplicates () {
164
        Object[] one = { new Integer (1), new Integer (2), new Integer (3) };
165
        Object[] two = { "1", "2", "3" };
166
        Object[] three = { new Integer (1) };
167
        Object[] four = { "2", "3", "4" };
168
        
169
        Enumeration[] alls = {
170
            array (one), array (two), array (three), array (four)
171
        };
172
        
173
        assertEnums (
174
            removeDuplicates (concat (array (alls))),
175
            array (new Object[] { new Integer (1), new Integer (2), new Integer (3), "1", "2", "3", "4" })
176
        );
177
        
178
    }
179
    
180
    public void testQueueEnum () {
181
        class Pr implements QueueProcess {
182
            public Object process (Object o, Collection c) {
183
                Integer i = (Integer)o;
184
                int plus = i.intValue () + 1;
185
                if (plus < 10) {
186
                    c.add (new Integer (plus));
187
                }
188
                return i;
189
            }
190
        }
191
        Pr p = new Pr ();
192
        
193
        Enumeration en = queue (
194
            Collections.nCopies (1, new Integer (0)), p
195
        );
196
        
197
        for (int i = 0; i < 10; i++) {
198
            assertTrue ("has next", en.hasMoreElements ());
199
            en.nextElement ();
200
        }
201
        
202
        assertFalse ("No next element", en.hasMoreElements ());
203
    }
204
    
205
    
206
    private static void assertEnums (Enumeration e1, Enumeration e2) {
207
        int indx = 0;
208
        while (e1.hasMoreElements () && e2.hasMoreElements ()) {
209
            Object i1 = e1.nextElement ();
210
            Object i2 = e2.nextElement ();
211
            assertEquals (indx++ + "th: ", i1, i2);
212
        }
213
        
214
        if (e1.hasMoreElements ()) {
215
            fail ("first one contains another element: " + e1.nextElement ());
216
        }
217
        if (e2.hasMoreElements ()) {
218
            fail ("second one contains another element: " + e2.nextElement ());
219
        }
220
        
221
        try {
222
            e1.nextElement ();
223
            fail ("First one should throw exception, but nothing happend");
224
        } catch (java.util.NoSuchElementException ex) {
225
            // ok
226
        }
227
        
228
        try {
229
            e2.nextElement ();
230
            fail ("Second one should throw exception, but nothing happend");
231
        } catch (java.util.NoSuchElementException ex) {
232
            // ok
233
        }
234
    }
235
    
236
    public void testConvertIntegersToStringRemoveNulls () {
237
        Object[] garbage = { new Integer (1), "kuk", "hle", new Integer (5) };
238
        
239
        assertEnums (
240
            removeNulls (convert (array (garbage), new MapIntegers ())), 
241
            array (new Object[] { "1", "5" })
242
        );
243
    }
244
    
245
    /** Filters only strings.
246
     */
247
    private static final class OnlyStrings implements java.util.Set {
248
        public boolean add (Object o) {
249
            fail ("Should not be every called");
250
            return false;
251
        }
252
        
253
        public boolean addAll (Collection c) {
254
            fail ("Should not be every called");
255
            return false;
256
        }
257
        
258
        public void clear () {
259
            fail ("Should not be every called");
260
        }
261
        
262
        public boolean contains (Object o) {
263
            return o instanceof String;
264
        }
265
        
266
        public boolean containsAll (Collection c) {
267
            fail ("Should not be every called");
268
            return false;
269
        }
270
        
271
        public boolean isEmpty () {
272
            fail ("Should not be every called");
273
            return false;
274
        }
275
        
276
        public Iterator iterator () {
277
            fail ("Should not be every called");
278
            return null;
279
        }
280
        
281
        public boolean remove (Object o) {
282
            fail ("Should not be every called");
283
            return false;
284
        }
285
        
286
        public boolean removeAll (Collection c) {
287
            fail ("Should not be every called");
288
            return false;
289
        }
290
        
291
        public boolean retainAll (Collection c) {
292
            fail ("Should not be every called");
293
            return false;
294
        }
295
        
296
        public int size () {
297
            fail ("Should not be every called");
298
            return 1;
299
        }
300
        
301
        public Object[] toArray () {
302
            fail ("Should not be every called");
303
            return null;
304
        }
305
        
306
        public Object[] toArray (Object[] a) {
307
            fail ("Should not be every called");
308
            return null;
309
        }
310
    }
311
    
312
    /** Filters only strings.
313
     */
314
    private static final class MapIntegers implements java.util.Map {
315
        public boolean containsKey (Object key) {
316
            fail ("Should not be every called");
317
            return false;
318
        }
319
        
320
        public boolean containsValue (Object value) {
321
            fail ("Should not be every called");
322
            return false;
323
        }
324
        
325
        public Set entrySet () {
326
            fail ("Should not be every called");
327
            return null;
328
        }
329
        
330
        public Object get (Object key) {
331
            if (key instanceof Integer) {
332
                return key.toString ();
333
            }
334
            return null;
335
        }
336
        
337
        public Set keySet () {
338
            fail ("Should not be every called");
339
            return null;
340
        }
341
        
342
        public Object put (Object key, Object value) {
343
            fail ("Should not be every called");
344
            return null;
345
        }
346
        
347
        public void putAll (Map t) {
348
            fail ("Should not be every called");
349
        }
350
        
351
        public Collection values () {
352
            fail ("Should not be every called");
353
            return null;
354
        }
355
        
356
        public void clear () {
357
            fail ("Should not be every called");
358
        }
359
        
360
        public boolean isEmpty () {
361
            fail ("Should not be every called");
362
            return false;
363
        }
364
        
365
        public Object remove (Object key) {
366
            fail ("Should not be every called");
367
            return null;
368
        }
369
        
370
        public int size () {
371
            fail ("Should not be every called");
372
            return 1;
373
        }
374
        
375
    }
376
}
(-)test/unit/src/org/openide/util/OldEnumerationsTest.java (+144 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
package org.openide.util;
14
15
import java.util.Collection;
16
import org.openide.util.enum.*;
17
import org.openide.util.enum.QueueEnumeration;
18
19
/** Implement factory methods from EnumerationsTest, shares the same tests
20
 * with EnumerationsTest.
21
 *
22
 * @author Jaroslav Tulach
23
 */
24
public class OldEnumerationsTest extends EnumerationsTest {
25
    
26
    /** Creates a new instance of EnumerationsTest */
27
    public OldEnumerationsTest (String testName) {
28
        super(testName);
29
    }
30
31
    public static void main(java.lang.String[] args) {
32
        junit.textui.TestRunner.run(new org.netbeans.junit.NbTestSuite(OldEnumerationsTest.class));
33
    }
34
35
    protected java.util.Enumeration singleton (Object obj) {
36
        return new SingletonEnumeration (obj);
37
    }    
38
    
39
    protected java.util.Enumeration convert (java.util.Enumeration en, final java.util.Map map) {
40
        return new AlterEnumeration (en) {
41
            protected Object alter (Object o) {
42
                return map.get (o);
43
            }
44
        };
45
    }    
46
47
    protected java.util.Enumeration removeDuplicates (java.util.Enumeration en) {
48
        return new RemoveDuplicatesEnumeration (en);
49
    }
50
    
51
    protected java.util.Enumeration removeNulls (java.util.Enumeration en) {
52
        return new FilterEnumeration (en);
53
    }
54
    
55
    protected java.util.Enumeration concat (java.util.Enumeration en1, java.util.Enumeration en2) {
56
        return new SequenceEnumeration (en1, en2);
57
    }
58
    
59
    protected java.util.Enumeration array (Object[] arr) {
60
        return new ArrayEnumeration (arr);
61
    }
62
    
63
    protected java.util.Enumeration filter (java.util.Enumeration en, final java.util.Set filter) {
64
        return new FilterEnumeration (en) {
65
            protected boolean accept (Object obj) {
66
                return filter.contains (obj);
67
            }
68
        };
69
    }
70
    
71
    protected java.util.Enumeration concat (java.util.Enumeration enumOfEnums) {
72
        return new SequenceEnumeration (enumOfEnums);
73
    }
74
    
75
    protected java.util.Enumeration empty () {
76
        return new EmptyEnumeration ();
77
    }
78
    
79
    protected java.util.Enumeration queue (Collection init, final QueueProcess process) {
80
        class QEAdd extends QueueEnumeration implements Collection {
81
            protected void process (Object obj) {
82
                process.process (obj, this);
83
            }
84
            
85
            public boolean add (Object o) {
86
                put (o);
87
                return true;
88
            }
89
            
90
            public boolean addAll (Collection c) {
91
                put (c.toArray ());
92
                return true;
93
            }
94
            
95
            public void clear () {
96
                throw new IllegalStateException ("Unsupported");
97
            }
98
            
99
            public boolean contains (Object o) {
100
                throw new IllegalStateException ("Unsupported");
101
            }
102
            
103
            public boolean containsAll (Collection c) {
104
                throw new IllegalStateException ("Unsupported");
105
            }
106
            
107
            public boolean isEmpty () {
108
                throw new IllegalStateException ("Unsupported");
109
            }
110
            
111
            public java.util.Iterator iterator () {
112
                throw new IllegalStateException ("Unsupported");
113
            }
114
            
115
            public boolean remove (Object o) {
116
                throw new IllegalStateException ("Unsupported");
117
            }
118
            
119
            public boolean removeAll (Collection c) {
120
                throw new IllegalStateException ("Unsupported");
121
            }
122
            
123
            public boolean retainAll (Collection c) {
124
                throw new IllegalStateException ("Unsupported");
125
            }
126
            
127
            public int size () {
128
                throw new IllegalStateException ("Unsupported");
129
            }
130
            
131
            public Object[] toArray () {
132
                throw new IllegalStateException ("Unsupported");
133
            }
134
            
135
            public Object[] toArray (Object[] a) {
136
                throw new IllegalStateException ("Unsupported");
137
            }
138
        }
139
        QEAdd qe = new QEAdd ();
140
        qe.put (init.toArray ());
141
        return qe;
142
    }
143
    
144
}

Return to bug 41166