jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r982181 - in /jackrabbit/trunk: jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/fl...
Date Wed, 04 Aug 2010 09:50:36 GMT
Author: mduerig
Date: Wed Aug  4 09:50:36 2010
New Revision: 982181

URL: http://svn.apache.org/viewvc?rev=982181&view=rev
Log:
JCR-2698: Consolidate type safe wrappers for commons-collection classes

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Predicate.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Transformer.java
Modified:
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/TreeTraverserTest.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/BTreeManager.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/Rank.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/TreeTraverser.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/GetItemsTest.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/ItemInfoStore.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/benchmark/ReadPerformanceTest.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/ItemInfoBuilder.java

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/TreeTraverserTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/TreeTraverserTest.java?rev=982181&r1=982180&r2=982181&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/TreeTraverserTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/TreeTraverserTest.java
Wed Aug  4 09:50:36 2010
@@ -16,13 +16,14 @@
  */
 package org.apache.jackrabbit.core.integration;
 
-import org.apache.commons.collections.Predicate;
-import org.apache.commons.collections.Transformer;
-import org.apache.commons.collections.iterators.EmptyIterator;
-import org.apache.commons.collections.iterators.FilterIterator;
-import org.apache.commons.collections.iterators.SingletonIterator;
-import org.apache.commons.collections.iterators.TransformIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.filterIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.singleton;
+import static org.apache.jackrabbit.commons.iterator.Iterators.transformIterator;
+
 import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.commons.iterator.Iterators;
+import org.apache.jackrabbit.commons.iterator.Predicate;
+import org.apache.jackrabbit.commons.iterator.Transformer;
 import org.apache.jackrabbit.flat.TreeTraverser;
 import org.apache.jackrabbit.test.AbstractJCRTest;
 
@@ -125,10 +126,10 @@ public class TreeTraverserTest extends A
     public void testTraversePropertiesEmpty() throws RepositoryException {
         Iterator<Node> nodeIt = TreeTraverser.nodeIterator(testNode);
         Iterator<Property> propertyIt = TreeTraverser.propertyIterator(nodeIt);
-        checkProperties(propertyIt, TreeTraverserTest.<String>empty(), testNode.getPath());
+        checkProperties(propertyIt, Iterators.<String>empty(), testNode.getPath());
 
         addNodes(testNode, nodes);
-        checkProperties(propertyIt, TreeTraverserTest.<String>empty(), testNode.getPath());
+        checkProperties(propertyIt, Iterators.<String>empty(), testNode.getPath());
     }
 
     public void testTraverseProperties() throws RepositoryException {
@@ -192,12 +193,10 @@ public class TreeTraverserTest extends A
         assertEquals(expectedPaths, actualPaths);
     }
 
