You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2009/07/22 14:04:52 UTC

svn commit: r796682 - in /sling/trunk/bundles/api/src: main/java/org/apache/sling/api/resource/ResourceUtil.java test/java/org/apache/sling/api/resource/ResourceUtilTest.java

Author: cziegeler
Date: Wed Jul 22 12:04:52 2009
New Revision: 796682

URL: http://svn.apache.org/viewvc?rev=796682&view=rev
Log:
SLING-697 : Add ResourceUtil.adaptTo(Iterator<Resource>, Class) to adapt all resources in to the given class.

Modified:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
    sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java?rev=796682&r1=796681&r2=796682&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java Wed Jul 22 12:04:52 2009
@@ -21,6 +21,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.NoSuchElementException;
 
 import org.apache.sling.api.wrappers.ValueMapDecorator;
 
@@ -397,4 +398,38 @@
         }
         return false;
     }
+
+    /**
+     * @param <T>
+     * @since 2.0.6
+     */
+    public static <T> Iterator<T> adaptTo(final Iterator<Resource> iterator, final Class<T> type) {
+        return new Iterator<T>() {
+
+            private T nextObject;
+
+            public boolean hasNext() {
+                while ( nextObject == null && iterator.hasNext() ) {
+                    final Resource r = iterator.next();
+                    nextObject = r.adaptTo(type);
+                }
+                return nextObject != null;
+            }
+
+            public T next() {
+                hasNext();
+                if ( nextObject == null ) {
+                    throw new NoSuchElementException();
+                }
+                final T object = nextObject;
+                nextObject = null;
+                return object;
+            }
+
+            public void remove() {
+                throw new UnsupportedOperationException();
+            }
+
+        };
+    }
 }

Modified: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java?rev=796682&r1=796681&r2=796682&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java (original)
+++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java Wed Jul 22 12:04:52 2009
@@ -25,8 +25,12 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 
 import org.apache.sling.api.wrappers.ValueMapDecorator;
 import org.jmock.Expectations;
@@ -309,4 +313,64 @@
         assertTrue(ResourceUtil.isA(r, "t:c"));
         assertFalse(ResourceUtil.isA(r, "h:p"));
     }
+
+    @SuppressWarnings("unchecked")
+    @Test public void test_adaptTo() {
+        // we define three resources
+        // a and b are adaptable to List
+        // a, b, and c are adaptable to Map
+        // none is adaptable to String
+        // b and c are adaptable to long
+        // a and c are adaptable to boolean
+        final Resource a = this.context.mock(Resource.class);
+        final Resource b = this.context.mock(Resource.class);
+        final Resource c = this.context.mock(Resource.class);
+        final List<Resource> l = new ArrayList<Resource>();
+        l.add(a); l.add(b); l.add(c);
+        this.context.checking(new Expectations() {{
+            allowing(a).adaptTo(List.class); will(returnValue(new ArrayList()));
+            allowing(b).adaptTo(List.class); will(returnValue(new ArrayList()));
+            allowing(c).adaptTo(List.class); will(returnValue(null));
+            allowing(a).adaptTo(Map.class); will(returnValue(new HashMap()));
+            allowing(b).adaptTo(Map.class); will(returnValue(new HashMap()));
+            allowing(c).adaptTo(Map.class); will(returnValue(new HashMap()));
+            allowing(a).adaptTo(Long.class); will(returnValue(null));
+            allowing(b).adaptTo(Long.class); will(returnValue(new Long(1)));
+            allowing(c).adaptTo(Long.class); will(returnValue(new Long(2)));
+            allowing(a).adaptTo(Boolean.class); will(returnValue(new Boolean(true)));
+            allowing(b).adaptTo(Boolean.class); will(returnValue(null));
+            allowing(c).adaptTo(Boolean.class); will(returnValue(new Boolean(false)));
+            allowing(a).adaptTo(String.class); will(returnValue(null));
+            allowing(b).adaptTo(String.class); will(returnValue(null));
+            allowing(c).adaptTo(String.class); will(returnValue(null));
+        }});
+
+        assertEquals(2, checkIterator(l, List.class));
+        assertEquals(3, checkIterator(l, Map.class));
+        assertEquals(0, checkIterator(l, String.class));
+        assertEquals(2, checkIterator(l, Long.class));
+        assertEquals(2, checkIterator(l, Boolean.class));
+    }
+
+    private <T> int checkIterator(final List<Resource> resources, final Class<T> type) {
+        final Iterator<T> i = ResourceUtil.adaptTo(resources.iterator(), type);
+        // we call hasNext() several times upfront
+        i.hasNext();
+        i.hasNext();
+        int count = 0;
+        while ( i.hasNext() ) {
+            final T object = i.next();
+            assertNotNull(object);
+            count++;
+        }
+        assertFalse(i.hasNext());
+        // next should throw an exception
+        try {
+            i.next();
+            fail("Iterator should have reached end.");
+        } catch (NoSuchElementException nsee) {
+            // fine
+        }
+        return count;
+    }
 }