Index: src/org/openide/util/Enumerations.java =================================================================== RCS file: src/org/openide/util/Enumerations.java diff -N src/org/openide/util/Enumerations.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/openide/util/Enumerations.java 19 May 2004 13:49:21 -0000 @@ -0,0 +1,488 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.openide.util; + +import java.util.*; +import java.util.Enumeration; +import java.util.Map; +import java.util.Set; + +/** Factory methods for various types of java.util.Enumeration. Allows composition + * of existing enumerations, filtering their content and/or modifying them. + * All of this is designed to be done in lazy way - e.g. at the latest time + * when needed. + * + * @since JST-PENDING + * @author Jaroslav Tulach + */ +public final class Enumerations extends Object { + /** No instances */ + private Enumerations () { + } + + /** An empty enumeration. Always returns false when + * EMPTY.hasMoreElements() and throws NoSuchElementException + * from the EMPTY.nextElement() method. + */ + public static final Enumeration EMPTY = Collections.enumeration (Collections.EMPTY_LIST); + + /** Creates enumeration with one element. + * @param obj the element to be present in the enumeration. + * @return enumeration + */ + public static Enumeration singleton (Object obj) { + return Collections.enumeration (Collections.singleton (obj)); + } + /** Concatenates the content of two enumerations into one. Until the + * end of en1 is reached its elements are being served. + * As soon as the en1 has no more elements, the content + * of en2 is being returned. + * + * @param en1 first enumeration + * @param en2 second enumeration + * @return enumeration + */ + public static Enumeration concat (Enumeration en1, Enumeration en2) { + return new SeqEn (en1, en2); + } + /** Concatenates the content of many enumerations. The input value + * is enumeration of Enumeration elements and the result is composed + * all their content. Each of the provided enumeration is fully read + * and their content returned before the next enumeration is asked for + * their elements. + * + * @param enumOfEnums Enumeration of Enumeration elements + * @return enumeration + */ + public static Enumeration concat (Enumeration enumOfEnums) { + return new SeqEn (enumOfEnums); + } + /** Filters the input enumeration to new one that should contain + * each of the provided elements just once. The elements are compared + * using their default equals and hashCode methods. + * + * @param en enumeration to filter + * @return enumeration without duplicated items + */ + public static Enumeration removeDuplicates (Enumeration en) { + class RDupls implements Processor { + private org.openide.util.WeakSet set = new org.openide.util.WeakSet(); + public Object process (Object o, Collection nothing) { + return set.add (o) ? o : null; + } + } + return filter (en, new RDupls ()); + } + + /** Returns an enumeration that iterates over provided array. + * @param arr the array of object + * @return enumeration of those objects + */ + public static Enumeration array (Object[] arr) { + return Collections.enumeration (Arrays.asList (arr)); + } + /** Removes all nulls from the input enumeration + * @param en enumeration that can contain nulls + * @return new enumeration without null values + */ + public static Enumeration removeNulls (Enumeration en) { + return filter (en, new RNulls()); + } + + /** For each element of the input enumeration en asks the + * {@link Processor} to provide a replacement. The toAdd + * argument of the processor is always null. + *

+ * Example to convert any objects into strings: + *

+     * Processor convertToString = new Process () {
+     *     public Object process (Object obj, Collection toAdd) { // toAdd is always null
+     *        return obj.toString (): // converts to string
+     *     }
+     * };
+     * Enumeration strings = Enumerations.convert (elems, convertToString);
+     * 
+ * + * @param en enumeration of any objects + * @param process a callback processor for the elements (its toAdd arguments is always null) + * @return new enumeration where all elements has been processed + */ + public static Enumeration convert (Enumeration en, Processor process) { + return new AltEn (en, process); + } + /** Allows to filter out some elements from the input enumeration. Just + * make the + * {@link Processor} return null. Btw. the toAdd + * argument of the processor is always null. + *

+ * Example to remove all objects that are not strings: + *

+     * Processor onlyString = new Process () {
+     *     public Object process (Object obj, Collection toAdd) { // toAdd is always null
+     *        if (obj instanceof String) {
+     *            return obj;
+     *        } else {
+     *            return null;
+     *        }
+     *     }
+     * };
+     * Enumeration strings = Enumerations.filter (elems, onlyString);
+     * 
+ * + * @param en enumeration of any objects + * @param process a callback processor for the elements (its toAdd arguments is always null) + * @return new enumeration which does not include non-processed (returned null from processor) elements + */ + public static Enumeration filter (Enumeration en, Processor filter) { + return new FilEn (en, filter); + } + + /** Support for breadth-first enumerating. Before any element is returned + * for the resulting enumeration it is processed in the {@link Processor} and + * the processor is allowed to modify it and also add additional elements + * at the (current) end of the queue by calling toAdd.add + * or toAdd.addAll. No other methods can be called on the + * provided toAdd collection. + *

+ * Example of doing breadth-first walk thru a tree: + *

+     * Processor queueSubnodes = new Process () {
+     *     public Object process (Object obj, Collection toAdd) { 
+     *        Node n = (Node)obj;
+     *        toAdd.addAll (n.getChildrenList ());
+     *        return n;
+     *     }
+     * };
+     * Enumeration strings = Enumerations.queue (elems, queueSubnodes);
+     * 
+ * + * @param en initial content of the resulting enumeration + * @param filter the processor that is called for each element and can + * add and addAll elements to its toAdd Collection argument + * @return enumeration with the initial and queued content + */ + public static Enumeration queue (Enumeration en, Processor filter) { + QEn q = new QEn (filter); + while (en.hasMoreElements ()) { + q.put (en.nextElement ()); + } + return q; + } + + /** Processor interface that can filter out objects from the enumeration, + * change them or add aditional objects to the end of the current enumeration. + */ + public static interface Processor { + /** @param original the object that is going to be returned from the enumeration right now + * @return a replacement for this object + * @param toAdd can be non-null if one can add new objects at the end of the enumeration + */ + public Object process (Object original, Collection toAdd); + } + + + /** Altering enumeration implementation */ + private static final class AltEn extends Object implements Enumeration { + /** enumeration to filter */ + private Enumeration en; + /** map to alter */ + private Processor process; + + /** + * @param en enumeration to filter + */ + public AltEn (Enumeration en, Processor process) { + this.en = en; + this.process = process; + } + + /** @return true if there is more elements in the enumeration + */ + public boolean hasMoreElements () { + return en.hasMoreElements (); + } + + /** @return next object in the enumeration + * @exception NoSuchElementException can be thrown if there is no next object + * in the enumeration + */ + public Object nextElement () { + return process.process (en.nextElement (), null); + } + } // end of AltEn + + /** Sequence of enumerations */ + private static final class SeqEn extends Object implements Enumeration { + /** enumeration of Enumerations */ + private Enumeration en; + /** current enumeration */ + private Enumeration current; + + /** is {@link #current} up-to-date and has more elements? + * The combination current == null and + * checked == true means there are no more elements + * in this enumeration. + */ + private boolean checked = false; + + /** Constructs new enumeration from already existing. The elements + * of en should be also enumerations. The resulting + * enumeration contains elements of such enumerations. + * + * @param en enumeration of Enumerations that should be sequenced + */ + public SeqEn (Enumeration en) { + this.en = en; + } + + /** Composes two enumerations into one. + * @param first first enumeration + * @param second second enumeration + */ + public SeqEn (Enumeration first, Enumeration second) { + this (array (new Enumeration[] { first, second })); + } + + /** Ensures that current enumeration is set. If there aren't more + * elements in the Enumerations, sets the field current to null. + */ + private void ensureCurrent () { + while (current == null || !current.hasMoreElements ()) { + if (en.hasMoreElements ()) { + current = (Enumeration)en.nextElement (); + } else { + // no next valid enumeration + current = null; + return; + } + } + } + + /** @return true if we have more elements */ + public boolean hasMoreElements () { + if( !checked ) { + ensureCurrent (); + checked = true; + } + return current != null; + } + + /** @return next element + * @exception NoSuchElementException if there is no next element + */ + public synchronized Object nextElement () { + if( !checked ) { + ensureCurrent (); + } + if( current != null ) { + checked = false; + return current.nextElement (); + } else { + checked = true; + throw new java.util.NoSuchElementException (); + } + } + } // end of SeqEn + + /** QueueEnumeration + */ + private static class QEn extends Object implements Enumeration { + /** item in linked list of Objects */ + private static final class ListItem { + Object object; + ListItem next; + + /** @param o the object for this item */ + ListItem (Object o) { + object = o; + } + } + /** next object to be returned */ + private ListItem next = null; + /** last object in the queue */ + private ListItem last = null; + /** processor to use */ + private Processor processor; + + public QEn (Processor p) { + this.processor = p; + } + + /** Put adds new object to the end of queue. + * @param o the object to add + */ + public synchronized void put (Object o) { + if (last != null) { + ListItem li = new ListItem (o); + last.next = li; + last = li; + } else { + next = last = new ListItem (o); + } + } + + /** Adds array of objects into the queue. + * @param arr array of objects to put into the queue + */ + public synchronized void put (Object[] arr) { + for (int i = 0; i < arr.length; i++) { + put (arr[i]); + } + } + + /** Is there any next object? + * @return true if there is next object, false otherwise + */ + public boolean hasMoreElements () { + return next != null; + } + + /** @return next object in enumeration + * @exception NoSuchElementException if there is no next object + */ + public synchronized Object nextElement () { + if (next == null) { + throw new NoSuchElementException (); + } + Object res = next.object; + + if ((next = next.next) == null) { + last = null; + }; + ToAdd toAdd = new ToAdd (this); + res = processor.process (res, toAdd); + toAdd.finish (); + return res; + } + + /** Temporary collection that supports only add and addAll operations*/ + private static final class ToAdd extends Object implements Collection { + private QEn q; + + public ToAdd (QEn q) { + this.q = q; + } + + public void finish () { + this.q = null; + } + + public boolean add (Object o) { + q.put (o); + return true; + } + + public boolean addAll (Collection c) { + q.put (c.toArray ()); + return true; + } + + public void clear () { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public boolean contains (Object o) { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public boolean containsAll (Collection c) { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public boolean isEmpty () { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public Iterator iterator () { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public boolean remove (Object o) { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public boolean removeAll (Collection c) { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public boolean retainAll (Collection c) { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public int size () { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public Object[] toArray () { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + public Object[] toArray (Object[] a) { + throw new IllegalStateException ("Onldy add and addAll are implemented"); // NOI18N + } + } // end of ToAdd + } // end of QEn + + /** Filtering enumeration */ + private static final class FilEn extends Object implements Enumeration { + /** marker object stating there is no nexte element prepared */ + private static final Object EMPTY = new Object(); + + /** enumeration to filter */ + private Enumeration en; + + /** element to be returned next time or {@link #EMPTY} if there is + * no such element prepared */ + private Object next = EMPTY; + + /** the set to use as filter */ + private Processor filter; + + /** + * @param en enumeration to filter + */ + public FilEn (Enumeration en, Processor filter) { + this.en = en; + this.filter = filter; + } + + /** @return true if there is more elements in the enumeration + */ + public boolean hasMoreElements () { + if (next != EMPTY) { + // there is a object already prepared + return true; + } + while (en.hasMoreElements ()) { + // read next + next = en.nextElement (); + if (filter.process (next, null) != null) { + // if the object is accepted + return true; + }; + } + next = EMPTY; + return false; + } + + /** @return next object in the enumeration + * @exception NoSuchElementException can be thrown if there is no next object + * in the enumeration + */ + public Object nextElement () { + if( next == EMPTY && !hasMoreElements() ) { + throw new NoSuchElementException (); + } + Object res = next; + next = EMPTY; + return res; + } + } // end of FilEn + + /** Returns true from contains if object is not null */ + private static class RNulls implements Processor { + public Object process (Object original, Collection toAdd) { + return original; + } + } // end of RNulls +} Index: src/org/openide/util/enum/AlterEnumeration.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/enum/AlterEnumeration.java,v retrieving revision 1.8 diff -u -r1.8 AlterEnumeration.java --- src/org/openide/util/enum/AlterEnumeration.java 3 Dec 2002 14:12:13 -0000 1.8 +++ src/org/openide/util/enum/AlterEnumeration.java 19 May 2004 13:49:21 -0000 @@ -15,7 +15,11 @@ import java.util.Enumeration; -/** Abstract class that takes an enumeration and alter their elements +/** +* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be + * replaced by {@link org.openide.util.Enumerations#convert}. + * +* Abstract class that takes an enumeration and alter their elements * to new objects. * To get this class fully work one must override alter method. * Objects in the input and resulting enumeration must not be null. Index: src/org/openide/util/enum/ArrayEnumeration.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/enum/ArrayEnumeration.java,v retrieving revision 1.7 diff -u -r1.7 ArrayEnumeration.java --- src/org/openide/util/enum/ArrayEnumeration.java 22 Nov 2001 08:57:09 -0000 1.7 +++ src/org/openide/util/enum/ArrayEnumeration.java 19 May 2004 13:49:21 -0000 @@ -16,7 +16,11 @@ import java.util.Enumeration; import java.util.NoSuchElementException; -/** The class that presents specifiED (in constructor) array +/** +* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be +* replaced by {@link org.openide.util.Enumerations#array}. +* +* The class that presents specifiED (in constructor) array * as an Enumeration. * * @author Ian Formanek Index: src/org/openide/util/enum/EmptyEnumeration.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/enum/EmptyEnumeration.java,v retrieving revision 1.7 diff -u -r1.7 EmptyEnumeration.java --- src/org/openide/util/enum/EmptyEnumeration.java 22 Nov 2001 08:57:09 -0000 1.7 +++ src/org/openide/util/enum/EmptyEnumeration.java 19 May 2004 13:49:21 -0000 @@ -16,7 +16,11 @@ import java.util.Enumeration; import java.util.NoSuchElementException; -/** The class that represents empty enumeration. +/** +* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be +* replaced by {@link org.openide.util.Enumerations#EMPTY}. +* +* The class that represents empty enumeration. * * @author Petr Hamernik * @version 0.11, May 12, 1998 Index: src/org/openide/util/enum/FilterEnumeration.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/enum/FilterEnumeration.java,v retrieving revision 1.9 diff -u -r1.9 FilterEnumeration.java --- src/org/openide/util/enum/FilterEnumeration.java 31 May 2001 15:08:52 -0000 1.9 +++ src/org/openide/util/enum/FilterEnumeration.java 19 May 2004 13:49:21 -0000 @@ -16,7 +16,11 @@ import java.util.Enumeration; import java.util.NoSuchElementException; -/** Abstract class that takes an enumeration and filters its elements. +/** +* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be +* replaced by {@link org.openide.util.Enumerations#filter}. +* +* Abstract class that takes an enumeration and filters its elements. * To get this class fully work one must override accept method. * Objects in the enumeration must not be null. * Index: src/org/openide/util/enum/QueueEnumeration.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/enum/QueueEnumeration.java,v retrieving revision 1.8 diff -u -r1.8 QueueEnumeration.java --- src/org/openide/util/enum/QueueEnumeration.java 6 Aug 2001 08:23:11 -0000 1.8 +++ src/org/openide/util/enum/QueueEnumeration.java 19 May 2004 13:49:21 -0000 @@ -16,7 +16,11 @@ import java.util.Enumeration; import java.util.NoSuchElementException; -/** Enumeration that represents a queue. It allows by redefining +/** +* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be +* replaced by {@link org.openide.util.Enumerations#queue}. +* +* Enumeration that represents a queue. It allows by redefining * method process each outputed object to add other to the end of * queue of waiting objects by a call to put. * Index: src/org/openide/util/enum/RemoveDuplicatesEnumeration.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/enum/RemoveDuplicatesEnumeration.java,v retrieving revision 1.9 diff -u -r1.9 RemoveDuplicatesEnumeration.java --- src/org/openide/util/enum/RemoveDuplicatesEnumeration.java 31 May 2001 15:08:53 -0000 1.9 +++ src/org/openide/util/enum/RemoveDuplicatesEnumeration.java 19 May 2004 13:49:21 -0000 @@ -16,7 +16,10 @@ import java.util.Enumeration; import java.util.HashSet; -/** Enumeration that scans through another one and removes duplicates. +/** +* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be + * replaced by {@link org.openide.util.Enumerations#removeDuplicates}. +* Enumeration that scans through another one and removes duplicates. * Two objects are duplicate if one.equals (another). * * @author Jaroslav Tulach Index: src/org/openide/util/enum/SequenceEnumeration.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/enum/SequenceEnumeration.java,v retrieving revision 1.7 diff -u -r1.7 SequenceEnumeration.java --- src/org/openide/util/enum/SequenceEnumeration.java 25 May 2001 13:19:02 -0000 1.7 +++ src/org/openide/util/enum/SequenceEnumeration.java 19 May 2004 13:49:21 -0000 @@ -15,7 +15,11 @@ import java.util.Enumeration; -/** Composes more enumerations into one. +/** +* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be +* replaced by {@link org.openide.util.Enumerations#concat}. +* +* Composes more enumerations into one. * * @author Jaroslav Tulach * @author Petr Nejedly Index: src/org/openide/util/enum/SingletonEnumeration.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/enum/SingletonEnumeration.java,v retrieving revision 1.7 diff -u -r1.7 SingletonEnumeration.java --- src/org/openide/util/enum/SingletonEnumeration.java 22 Nov 2001 08:57:09 -0000 1.7 +++ src/org/openide/util/enum/SingletonEnumeration.java 19 May 2004 13:49:21 -0000 @@ -16,7 +16,11 @@ import java.util.Enumeration; import java.util.NoSuchElementException; -/** The class that encapsulates one object into one element enumeration. +/** +* @deprecated JDK1.5 treats enum as keyword and that is why this class had to be +* replaced by {@link org.openide.util.Enumerations#singleton}. +* +* The class that encapsulates one object into one element enumeration. * * @author Jaroslav Tulach * @version 0.10, Apr 10, 1998 Index: test/unit/src/org/openide/util/EnumerationsTest.java =================================================================== RCS file: test/unit/src/org/openide/util/EnumerationsTest.java diff -N test/unit/src/org/openide/util/EnumerationsTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/unit/src/org/openide/util/EnumerationsTest.java 19 May 2004 13:49:23 -0000 @@ -0,0 +1,376 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.openide.util; + +import java.util.*; +import java.util.Enumeration; +import java.util.Map; +import java.util.Set; + +/** This is the base test for new and old enumerations. It contains + * factory methods for various kinds of enumerations and set of tests + * that use them. Factory methods are overriden in OldEnumerationsTest + * + * @author Jaroslav Tulach + */ +public class EnumerationsTest extends org.netbeans.junit.NbTestCase { + + /** Creates a new instance of EnumerationsTest */ + public EnumerationsTest (String testName) { + super(testName); + } + + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(new org.netbeans.junit.NbTestSuite(EnumerationsTest.class)); + } + + // + // Factory methods + // + + protected Enumeration singleton (Object obj) { + return Enumerations.singleton (obj); + } + protected Enumeration concat (Enumeration en1, Enumeration en2) { + return Enumerations.concat (en1, en2); + } + protected Enumeration concat (Enumeration enumOfEnums) { + return Enumerations.concat (enumOfEnums); + } + protected Enumeration removeDuplicates (Enumeration en) { + return Enumerations.removeDuplicates (en); + } + protected Enumeration empty () { + return Enumerations.EMPTY; + } + protected Enumeration array (Object[] arr) { + return Enumerations.array (arr); + } + protected Enumeration convert (Enumeration en, final Map map) { + class P implements Enumerations.Processor { + public Object process (Object obj, Collection nothing) { + return map.get (obj); + } + } + + + return Enumerations.convert (en, new P ()); + } + protected Enumeration removeNulls (Enumeration en) { + return Enumerations.removeNulls (en); + } + protected Enumeration filter (Enumeration en, final Set filter) { + class P implements Enumerations.Processor { + public Object process (Object obj, Collection nothing) { + return filter.contains (obj) ? obj : null; + } + } + + return Enumerations.filter (en, new P ()); + } + /** + * @param filter the set.contains (...) is called before each object is produced + * @return Enumeration + */ + protected Enumeration queue (Collection initContent, final QueueProcess process) { + class C implements Enumerations.Processor { + public Object process (Object object, Collection toAdd) { + return process.process (object, toAdd); + } + } + return Enumerations.queue ( + Collections.enumeration (initContent), + new C () + ); + } + + /** Processor interface. + */ + public static interface QueueProcess { + public Object process (Object object, Collection toAdd); + } + + // + // The tests + // + + public void testEmptyIsEmpty () { + Enumeration e = empty (); + assertFalse (e.hasMoreElements ()); + try { + e.nextElement (); + fail ("No elements"); + } catch (java.util.NoSuchElementException ex) { + // ok + } + } + + public void testSingleIsSingle () { + Enumeration e = singleton (this); + assertTrue (e.hasMoreElements ()); + assertEquals ("Returns me", this, e.nextElement ()); + assertFalse ("Now it is empty", e.hasMoreElements ()); + try { + e.nextElement (); + fail ("No elements"); + } catch (java.util.NoSuchElementException ex) { + // ok + } + } + + public void testConcatTwoAndArray () { + Object[] one = { new Integer (1), new Integer (2), new Integer (3) }; + Object[] two = { "1", "2", "3" }; + + ArrayList list = new ArrayList (Arrays.asList (one)); + list.addAll (Arrays.asList (two)); + + assertEnums ( + concat (array (one), array (two)), + Collections.enumeration (list) + ); + } + + public void testConcatTwoAndArrayAndTakeOnlyStrings () { + Object[] one = { new Integer (1), new Integer (2), new Integer (3) }; + Object[] two = { "1", "2", "3" }; + Object[] three = { new Long (1) }; + Object[] four = { "Kuk" }; + + ArrayList list = new ArrayList (Arrays.asList (two)); + list.addAll (Arrays.asList (four)); + + Enumeration[] alls = { + array (one), array (two), array (three), array (four) + }; + + assertEnums ( + filter (concat (array (alls)), new OnlyStrings()), + Collections.enumeration (list) + ); + } + + public void testRemoveDuplicates () { + Object[] one = { new Integer (1), new Integer (2), new Integer (3) }; + Object[] two = { "1", "2", "3" }; + Object[] three = { new Integer (1) }; + Object[] four = { "2", "3", "4" }; + + Enumeration[] alls = { + array (one), array (two), array (three), array (four) + }; + + assertEnums ( + removeDuplicates (concat (array (alls))), + array (new Object[] { new Integer (1), new Integer (2), new Integer (3), "1", "2", "3", "4" }) + ); + + } + + public void testQueueEnum () { + class Pr implements QueueProcess { + public Object process (Object o, Collection c) { + Integer i = (Integer)o; + int plus = i.intValue () + 1; + if (plus < 10) { + c.add (new Integer (plus)); + } + return i; + } + } + Pr p = new Pr (); + + Enumeration en = queue ( + Collections.nCopies (1, new Integer (0)), p + ); + + for (int i = 0; i < 10; i++) { + assertTrue ("has next", en.hasMoreElements ()); + en.nextElement (); + } + + assertFalse ("No next element", en.hasMoreElements ()); + } + + + private static void assertEnums (Enumeration e1, Enumeration e2) { + int indx = 0; + while (e1.hasMoreElements () && e2.hasMoreElements ()) { + Object i1 = e1.nextElement (); + Object i2 = e2.nextElement (); + assertEquals (indx++ + "th: ", i1, i2); + } + + if (e1.hasMoreElements ()) { + fail ("first one contains another element: " + e1.nextElement ()); + } + if (e2.hasMoreElements ()) { + fail ("second one contains another element: " + e2.nextElement ()); + } + + try { + e1.nextElement (); + fail ("First one should throw exception, but nothing happend"); + } catch (java.util.NoSuchElementException ex) { + // ok + } + + try { + e2.nextElement (); + fail ("Second one should throw exception, but nothing happend"); + } catch (java.util.NoSuchElementException ex) { + // ok + } + } + + public void testConvertIntegersToStringRemoveNulls () { + Object[] garbage = { new Integer (1), "kuk", "hle", new Integer (5) }; + + assertEnums ( + removeNulls (convert (array (garbage), new MapIntegers ())), + array (new Object[] { "1", "5" }) + ); + } + + /** Filters only strings. + */ + private static final class OnlyStrings implements java.util.Set { + public boolean add (Object o) { + fail ("Should not be every called"); + return false; + } + + public boolean addAll (Collection c) { + fail ("Should not be every called"); + return false; + } + + public void clear () { + fail ("Should not be every called"); + } + + public boolean contains (Object o) { + return o instanceof String; + } + + public boolean containsAll (Collection c) { + fail ("Should not be every called"); + return false; + } + + public boolean isEmpty () { + fail ("Should not be every called"); + return false; + } + + public Iterator iterator () { + fail ("Should not be every called"); + return null; + } + + public boolean remove (Object o) { + fail ("Should not be every called"); + return false; + } + + public boolean removeAll (Collection c) { + fail ("Should not be every called"); + return false; + } + + public boolean retainAll (Collection c) { + fail ("Should not be every called"); + return false; + } + + public int size () { + fail ("Should not be every called"); + return 1; + } + + public Object[] toArray () { + fail ("Should not be every called"); + return null; + } + + public Object[] toArray (Object[] a) { + fail ("Should not be every called"); + return null; + } + } + + /** Filters only strings. + */ + private static final class MapIntegers implements java.util.Map { + public boolean containsKey (Object key) { + fail ("Should not be every called"); + return false; + } + + public boolean containsValue (Object value) { + fail ("Should not be every called"); + return false; + } + + public Set entrySet () { + fail ("Should not be every called"); + return null; + } + + public Object get (Object key) { + if (key instanceof Integer) { + return key.toString (); + } + return null; + } + + public Set keySet () { + fail ("Should not be every called"); + return null; + } + + public Object put (Object key, Object value) { + fail ("Should not be every called"); + return null; + } + + public void putAll (Map t) { + fail ("Should not be every called"); + } + + public Collection values () { + fail ("Should not be every called"); + return null; + } + + public void clear () { + fail ("Should not be every called"); + } + + public boolean isEmpty () { + fail ("Should not be every called"); + return false; + } + + public Object remove (Object key) { + fail ("Should not be every called"); + return null; + } + + public int size () { + fail ("Should not be every called"); + return 1; + } + + } +} Index: test/unit/src/org/openide/util/OldEnumerationsTest.java =================================================================== RCS file: test/unit/src/org/openide/util/OldEnumerationsTest.java diff -N test/unit/src/org/openide/util/OldEnumerationsTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/unit/src/org/openide/util/OldEnumerationsTest.java 19 May 2004 13:49:23 -0000 @@ -0,0 +1,144 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.openide.util; + +import java.util.Collection; +import org.openide.util.enum.*; +import org.openide.util.enum.QueueEnumeration; + +/** Implement factory methods from EnumerationsTest, shares the same tests + * with EnumerationsTest. + * + * @author Jaroslav Tulach + */ +public class OldEnumerationsTest extends EnumerationsTest { + + /** Creates a new instance of EnumerationsTest */ + public OldEnumerationsTest (String testName) { + super(testName); + } + + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(new org.netbeans.junit.NbTestSuite(OldEnumerationsTest.class)); + } + + protected java.util.Enumeration singleton (Object obj) { + return new SingletonEnumeration (obj); + } + + protected java.util.Enumeration convert (java.util.Enumeration en, final java.util.Map map) { + return new AlterEnumeration (en) { + protected Object alter (Object o) { + return map.get (o); + } + }; + } + + protected java.util.Enumeration removeDuplicates (java.util.Enumeration en) { + return new RemoveDuplicatesEnumeration (en); + } + + protected java.util.Enumeration removeNulls (java.util.Enumeration en) { + return new FilterEnumeration (en); + } + + protected java.util.Enumeration concat (java.util.Enumeration en1, java.util.Enumeration en2) { + return new SequenceEnumeration (en1, en2); + } + + protected java.util.Enumeration array (Object[] arr) { + return new ArrayEnumeration (arr); + } + + protected java.util.Enumeration filter (java.util.Enumeration en, final java.util.Set filter) { + return new FilterEnumeration (en) { + protected boolean accept (Object obj) { + return filter.contains (obj); + } + }; + } + + protected java.util.Enumeration concat (java.util.Enumeration enumOfEnums) { + return new SequenceEnumeration (enumOfEnums); + } + + protected java.util.Enumeration empty () { + return new EmptyEnumeration (); + } + + protected java.util.Enumeration queue (Collection init, final QueueProcess process) { + class QEAdd extends QueueEnumeration implements Collection { + protected void process (Object obj) { + process.process (obj, this); + } + + public boolean add (Object o) { + put (o); + return true; + } + + public boolean addAll (Collection c) { + put (c.toArray ()); + return true; + } + + public void clear () { + throw new IllegalStateException ("Unsupported"); + } + + public boolean contains (Object o) { + throw new IllegalStateException ("Unsupported"); + } + + public boolean containsAll (Collection c) { + throw new IllegalStateException ("Unsupported"); + } + + public boolean isEmpty () { + throw new IllegalStateException ("Unsupported"); + } + + public java.util.Iterator iterator () { + throw new IllegalStateException ("Unsupported"); + } + + public boolean remove (Object o) { + throw new IllegalStateException ("Unsupported"); + } + + public boolean removeAll (Collection c) { + throw new IllegalStateException ("Unsupported"); + } + + public boolean retainAll (Collection c) { + throw new IllegalStateException ("Unsupported"); + } + + public int size () { + throw new IllegalStateException ("Unsupported"); + } + + public Object[] toArray () { + throw new IllegalStateException ("Unsupported"); + } + + public Object[] toArray (Object[] a) { + throw new IllegalStateException ("Unsupported"); + } + } + QEAdd qe = new QEAdd (); + qe.put (init.toArray ()); + return qe; + } + +}