#88606: NbCollections.iterable methods added, together with a few example usages. before-new-xtest-db coco_base vwp551_root
authorjglick@netbeans.org
Mon, 13 Nov 2006 20:11:53 +0000
changeset 2331c405de40b4f
parent 232 1e104e66e371
child 234 3e4dea92b45b
#88606: NbCollections.iterable methods added, together with a few example usages.
openide.util/apichanges.xml
openide.util/nbproject/project.properties
openide.util/src/org/openide/util/Enumerations.java
openide.util/src/org/openide/util/NbBundle.java
openide.util/src/org/openide/util/NbCollections.java
openide.util/test/unit/src/org/openide/util/NbCollectionsTest.java
     1.1 --- a/openide.util/apichanges.xml	Fri Nov 10 15:25:08 2006 +0000
     1.2 +++ b/openide.util/apichanges.xml	Mon Nov 13 20:11:53 2006 +0000
     1.3 @@ -26,6 +26,24 @@
     1.4      <apidef name="actions">Actions API</apidef>
     1.5  </apidefs>
     1.6  <changes>
     1.7 +
     1.8 +    <change id="NbCollections.iterable">
     1.9 +        <api name="util"/>
    1.10 +        <summary>Added <code>NbCollections.iterable(...)</code> methods</summary>
    1.11 +        <version major="7" minor="5"/>
    1.12 +        <date day="13" month="11" year="2006"/>
    1.13 +        <author login="jglick"/>
    1.14 +        <compatibility addition="yes"/>
    1.15 +        <description>
    1.16 +            <p>
    1.17 +                Added two new methods to make enhanced for-loops easier to use
    1.18 +                with legacy APIs returning <code>Iterator</code> or <code>Enumeration</code>.
    1.19 +            </p>
    1.20 +        </description>
    1.21 +        <class package="org.openide.util" name="NbCollections"/>
    1.22 +        <issue number="88606"/>
    1.23 +    </change>
    1.24 +
    1.25      <change id="nbpreferences">
    1.26          <api name="util"/>
    1.27          <summary>Added <code>NbPreferences.forModule(Class cls)</code> and
     2.1 --- a/openide.util/nbproject/project.properties	Fri Nov 10 15:25:08 2006 +0000
     2.2 +++ b/openide.util/nbproject/project.properties	Mon Nov 13 20:11:53 2006 +0000
     2.3 @@ -19,7 +19,7 @@
     2.4  javac.source=1.5
     2.5  module.jar.dir=lib
     2.6  
     2.7 -spec.version.base=7.4.0
     2.8 +spec.version.base=7.5.0
     2.9  
    2.10  # For XMLSerializer, needed for XMLUtil.write to work w/ namespaces under JDK 1.4:
    2.11  
     3.1 --- a/openide.util/src/org/openide/util/Enumerations.java	Fri Nov 10 15:25:08 2006 +0000
     3.2 +++ b/openide.util/src/org/openide/util/Enumerations.java	Mon Nov 13 20:11:53 2006 +0000
     3.3 @@ -36,6 +36,7 @@
     3.4   * @since 4.37
     3.5   * @author Jaroslav Tulach
     3.6   * @see NbCollections#checkedEnumerationByFilter
     3.7 + * @see NbCollections#iterable(Enumeration)
     3.8   */
     3.9  public final class Enumerations extends Object {
    3.10      /** No instances */
     4.1 --- a/openide.util/src/org/openide/util/NbBundle.java	Fri Nov 10 15:25:08 2006 +0000
     4.2 +++ b/openide.util/src/org/openide/util/NbBundle.java	Mon Nov 13 20:11:53 2006 +0000
     4.3 @@ -265,10 +265,8 @@
     4.4      * @return the localized object or <code>null</code> if no key matches
     4.5      */
     4.6      public static <T> T getLocalizedValue(Map<String,T> table, String key, Locale locale) {
     4.7 -        Iterator<String> it = new LocaleIterator(locale);
     4.8 -
     4.9 -        while (it.hasNext()) {
    4.10 -            String physicalKey = key + it.next();
    4.11 +        for (String suffix : NbCollections.iterable(new LocaleIterator(locale))) {
    4.12 +            String physicalKey = key + suffix;
    4.13              T v = table.get(physicalKey);
    4.14  
    4.15              if (v != null) {
     5.1 --- a/openide.util/src/org/openide/util/NbCollections.java	Fri Nov 10 15:25:08 2006 +0000
     5.2 +++ b/openide.util/src/org/openide/util/NbCollections.java	Mon Nov 13 20:11:53 2006 +0000
     5.3 @@ -462,4 +462,78 @@
     5.4          });
     5.5      }
     5.6  
     5.7 +    /**
     5.8 +     * Treat an {@link Iterator} as an {@link Iterable} so it can be used in an enhanced for-loop.
     5.9 +     * Bear in mind that the iterator is "consumed" by the loop and so should be used only once.
    5.10 +     * Generally it is best to put the code which obtains the iterator inside the loop header.
    5.11 +     * <div class="nonnormative">
    5.12 +     * <p>Example of correct usage:</p>
    5.13 +     * <pre>
    5.14 +     * String text = ...;
    5.15 +     * for (String token : NbCollections.iterable(new {@link java.util.Scanner}(text))) {
    5.16 +     *     // ...
    5.17 +     * }
    5.18 +     * </pre>
    5.19 +     * </div>
    5.20 +     * @param iterator an iterator
    5.21 +     * @return an iterable wrapper which will traverse the iterator once
    5.22 +     * @throws NullPointerException if the iterator is null
    5.23 +     * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6312085">Java bug #6312085</a>
    5.24 +     * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6360734">Java bug #6360734</a>
    5.25 +     * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4988624">Java bug #4988624</a>
    5.26 +     * @since org.openide.util 7.5
    5.27 +     */
    5.28 +    public static <E> Iterable<E> iterable(final Iterator<E> iterator) {
    5.29 +        if (iterator == null) {
    5.30 +            throw new NullPointerException();
    5.31 +        }
    5.32 +        return new Iterable<E>() {
    5.33 +            public Iterator<E> iterator() {
    5.34 +                return iterator;
    5.35 +            }
    5.36 +        };
    5.37 +    }
    5.38 +
    5.39 +    /**
    5.40 +     * Treat an {@link Enumeration} as an {@link Iterable} so it can be used in an enhanced for-loop.
    5.41 +     * Bear in mind that the enumeration is "consumed" by the loop and so should be used only once.
    5.42 +     * Generally it is best to put the code which obtains the enumeration inside the loop header.
    5.43 +     * <div class="nonnormative">
    5.44 +     * <p>Example of correct usage:</p>
    5.45 +     * <pre>
    5.46 +     * ClassLoader loader = ...;
    5.47 +     * String name = ...;
    5.48 +     * for (URL resource : NbCollections.iterable(loader.{@link ClassLoader#getResources getResources}(name))) {
    5.49 +     *     // ...
    5.50 +     * }
    5.51 +     * </pre>
    5.52 +     * </div>
    5.53 +     * @param enumeration an enumeration
    5.54 +     * @return an iterable wrapper which will traverse the enumeration once
    5.55 +     *         ({@link Iterator#remove} is not supported)
    5.56 +     * @throws NullPointerException if the enumeration is null
    5.57 +     * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6349852">Java bug #6349852</a>
    5.58 +     * @since org.openide.util 7.5
    5.59 +     */
    5.60 +    public static <E> Iterable<E> iterable(final Enumeration<E> enumeration) {
    5.61 +        if (enumeration == null) {
    5.62 +            throw new NullPointerException();
    5.63 +        }
    5.64 +        return new Iterable<E>() {
    5.65 +            public Iterator<E> iterator() {
    5.66 +                return new Iterator<E>() {
    5.67 +                    public boolean hasNext() {
    5.68 +                        return enumeration.hasMoreElements();
    5.69 +                    }
    5.70 +                    public E next() {
    5.71 +                        return enumeration.nextElement();
    5.72 +                    }
    5.73 +                    public void remove() {
    5.74 +                        throw new UnsupportedOperationException();
    5.75 +                    }
    5.76 +                };
    5.77 +            }
    5.78 +        };
    5.79 +    }
    5.80 +
    5.81  }
     6.1 --- a/openide.util/test/unit/src/org/openide/util/NbCollectionsTest.java	Fri Nov 10 15:25:08 2006 +0000
     6.2 +++ b/openide.util/test/unit/src/org/openide/util/NbCollectionsTest.java	Mon Nov 13 20:11:53 2006 +0000
     6.3 @@ -24,6 +24,7 @@
     6.4  import java.io.IOException;
     6.5  import java.io.ObjectInputStream;
     6.6  import java.io.ObjectOutputStream;
     6.7 +import java.net.URL;
     6.8  import java.util.ArrayList;
     6.9  import java.util.Arrays;
    6.10  import java.util.Collections;
    6.11 @@ -35,6 +36,7 @@
    6.12  import java.util.List;
    6.13  import java.util.Map;
    6.14  import java.util.NoSuchElementException;
    6.15 +import java.util.Scanner;
    6.16  import java.util.Set;
    6.17  import org.netbeans.junit.NbTestCase;
    6.18  
    6.19 @@ -420,4 +422,33 @@
    6.20          return new ObjectInputStream(bais).readObject();
    6.21      }
    6.22  
    6.23 +    public void testIterable() throws Exception {
    6.24 +        String text = "hello kitty!";
    6.25 +        List<String> l1 = new ArrayList<String>();
    6.26 +        for (String token : NbCollections.iterable(new Scanner(text))) {
    6.27 +            l1.add(token);
    6.28 +        }
    6.29 +        assertEquals(Arrays.asList("hello", "kitty!"), l1);
    6.30 +        for (String token : NbCollections.iterable(new Scanner(""))) {
    6.31 +            fail();
    6.32 +        }
    6.33 +        try {
    6.34 +            NbCollections.iterable((Iterator<?>) null);
    6.35 +            fail();
    6.36 +        } catch (NullPointerException x) {/* OK */}
    6.37 +        List<URL> l2 = new ArrayList<URL>();
    6.38 +        for (URL u : NbCollections.iterable(NbCollections.class.getClassLoader().getResources(NbCollections.class.getName().replace('.', '/') + ".class"))) {
    6.39 +            assertNotNull(u);
    6.40 +            l2.add(u);
    6.41 +        }
    6.42 +        assertFalse(l2.isEmpty()); // permissible to have >1 element in case JAR doubly added to CP
    6.43 +        for (URL u : NbCollections.iterable(NbCollections.class.getClassLoader().getResources("nonexistent"))) {
    6.44 +            fail();
    6.45 +        }
    6.46 +        try {
    6.47 +            NbCollections.iterable((Enumeration<?>) null);
    6.48 +            fail();
    6.49 +        } catch (NullPointerException x) {/* OK */}
    6.50 +    }
    6.51 +
    6.52  }