-    @SuppressWarnings("unchecked")
     private static Iterator<Property> removeIgnored(Iterator<Property> properties)
{
-        return new FilterIterator(properties, new Predicate() {
-            public boolean evaluate(Object object) {
+        return filterIterator(properties, new Predicate<Property>() {
+            public boolean evaluate(Property property) {
                 try {
-                    Property property = (Property) object;
                     return !JcrConstants.JCR_PRIMARYTYPE.equals(property.getName());
                 }
                 catch (RepositoryException e) {
@@ -207,12 +206,11 @@ public class TreeTraverserTest extends A
         });
     }
 
-    @SuppressWarnings("unchecked")
     private static Iterator<String> property2Path(Iterator<Property> properties)
{
-        return new TransformIterator(properties, new Transformer() {
-            public Object transform(Object input) {
+        return transformIterator(properties, new Transformer<Property, String>() {
+            public String transform(Property property) {
                 try {
-                    return ((Property) input).getPath();
+                    return property.getPath();
                 }
                 catch (RepositoryException e) {
                     throw (AssertionFailedError) new AssertionFailedError().initCause(e);
@@ -238,23 +236,12 @@ public class TreeTraverserTest extends A
         }
     }
 
-    @SuppressWarnings("unchecked")
     private static Iterator<String> cat(final String s, Iterator<String> strings)
{
-        return new TransformIterator(strings, new Transformer() {
-            public Object transform(Object input) {
-                return cat(s, (String) input);
+        return transformIterator(strings, new Transformer<String, String>() {
+            public String transform(String string) {
+                return cat(s, string);
             }
         });
     }
 
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> singleton(T element) {
-        return new SingletonIterator(element);
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> empty() {
-        return EmptyIterator.INSTANCE;
-    }
-
 }

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java?rev=982181&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Iterators.java
Wed Aug  4 09:50:36 2010
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.jackrabbit.commons.iterator;
+
+import org.apache.commons.collections.iterators.ArrayIterator;
+import org.apache.commons.collections.iterators.EmptyIterator;
+import org.apache.commons.collections.iterators.FilterIterator;
+import org.apache.commons.collections.iterators.IteratorChain;
+import org.apache.commons.collections.iterators.SingletonIterator;
+import org.apache.commons.collections.iterators.TransformIterator;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * Utility class containing type safe adapters for some of the iterators of
+ * commons-collections.
+ */
+public final class Iterators {
+
+    private Iterators() {
+        super();
+    }
+
+    /**
+     * Returns an iterator containing the single element <code>element</code>
of
+     * type <code>T</code>.
+     *
+     * @param <T>
+     * @param element
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Iterator<T> singleton(T element) {
+        return new SingletonIterator(element);
+    }
+
+    /**
+     * Returns an empty iterator of type <code>T</code>.
+     *
+     * @param <T>
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Iterator<T> empty() {
+        return EmptyIterator.INSTANCE;
+    }
+
+    /**
+     * Returns an iterator for the concatenation of <code>iterator1</code> and
+     * <code>iterator2</code>.
+     *
+     * @param <T>
+     * @param iterator1
+     * @param iterator2
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Iterator<T> iteratorChain(Iterator<? extends T> iterator1,
Iterator<? extends T> iterator2) {
+        return new IteratorChain(iterator1, iterator2);
+    }
+
+    /**
+     * Returns an iterator for the concatenation of all the given <code>iterators</code>.
+     *
+     * @param <T>
+     * @param iterators
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Iterator<T> iteratorChain(Iterator<? extends T>[]
iterators) {
+        return new IteratorChain(iterators);
+    }
+
+    /**
+     * Returns an iterator for the concatenation of all the given <code>iterators</code>.
+     *
+     * @param <T>
+     * @param iterators
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Iterator<T> iteratorChain(Collection<? extends T>
iterators) {
+        return new IteratorChain(iterators);
+    }
+
+    /**
+     * Returns an iterator for elements of an array of <code>values</code>.
+     *
+     * @param <T>
+     * @param values  the array to iterate over.
+     * @param from  the index to start iterating at.
+     * @param to  the index to finish iterating at.
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Iterator<T> arrayIterator(T[] values, int from, int to)
{
+        return new ArrayIterator(values, from, to);
+    }
+
+    /**
+     * Returns an iterator with elements from an original <code>iterator</code>
where the
+     * given <code>predicate</code> matches removed.
+     *
+     * @param <T>
+     * @param iterator
+     * @param predicate
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Iterator<T> filterIterator(Iterator<? extends T>
iterator,
+            final Predicate<? super T> predicate) {
+
+        return new FilterIterator(iterator, new org.apache.commons.collections.Predicate()
{
+            public boolean evaluate(Object object) {
+                return predicate.evaluate((T) object);
+            }
+        });
+    }
+
+    /**
+     * Returns an iterator with elements of an original  <code>iterator</code>
transformed by
+     * a <code>transformer</code>.
+     *
+     * @param <T>
+     * @param <R>
+     * @param <S>
+     * @param iterator
+     * @param transformer
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T, R, S extends T> Iterator<R> transformIterator(Iterator<?
extends T> iterator,
+            final Transformer<S, ? super R> transformer) {
+
+        return new TransformIterator(iterator, new org.apache.commons.collections.Transformer()
{
+            public Object transform(Object input) {
+                return transformer.transform((S) input);
+            }
+        });
+    }
+
+    /**
+     * Returns an iterator of {@link Property} from a {@link PropertyIterator}.
+     *
+     * @param propertyIterator
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterator<Property> properties(PropertyIterator propertyIterator)
{
+        return propertyIterator;
+    }
+
+    /**
+     * Returns an iterator of {@link Node} from a {@link NodeIterator}.
+     * @param nodeIterator
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static Iterator<Node> nodes(NodeIterator nodeIterator) {
+        return nodeIterator;
+    }
+
+}

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java?rev=982181&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/LazyIteratorChain.java
Wed Aug  4 09:50:36 2010
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.jackrabbit.commons.iterator;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * This class implements the concatenation of iterators. The implementation
+ * is lazy in the sense that advancing of any iterator is deferred as much
+ * as possible. Specifically no iterator is fully unwrapped at one single
+ * point of time.
+ *
+ * @param <T>  type of values iterating over
+ */
+public class LazyIteratorChain<T> implements Iterator<T> {
+    private final Iterator<Iterator<T>> iterators;
+    private Iterator<T> currentIterator;
+
+    /**
+     * Returns the concatenation of all iterators in <code>iterators</code>.
+     *
+     * @param <T>
+     * @param iterators
+     * @return
+     */
+    public static <T> Iterator<T> chain(Iterator<Iterator<T>> iterators)
{
+        return new LazyIteratorChain<T>(iterators);
+    }
+
+    public LazyIteratorChain(Iterator<Iterator<T>> iterators) {
+        super();
+        this.iterators = iterators;
+    }
+
+    public boolean hasNext() {
+        while ((currentIterator == null || !currentIterator.hasNext()) && iterators.hasNext())
{
+            currentIterator = iterators.next();
+        }
+        return currentIterator != null && currentIterator.hasNext();
+    }
+
+    public T next() {
+        if (hasNext()) {
+            return currentIterator.next();
+        }
+        else {
+            throw new NoSuchElementException();
+        }
+    }
+
+    public void remove() {
+        throw new UnsupportedOperationException();
+    }
+}
\ No newline at end of file

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Predicate.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Predicate.java?rev=982181&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Predicate.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Predicate.java
Wed Aug  4 09:50:36 2010
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.jackrabbit.commons.iterator;
+
+/**
+ * Type safe counter part of {@link org.apache.commons.collections.Predicate}.
+ *
+ * @param <T> type of values this predicate is defined on
+ */
+public interface Predicate<T> {
+
+    /**
+     * Use the specified parameter to perform a test that returns true or false.
+     *
+     * @param arg  the predicate to evaluate, should not be changed
+     * @return true or false
+     */
+    public boolean evaluate(T arg);
+}

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Transformer.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Transformer.java?rev=982181&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Transformer.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/iterator/Transformer.java
Wed Aug  4 09:50:36 2010
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.jackrabbit.commons.iterator;
+
+/**
+ * Type safe counter part of {@link org.apache.commons.collections.Transformer}.
+ *
+ * @param <A>  argument type to transform from
+ * @param <R>  result type to transform to
+ */
+public interface Transformer<A, R> {
+
+    /**
+     * Transforms the input object (leaving it unchanged) into some output object.
+     *
+     * @param input  the object to be transformed, should be left unchanged
+     * @return a transformed object
+     */
+    public R transform(A argument);
+
+}

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/BTreeManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/BTreeManager.java?rev=982181&r1=982180&r2=982181&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/BTreeManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/BTreeManager.java
Wed Aug  4 09:50:36 2010
@@ -16,10 +16,12 @@
  */
 package org.apache.jackrabbit.flat;
 
+import static org.apache.jackrabbit.commons.iterator.Iterators.filterIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.nodes;
+import static org.apache.jackrabbit.commons.iterator.Iterators.properties;
 
-import org.apache.commons.collections.Predicate;
-import org.apache.commons.collections.iterators.FilterIterator;
 import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.commons.iterator.Predicate;
 
 import javax.jcr.Item;
 import javax.jcr.Node;
@@ -254,10 +256,9 @@ public class BTreeManager implements Tre
     /**
      * Returns a {@link SizedIterator} of the child nodes of <code>node</code>.
      */
-    @SuppressWarnings("unchecked")
     protected SizedIterator<Node> getNodes(Node node) throws RepositoryException {
         NodeIterator nodes = node.getNodes();
-        return getSizedIterator(nodes, nodes.getSize());
+        return getSizedIterator(nodes(nodes), nodes.getSize());
     }
 
     /**
@@ -267,17 +268,14 @@ public class BTreeManager implements Tre
     protected SizedIterator<Property> getProperties(final Node node) throws RepositoryException
{
         final PropertyIterator properties = node.getProperties();
 
-        @SuppressWarnings("unchecked")
-        final Iterator<Property> filtered = new FilterIterator(properties, new Predicate()
{
-            public boolean evaluate(Object object) {
-                Property p = (Property) object;
+        Iterator<Property> filtered = filterIterator(properties(properties), new Predicate<Property>()
{
+            public boolean evaluate(Property property) {
                 try {
-                    return !JcrConstants.JCR_PRIMARYTYPE.equals(p.getName());
+                    return !JcrConstants.JCR_PRIMARYTYPE.equals(property.getName());
                 }
                 catch (RepositoryException ignore) {
                     return true;
                 }
-
             }
         });
 

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/Rank.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/Rank.java?rev=982181&r1=982180&r2=982181&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/Rank.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/Rank.java
Wed Aug  4 09:50:36 2010
@@ -16,8 +16,8 @@
  */
 package org.apache.jackrabbit.flat;
 
-import org.apache.commons.collections.iterators.ArrayIterator;
-import org.apache.commons.collections.iterators.EmptyIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.arrayIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.empty;
 
 import java.lang.reflect.Array;
 import java.util.Collection;
@@ -74,11 +74,9 @@ public class Rank<T> {
      * @param componentType type evidence for the values
      * @param order Ordering for ranking
      */
-    @SuppressWarnings("unchecked")
     public Rank(Collection<T> values, Class<T> componentType, Comparator<?
super T> order) {
         super();
-        Object array = Array.newInstance(componentType, values.size());
-        this.values = values.toArray((T[]) array);
+        this.values = toArray(values, componentType);
         this.order = order;
     }
 
@@ -93,13 +91,12 @@ public class Rank<T> {
      * @param count Number of items to include. -1 for all.
      * @param order Ordering for ranking
      */
-    @SuppressWarnings("unchecked")
     public Rank(Iterator<T> values, Class<T> componentType, int count, Comparator<?
super T> order) {
         super();
         this.order = order;
 
         if (count >= 0) {
-            this.values = (T[]) Array.newInstance(componentType, count);
+            this.values = createArray(count, componentType);
             for (int k = 0; k < count; k++) {
                 this.values[k] = values.next();
             }
@@ -109,8 +106,7 @@ public class Rank<T> {
             while (values.hasNext()) {
                 l.add(values.next());
             }
-            Object array = Array.newInstance(componentType, l.size());
-            this.values = l.toArray((T[]) array);
+            this.values = toArray(l, componentType);
         }
     }
 
@@ -267,14 +263,15 @@ public class Rank<T> {
         values[hi] = t1;
     }
 
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> arrayIterator(T[] values, int from, int to)
{
-        return new ArrayIterator(values, from, to);
+    // -----------------------------------------------------< utility >---
+
+    private static <S> S[] toArray(Collection<S> collection, Class<S> componentType)
{
+        return collection.toArray(createArray(collection.size(), componentType));
     }
 
     @SuppressWarnings("unchecked")
-    private Iterator<T> empty() {
-        return EmptyIterator.INSTANCE;
+    private static <S> S[] createArray(int size, Class<S> componentType) {
+        return (S[]) Array.newInstance(componentType, size);
     }
 
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/TreeTraverser.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/TreeTraverser.java?rev=982181&r1=982180&r2=982181&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/TreeTraverser.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/flat/TreeTraverser.java
Wed Aug  4 09:50:36 2010
@@ -16,9 +16,11 @@
  */
 package org.apache.jackrabbit.flat;
 
-import org.apache.commons.collections.iterators.EmptyIterator;
-import org.apache.commons.collections.iterators.IteratorChain;
-import org.apache.commons.collections.iterators.SingletonIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.empty;
+import static org.apache.jackrabbit.commons.iterator.Iterators.iteratorChain;
+import static org.apache.jackrabbit.commons.iterator.Iterators.properties;
+import static org.apache.jackrabbit.commons.iterator.Iterators.singleton;
+import static org.apache.jackrabbit.commons.iterator.LazyIteratorChain.chain;
 
 import javax.jcr.Item;
 import javax.jcr.Node;
@@ -27,7 +29,6 @@ import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 
 import java.util.Iterator;
-import java.util.NoSuchElementException;
 
 /**
  * <p>
@@ -249,7 +250,7 @@ public final class TreeTraverser impleme
     private Iterator<Node> iterator(Node node) {
         try {
             if (inclusionPolicy.include(node)) {
-                return chain(singleton(node), childIterators(node));
+                return iteratorChain(singleton(node), chain(childIterators(node)));
             }
             else {
                 return chain(childIterators(node));
@@ -262,14 +263,6 @@ public final class TreeTraverser impleme
     }
 
     /**
-     * Returns an iterator containing the single element <code>element</code>.
-     */
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> singleton(T element) {
-        return new SingletonIterator(element);
-    }
-
-    /**
      * Returns an iterator of iterators of the child nodes of <code>node</code>.
      */
     private Iterator<Iterator<Node>> childIterators(final Node node) {
@@ -310,11 +303,10 @@ public final class TreeTraverser impleme
                 return nodes.hasNext();
             }
 
-            @SuppressWarnings("unchecked")
             public Iterator<Property> next() {
                 Node n = nodes.next();
                 try {
-                    return n.getProperties();
+                    return properties(n.getProperties());
                 }
                 catch (RepositoryException e) {
                     errorHandler.call(n, e);
@@ -328,64 +320,4 @@ public final class TreeTraverser impleme
         };
     }
 
-    /**
-     * Returns the concatenation of <code>iterator</code> with the concatenation
-     * of all iterators in <code>iterators</code>.
-     */
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> chain(Iterator<T> iterator, Iterator<Iterator<T>>
iterators) {
-        return new IteratorChain(iterator, new LazyIteratorChain<T>(iterators));
-    }
-
-    /**
-     * Returns the concatenation of all iterators in <code>iterators</code>.
-     */
-    private static <T> Iterator<T> chain(Iterator<Iterator<T>> iterators)
{
-        return new LazyIteratorChain<T>(iterators);
-    }
-
-    /**
-     * Returns an empty iterator.
-     */
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> empty() {
-        return EmptyIterator.INSTANCE;
-    }
-
-    /**
-     * The class implements the concatenation of iterators. The implementation
-     * is lazy in the sense that advancing off all iterators is deferred as much
-     * as possible. Specifically no iterator is fully unwrapped at one single
-     * point of time.
-     */
-    private static final class LazyIteratorChain<T> implements Iterator<T> {
-        private final Iterator<Iterator<T>> iterators;
-        private Iterator<T> currentIterator;
-
-        private LazyIteratorChain(Iterator<Iterator<T>> iterators) {
-            super();
-            this.iterators = iterators;
-        }
-
-        public boolean hasNext() {
-            while ((currentIterator == null || !currentIterator.hasNext()) && iterators.hasNext())
{
-                currentIterator = iterators.next();
-            }
-            return currentIterator != null && currentIterator.hasNext();
-        }
-
-        public T next() {
-            if (hasNext()) {
-                return currentIterator.next();
-            }
-            else {
-                throw new NoSuchElementException();
-            }
-        }
-
-        public void remove() {
-            throw new UnsupportedOperationException();
-        }
-    }
-
 }

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/GetItemsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/GetItemsTest.java?rev=982181&r1=982180&r2=982181&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/GetItemsTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/GetItemsTest.java
Wed Aug  4 09:50:36 2010
@@ -16,19 +16,11 @@
  */
 package org.apache.jackrabbit.jcr2spi;
 
-import java.util.Iterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.filterIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.iteratorChain;
+import static org.apache.jackrabbit.commons.iterator.Iterators.singleton;
 
-import javax.jcr.AccessDeniedException;
-import javax.jcr.Item;
-import javax.jcr.ItemNotFoundException;
-import javax.jcr.Node;
-import javax.jcr.PathNotFoundException;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-
-import org.apache.commons.collections.iterators.FilterIterator;
-import org.apache.commons.collections.iterators.IteratorChain;
-import org.apache.commons.collections.iterators.SingletonIterator;
+import org.apache.jackrabbit.commons.iterator.Predicate;
 import org.apache.jackrabbit.spi.ChildInfo;
 import org.apache.jackrabbit.spi.ItemInfo;
 import org.apache.jackrabbit.spi.NodeId;
@@ -40,6 +32,16 @@ import org.apache.jackrabbit.spi.Reposit
 import org.apache.jackrabbit.spi.SessionInfo;
 import org.apache.jackrabbit.spi.commons.ItemInfoBuilder.NodeInfoBuilder;
 
+import javax.jcr.AccessDeniedException;
+import javax.jcr.Item;
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import java.util.Iterator;
+
 /**
  * Test cases for {@link RepositoryService#getItemInfos(SessionInfo, NodeId)}. Specifically
  * for JCR-1797.
@@ -151,9 +153,9 @@ public class GetItemsTest extends Abstra
     public Iterator<ItemInfo> getItemInfos(SessionInfo sessionInfo, final NodeId nodeId)
             throws RepositoryException {
 
-        return chain(
+        return iteratorChain(
                 singleton(itemInfoStore.getNodeInfo(nodeId)),
-                filter(itemInfoStore.getItemInfos(), new Predicate<ItemInfo>() {
+                filterIterator(itemInfoStore.getItemInfos(), new Predicate<ItemInfo>()
{
                     public boolean evaluate(ItemInfo info) {
                         return !nodeId.equals(info.getId());
                     }
@@ -172,29 +174,4 @@ public class GetItemsTest extends Abstra
         return itemInfoStore.getPropertyInfo(propertyId);
     }
 
-    // -----------------------------------------------------< private >---
-
-    private interface Predicate<T> {
-        public boolean evaluate(T value);
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> chain(Iterator<? extends T> first, Iterator<?
extends T> second) {
-        return new IteratorChain(first, second);
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> singleton(T value) {
-        return new SingletonIterator(value);
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> filter(Iterator<T> source, final Predicate<T>
predicate) {
-        return new FilterIterator(source, new org.apache.commons.collections.Predicate()
{
-            public boolean evaluate(Object object) {
-                return predicate.evaluate((T) object);
-            }
-        });
-    }
-
 }

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/ItemInfoStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/ItemInfoStore.java?rev=982181&r1=982180&r2=982181&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/ItemInfoStore.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/ItemInfoStore.java
Wed Aug  4 09:50:36 2010
@@ -16,16 +16,7 @@
  */
 package org.apache.jackrabbit.jcr2spi;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.jcr.ItemNotFoundException;
-
-import org.apache.commons.collections.iterators.EmptyIterator;
+import org.apache.jackrabbit.commons.iterator.Iterators;
 import org.apache.jackrabbit.spi.ChildInfo;
 import org.apache.jackrabbit.spi.ItemId;
 import org.apache.jackrabbit.spi.ItemInfo;
@@ -35,6 +26,15 @@ import org.apache.jackrabbit.spi.Propert
 import org.apache.jackrabbit.spi.PropertyInfo;
 import org.apache.jackrabbit.spi.RepositoryService;
 
+import javax.jcr.ItemNotFoundException;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
 /**
  * In memory store for {@link ItemInfo}s.
  */
@@ -106,7 +106,7 @@ public class ItemInfoStore {
         Iterable<ItemInfo> batch = batches.get(id);
 
         return batch == null
-            ? ItemInfoStore.<ItemInfo>empty()
+            ? Iterators.<ItemInfo>empty()
             : batch.iterator();
     }
 
@@ -186,9 +186,4 @@ public class ItemInfoStore {
         return list;
     }
 
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> empty() {
-        return EmptyIterator.INSTANCE;
-    }
-
 }

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/benchmark/ReadPerformanceTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/benchmark/ReadPerformanceTest.java?rev=982181&r1=982180&r2=982181&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/benchmark/ReadPerformanceTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/benchmark/ReadPerformanceTest.java
Wed Aug  4 09:50:36 2010
@@ -16,24 +16,11 @@
  */
 package org.apache.jackrabbit.jcr2spi.benchmark;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
-import java.util.concurrent.Callable;
+import static org.apache.jackrabbit.commons.iterator.Iterators.filterIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.iteratorChain;
+import static org.apache.jackrabbit.commons.iterator.Iterators.singleton;
 
-import javax.jcr.Item;
-import javax.jcr.ItemNotFoundException;
-import javax.jcr.Node;
-import javax.jcr.Property;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-
-import org.apache.commons.collections.iterators.FilterIterator;
-import org.apache.commons.collections.iterators.IteratorChain;
-import org.apache.commons.collections.iterators.SingletonIterator;
+import org.apache.jackrabbit.commons.iterator.Predicate;
 import org.apache.jackrabbit.jcr2spi.AbstractJCR2SPITest;
 import org.apache.jackrabbit.spi.ChildInfo;
 import org.apache.jackrabbit.spi.ItemInfo;
@@ -45,9 +32,24 @@ import org.apache.jackrabbit.spi.Propert
 import org.apache.jackrabbit.spi.QNodeDefinition;
 import org.apache.jackrabbit.spi.RepositoryService;
 import org.apache.jackrabbit.spi.SessionInfo;
-import org.apache.jackrabbit.spi.commons.ItemInfoCacheImpl;
 import org.apache.jackrabbit.spi.commons.ItemInfoBuilder.NodeInfoBuilder;
 import org.apache.jackrabbit.spi.commons.ItemInfoBuilder.PropertyInfoBuilder;
+import org.apache.jackrabbit.spi.commons.ItemInfoCacheImpl;
+
+import javax.jcr.Item;
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.Callable;
 
 /**
  * Utility for testing jcr2spi read performance
@@ -100,7 +102,7 @@ public class ReadPerformanceTest extends
     private final Random rnd = new Random(12345);
 
     /**
-     * This implementation ovverides the default cache size with the value of
+     * This implementation overrides the default cache size with the value of
      * {@value #ITEM_INFO_CACHE_SIZE}
      */
     @Override
@@ -302,7 +304,7 @@ public class ReadPerformanceTest extends
     }
 
     private Iterator<ItemInfo> getBatch() {
-        return filter(itemInfoStore.getItemInfos(), new Predicate<ItemInfo>() {
+        return filterIterator(itemInfoStore.getItemInfos(), new Predicate<ItemInfo>()
{
             public boolean evaluate(ItemInfo value) {
                 return rnd.nextInt(batchRatio) == 0;
             }
@@ -323,7 +325,7 @@ public class ReadPerformanceTest extends
 
         roundTripCount++;
         NodeInfo nodeInfo = itemInfoStore.getNodeInfo(nodeId);
-        return chain(singleton(nodeInfo), getBatch());
+        return iteratorChain(singleton(nodeInfo), getBatch());
     }
 
     @Override
@@ -348,29 +350,4 @@ public class ReadPerformanceTest extends
         return itemInfoStore.getChildInfos(parentId);
     }
 
-    // -----------------------------------------------------< private >---
-
-    private interface Predicate<T> {
-        public boolean evaluate(T value);
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> chain(Iterator<? extends T> first, Iterator<?
extends T> second) {
-        return new IteratorChain(first, second);
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> singleton(T value) {
-        return new SingletonIterator(value);
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> filter(Iterator<T> source, final Predicate<T>
predicate) {
-        return new FilterIterator(source, new org.apache.commons.collections.Predicate()
{
-            public boolean evaluate(Object object) {
-                return predicate.evaluate((T) object);
-            }
-        });
-    }
-
 }

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/ItemInfoBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/ItemInfoBuilder.java?rev=982181&r1=982180&r2=982181&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/ItemInfoBuilder.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/ItemInfoBuilder.java
Wed Aug  4 09:50:36 2010
@@ -16,23 +16,12 @@
  */
 package org.apache.jackrabbit.spi.commons;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.math.BigDecimal;
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Iterator;
-import java.util.List;
+import static org.apache.jackrabbit.commons.iterator.Iterators.filterIterator;
+import static org.apache.jackrabbit.commons.iterator.Iterators.transformIterator;
 
-import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
-
-import org.apache.commons.collections.Transformer;
-import org.apache.commons.collections.iterators.EmptyIterator;
-import org.apache.commons.collections.iterators.FilterIterator;
-import org.apache.commons.collections.iterators.TransformIterator;
+import org.apache.jackrabbit.commons.iterator.Iterators;
+import org.apache.jackrabbit.commons.iterator.Predicate;
+import org.apache.jackrabbit.commons.iterator.Transformer;
 import org.apache.jackrabbit.spi.ChildInfo;
 import org.apache.jackrabbit.spi.ItemInfo;
 import org.apache.jackrabbit.spi.Name;
@@ -42,12 +31,26 @@ import org.apache.jackrabbit.spi.Path;
 import org.apache.jackrabbit.spi.PropertyId;
 import org.apache.jackrabbit.spi.PropertyInfo;
 import org.apache.jackrabbit.spi.QValue;
+import org.apache.jackrabbit.spi.commons.ItemInfoBuilder.NodeInfoBuilder;
 import org.apache.jackrabbit.spi.commons.identifier.IdFactoryImpl;
 import org.apache.jackrabbit.spi.commons.name.NameConstants;
 import org.apache.jackrabbit.spi.commons.name.NameFactoryImpl;
 import org.apache.jackrabbit.spi.commons.name.PathFactoryImpl;
 import org.apache.jackrabbit.spi.commons.value.QValueFactoryImpl;
 
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigDecimal;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Iterator;
+import java.util.List;
+
 /**
  * Utility class providing a {@link NodeInfoBuilder} for building {@link NodeInfo}.
  * Example usage:
@@ -265,7 +268,7 @@ public final class ItemInfoBuilder {
                 NodeId id = getId();
 
                 nodeInfo = new NodeInfoImpl(id.getPath(), id, index, primaryTypeName,
-                        mixins.toArray(new Name[mixins.size()]), EmptyIterator.INSTANCE,
+                        mixins.toArray(new Name[mixins.size()]), Iterators.<PropertyId>empty(),
                         getPropertyIds(), includeChildInfos ? getChildInfos() : null);
 
                 if (listener != null) {
@@ -333,9 +336,9 @@ public final class ItemInfoBuilder {
         }
 
         private Iterator<ChildInfo> getChildInfos() {
-            return map(itemInfos.iterator(),
-                    new Function<ItemInfo, ChildInfo>(){
-                        public ChildInfo apply(ItemInfo info) {
+            return transformIterator(itemInfos.iterator(),
+                    new Transformer<ItemInfo, ChildInfo>(){
+                        public ChildInfo transform(ItemInfo info) {
                             Name name = info.getPath().getNameElement().getName();
                             return new ChildInfoImpl(name, null, Path.INDEX_DEFAULT);
                         }
@@ -343,14 +346,14 @@ public final class ItemInfoBuilder {
         }
 
         private Iterator<PropertyId> getPropertyIds() {
-            return map(filter(itemInfos.iterator(),
+            return transformIterator(filterIterator(itemInfos.iterator(),
                     new Predicate<ItemInfo>(){
                         public boolean evaluate(ItemInfo info) {
                             return !info.denotesNode();
                         }
                     }),
-                    new Function<ItemInfo, PropertyId>(){
-                        public PropertyId apply(ItemInfo info) {
+                    new Transformer<ItemInfo, PropertyId>(){
+                        public PropertyId transform(ItemInfo info) {
                             return (PropertyId) info.getId();
                         }
                     });
@@ -631,32 +634,4 @@ public final class ItemInfoBuilder {
 
     }
 
-    // -----------------------------------------------------< private >---
-
-    private interface Predicate<T> {
-        public boolean evaluate(T value);
-    }
-
-    private interface Function<S, T> {
-        public T apply(S s);
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <S, T> Iterator<T> map(Iterator<S> source, final Function<S,
T> f) {
-        return new TransformIterator(source, new Transformer() {
-            public Object transform(Object o) {
-                return f.apply((S) o);
-            }
-        });
-    }
-
-    @SuppressWarnings("unchecked")
-    private static <T> Iterator<T> filter(Iterator<T> source, final Predicate<T>
predicate) {
-        return new FilterIterator(source, new org.apache.commons.collections.Predicate()
{
-            public boolean evaluate(Object object) {
-                return predicate.evaluate((T) object);
-            }
-        });
-    }
-
 }



Mime
View raw message