# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /space/src/nb_all/openide/util # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: src/org/openide/util/NbCollections.java *** /space/src/nb_all/openide/util/src/org/openide/util/NbCollections.java Base (1.4) --- /space/src/nb_all/openide/util/src/org/openide/util/NbCollections.java Locally Modified (Based On 1.4) *************** *** 462,468 **** --- 462,543 ---- }); } + /** + * Treat an {@link Iterator} as an {@link Iterable} so it can be used in an enhanced for-loop. + * Bear in mind that the iterator is "consumed" by the loop and so should be used only once. + * Generally it is best to put the code which obtains the iterator inside the loop header. + *
+ *

Example of correct usage:

+ *
+      * String text = ...;
+      * for (String token : NbCollections.iterable(new {@link java.util.Scanner}(text))) {
+      *     // ...
+      * }
+      * 
+ *
+ * @param iterator an iterator + * @return an iterable wrapper which will iterate through the iterator once + * @throws NullPointerException if the iterator is null + * @see Java bug #6312085 + * @see Java bug #6360734 + * @see Java bug #4988624 + * @since XXX + */ + public static Iterable iterable(final Iterator iterator) { + if (iterator == null) { + throw new NullPointerException(); } + return new Iterable() { + public Iterator iterator() { + return iterator; + } + }; + } + + /** + * Treat an {@link Enumeration} as an {@link Iterable} so it can be used in an enhanced for-loop. + * Bear in mind that the enumeration is "consumed" by the loop and so should be used only once. + * Generally it is best to put the code which obtains the enumeration inside the loop header. + *
+ *

Example of correct usage:

+ *
+      * ClassLoader loader = ...;
+      * String name = ...;
+      * for (URL resource : NbCollections.iterable(loader.{@link ClassLoader#getResources getResources}(name))) {
+      *     // ...
+      * }
+      * 
+ *
+ * @param enumeration an enumeration + * @return an iterable wrapper which will iterate through the enumeration once + * ({@link Iterator#remove} is not supported) + * @throws NullPointerException if the enumeration is null + * @see Java bug #6349852 + * @since XXX + */ + public static Iterable iterable(final Enumeration enumeration) { + if (enumeration == null) { + throw new NullPointerException(); + } + return new Iterable() { + public Iterator iterator() { + return new Iterator() { + public boolean hasNext() { + return enumeration.hasMoreElements(); + } + public E next() { + return enumeration.nextElement(); + } + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; + } + + } Index: test/unit/src/org/openide/util/NbCollectionsTest.java *** /space/src/nb_all/openide/util/test/unit/src/org/openide/util/NbCollectionsTest.java Base (1.3) --- /space/src/nb_all/openide/util/test/unit/src/org/openide/util/NbCollectionsTest.java Locally Modified (Based On 1.3) *************** *** 24,29 **** --- 24,30 ---- import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; + import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; *************** *** 35,40 **** --- 36,42 ---- import java.util.List; import java.util.Map; import java.util.NoSuchElementException; + import java.util.Scanner; import java.util.Set; import org.netbeans.junit.NbTestCase; *************** *** 420,426 **** --- 422,458 ---- return new ObjectInputStream(bais).readObject(); } + public void testIterable() throws Exception { + String text = "hello kitty!"; + List l1 = new ArrayList(); + for (String token : NbCollections.iterable(new Scanner(text))) { + l1.add(token); } + assertEquals(Arrays.asList("hello", "kitty!"), l1); + for (String token : NbCollections.iterable(new Scanner(""))) { + fail(); + } + try { + NbCollections.iterable((Iterator) null); + fail(); + } catch (NullPointerException x) {/* OK */} + List l2 = new ArrayList(); + for (URL u : NbCollections.iterable(NbCollections.class.getClassLoader().getResources(NbCollections.class.getName().replace('.', '/') + ".class"))) { + assertNotNull(u); + l2.add(u); + } + assertFalse(l2.isEmpty()); // permissible to have >1 element in case JAR doubly added to CP + for (URL u : NbCollections.iterable(NbCollections.class.getClassLoader().getResources("nonexistent"))) { + fail(); + } + try { + NbCollections.iterable((Enumeration) null); + fail(); + } catch (NullPointerException x) {/* OK */} + } + + }