jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1364113 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ oak-core/src/main/java/org/apache/jackrabbit/oak/security/privilege/ oak-core/src...
Date Sat, 21 Jul 2012 15:20:05 GMT
Author: jukka
Date: Sat Jul 21 15:20:04 2012
New Revision: 1364113

URL: http://svn.apache.org/viewvc?rev=1364113&view=rev
Log:
OAK-200: Replace Commons Collections with Guava

Simplify code using higher-level collection abstractions in Guava

Removed:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/PagedIterator.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryChildNodeEntry.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/privilege/PrivilegeRegistry.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/ArrayUtils.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeState.java?rev=1364113&r1=1364112&r2=1364113&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeState.java
Sat Jul 21 15:20:04 2012
@@ -21,12 +21,10 @@ package org.apache.jackrabbit.oak.kernel
 import static org.apache.jackrabbit.oak.kernel.CoreValueMapper.fromJsopReader;
 import static org.apache.jackrabbit.oak.kernel.CoreValueMapper.listFromJsopReader;
 
-import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 
 import javax.annotation.Nonnull;
 
@@ -42,9 +40,9 @@ import org.apache.jackrabbit.oak.spi.sta
 import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
-import org.apache.jackrabbit.oak.util.PagedIterator;
 
-import com.google.common.collect.Iterators;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 
 /**
  * Basic {@link NodeState} implementation based on the {@link MicroKernel}
@@ -168,18 +166,20 @@ final class KernelNodeState extends Abst
 
     @Override
     public Iterable<? extends ChildNodeEntry> getChildNodeEntries() {
-        return new Iterable<ChildNodeEntry>() {
-            @Override
-            public Iterator<ChildNodeEntry> iterator() {
-                return  Iterators.concat(
-                    new PagedIterator<ChildNodeEntry>(MAX_CHILD_NODE_NAMES) {
-                        @Override
-                        protected Iterator<? extends ChildNodeEntry> getPage(long pos,
int size) {
-                            return getChildNodeEntries(pos, size);
-                        }
-                });
+        init();
+        Iterable<ChildNodeEntry> iterable =
+                MemoryChildNodeEntry.iterable(childNodes.entrySet());
+        if (childNodeCount > childNodes.size()) {
+            List<Iterable<ChildNodeEntry>> iterables = Lists.newArrayList();
+            iterables.add(iterable);
+            long offset = childNodes.size();
+            while (offset < childNodeCount) {
+                iterables.add(getChildNodeEntries(offset, MAX_CHILD_NODE_NAMES));
+                offset += MAX_CHILD_NODE_NAMES;
             }
-        };
+            iterable = Iterables.concat(iterables);
+        }
+        return iterable;
     }
 
     /**
@@ -261,60 +261,35 @@ final class KernelNodeState extends Abst
 
     //------------------------------------------------------------< private >---
 
-    private Iterator<? extends ChildNodeEntry> getChildNodeEntries(
-            long offset, int count) {
-        init();
-        boolean all;
-        if (count == -1) {
-            count = Integer.MAX_VALUE;
-            all = true;
-            if (childNodeCount > count) {
-                throw new RuntimeException("Too many child nodes");
-            }
-        } else {
-            all = false;
-        }
-
-        List<ChildNodeEntry> entries = new ArrayList<ChildNodeEntry>();
-
-        if (offset < childNodes.size()) {
-            Iterator<Entry<String, NodeState>> iterator =
-                    childNodes.entrySet().iterator();
-            while (offset > 0) {
-                iterator.next();
-                offset--;
-            }
-            while (count > 0 && iterator.hasNext()) {
-                entries.add(new MemoryChildNodeEntry(iterator.next()));
-                count--;
+    private Iterable<ChildNodeEntry> getChildNodeEntries(
+            final long offset, final int count) {
+        return new Iterable<ChildNodeEntry>() {
+            @Override
+            public Iterator<ChildNodeEntry> iterator() {
+                List<ChildNodeEntry> entries =
+                        Lists.newArrayListWithCapacity(count);
+                String json = kernel.getNodes(
+                        path, revision, 0, offset, count, null);
+                JsopReader reader = new JsopTokenizer(json);
+                reader.read('{');
+                do {
+                    String name = reader.readString();
+                    reader.read(':');
+                    if (reader.matches('{')) {
+                        reader.read('}');
+                        String childPath = getChildPath(name);
+                        NodeState child =
+                                new KernelNodeState(kernel, childPath, revision);
+                        entries.add(new MemoryChildNodeEntry(name, child));
+                    } else {
+                        reader.read();
+                    }
+                } while (reader.matches(','));
+                reader.read('}');
+                reader.read(JsopReader.END);
+                return entries.iterator();
             }
-            offset = childNodes.size();
-        }
-
-        if (count > 0 && childNodeCount > MAX_CHILD_NODE_NAMES) {
-            String json = kernel.getNodes(
-                    path, revision, 0, offset, all ? -1 : count, null);
-
-            JsopReader reader = new JsopTokenizer(json);
-            reader.read('{');
-            do {
-                String name = reader.readString();
-                reader.read(':');
-                if (reader.matches('{')) {
-                    reader.read('}');
-                    String childPath = getChildPath(name);
-                    NodeState child =
-                            new KernelNodeState(kernel, childPath, revision);
-                    entries.add(new MemoryChildNodeEntry(name, child));
-                } else {
-                    reader.read();
-                }
-            } while (reader.matches(','));
-            reader.read('}');
-            reader.read(JsopReader.END);
-        }
-
-        return entries.iterator();
+        };
     }
 
     private String getChildPath(String name) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryChildNodeEntry.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryChildNodeEntry.java?rev=1364113&r1=1364112&r2=1364113&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryChildNodeEntry.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryChildNodeEntry.java
Sat Jul 21 15:20:04 2012
@@ -18,16 +18,15 @@
  */
 package org.apache.jackrabbit.oak.plugins.memory;
 
+import java.util.Map;
+import java.util.Map.Entry;
+
 import org.apache.jackrabbit.oak.spi.state.AbstractChildNodeEntry;
 import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 
 import com.google.common.base.Function;
-import com.google.common.collect.Iterators;
-
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Map.Entry;
+import com.google.common.collect.Iterables;
 
 /**
  * Basic JavaBean implementation of a child node entry.
@@ -36,10 +35,10 @@ public class MemoryChildNodeEntry extend
     private final String name;
     private final NodeState node;
 
-    public static Iterator<ChildNodeEntry> iterator(
-            final Iterator<Entry<String, NodeState>> iterator) {
-        return Iterators.transform(
-                iterator,
+    public static Iterable<ChildNodeEntry> iterable(
+            final Iterable<Entry<String, NodeState>> iterable) {
+        return Iterables.transform(
+                iterable,
                 new Function<Entry<String, NodeState>, ChildNodeEntry>() {
                     @Override
                     public ChildNodeEntry apply(Entry<String, NodeState> input) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeState.java?rev=1364113&r1=1364112&r2=1364113&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MemoryNodeState.java
Sat Jul 21 15:20:04 2012
@@ -24,7 +24,6 @@ import org.apache.jackrabbit.oak.spi.sta
 
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 
 /**
@@ -88,12 +87,7 @@ public class MemoryNodeState extends Abs
 
     @Override
     public Iterable<ChildNodeEntry> getChildNodeEntries() {
-        return new Iterable<ChildNodeEntry>() {
-            @Override
-            public Iterator<ChildNodeEntry> iterator() {
-                return MemoryChildNodeEntry.iterator(nodes.entrySet().iterator());
-            }
-        };
+        return MemoryChildNodeEntry.iterable(nodes.entrySet());
     }
 
     /**

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java?rev=1364113&r1=1364112&r2=1364113&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
Sat Jul 21 15:20:04 2012
@@ -24,11 +24,10 @@ import org.apache.jackrabbit.oak.spi.sta
 
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
-import com.google.common.collect.Iterators;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
 
-import java.util.Iterator;
 import java.util.Map;
-import java.util.Map.Entry;
 
 public class ModifiedNodeState extends AbstractNodeState {
 
@@ -80,21 +79,18 @@ public class ModifiedNodeState extends A
         if (properties.isEmpty()) {
             return base.getProperties(); // shortcut
         }
-        final Iterable<? extends PropertyState> unmodified = base.getProperties();
-        final Iterable<? extends PropertyState> modified = properties.values();
 
-        return new Iterable<PropertyState>() {
-            @Override
-            public Iterator<PropertyState> iterator() {
-                Iterator<? extends PropertyState> a = Iterators.filter(
-                        unmodified.iterator(), new UnmodifiedPropertyPredicate());
-
-                Iterator<? extends PropertyState> b = Iterators.filter(
-                        modified.iterator(), Predicates.notNull());
-
-                return Iterators.concat(a, b);
-            }
-        };
+        Iterable<? extends PropertyState> unmodified = Iterables.filter(
+                base.getProperties(),
+                new Predicate<PropertyState>() {
+                    @Override
+                    public boolean apply(PropertyState property) {
+                        return !properties.containsKey(property.getName());
+                    }
+                });
+        Iterable<PropertyState> modified =
+                Iterables.filter(properties.values(), Predicates.notNull());
+        return Iterables.concat(unmodified, modified);
     }
 
     @Override
@@ -128,21 +124,18 @@ public class ModifiedNodeState extends A
         if (nodes.isEmpty()) {
             return base.getChildNodeEntries(); // shortcut
         }
-        final Iterable<? extends ChildNodeEntry> unmodified = base.getChildNodeEntries();
-        final Iterator<Entry<String, NodeState>> modified = nodes.entrySet().iterator();
 
-        return new Iterable<ChildNodeEntry>() {
-            @Override
-            public Iterator<ChildNodeEntry> iterator() {
-                Iterator<? extends ChildNodeEntry> a = Iterators.filter(
-                        unmodified.iterator(), new UnmodifiedChildNodePredicate());
-
-                Iterator<Entry<String, NodeState>> b = Iterators.filter(
-                        modified, new UndeletedChildNodePredicate());
-
-                return Iterators.concat(a, MemoryChildNodeEntry.iterator(b));
-            }
-        };
+        Iterable<? extends ChildNodeEntry> unmodified = Iterables.filter(
+                base.getChildNodeEntries(),
+                new Predicate<ChildNodeEntry>() {
+                    @Override
+                    public boolean apply(ChildNodeEntry input) {
+                        return !nodes.containsKey(input.getName());
+                    }
+                });
+        Iterable<ChildNodeEntry> modified = MemoryChildNodeEntry.iterable(
+                Maps.filterValues(nodes, Predicates.notNull()).entrySet());
+        return Iterables.concat(unmodified, modified);
     }
 
     /**
@@ -226,27 +219,4 @@ public class ModifiedNodeState extends A
         }
     }
 
-    //-----------------------------------------------------------< private >--
-
-    private class UnmodifiedPropertyPredicate implements Predicate<PropertyState> {
-        @Override
-        public boolean apply(PropertyState property) {
-            return !properties.containsKey(property.getName());
-        }
-    }
-
-    private class UnmodifiedChildNodePredicate implements Predicate<ChildNodeEntry>
{
-        @Override
-        public boolean apply(ChildNodeEntry entry) {
-            return !nodes.containsKey(entry.getName());
-        }
-    }
-
-    private static class UndeletedChildNodePredicate implements Predicate<Entry<?,
?>> {
-        @Override
-        public boolean apply(Entry<?, ?> entry) {
-            return entry.getValue() != null;
-        }
-    }
-
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/privilege/PrivilegeRegistry.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/privilege/PrivilegeRegistry.java?rev=1364113&r1=1364112&r2=1364113&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/privilege/PrivilegeRegistry.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/privilege/PrivilegeRegistry.java
Sat Jul 21 15:20:04 2012
@@ -26,11 +26,12 @@ import javax.annotation.Nonnull;
 import javax.jcr.RepositoryException;
 
 import org.apache.jackrabbit.oak.api.ContentSession;
-import org.apache.jackrabbit.oak.util.ArrayUtils;
 import org.apache.jackrabbit.oak.util.TODO;
 import org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeDefinition;
 import org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeProvider;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * PrivilegeProviderImpl... TODO
  */
@@ -130,7 +131,7 @@ public class PrivilegeRegistry implement
 
         private PrivilegeDefinitionImpl(String name, boolean isAbstract,
                                         String... declaredAggregateNames) {
-            this(name, isAbstract, ArrayUtils.toSet(declaredAggregateNames));
+            this(name, isAbstract, ImmutableSet.copyOf(declaredAggregateNames));
         }
 
         //--------------------------------------------< PrivilegeDefinition >---

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/ArrayUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/ArrayUtils.java?rev=1364113&r1=1364112&r2=1364113&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/ArrayUtils.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/ArrayUtils.java
Sat Jul 21 15:20:04 2012
@@ -17,8 +17,6 @@
 package org.apache.jackrabbit.oak.util;
 
 import java.lang.reflect.Array;
-import java.util.HashSet;
-import java.util.Set;
 
 import javax.annotation.Nonnull;
 
@@ -207,31 +205,4 @@ public class ArrayUtils {
         }
     }
 
-        /**
-     * Check whether an array contains a given element
-     * @param array
-     * @param element
-     * @param <T>
-     * @return {@code true} iff {@code array} contains {@code element}.
-     */
-    public static <T> boolean contains(T[] array, T element) {
-        for (T t : array) {
-            if (element == null && t == null || element != null && element.equals(t))
{
-                return true;
-            }
-        }
-        return false;
-    }
-
-    /**
-     * Convert an array to a set.
-     * @param elements
-     * @param <T>
-     * @return
-     */
-    @Nonnull
-    public static <T> Set<T> toSet(T... elements) {
-        return new HashSet<T>(java.util.Arrays.asList(elements));
-    }
-
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java?rev=1364113&r1=1364112&r2=1364113&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
Sat Jul 21 15:20:04 2012
@@ -66,9 +66,9 @@ import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import com.google.common.collect.Sets;
+
 import static java.util.Arrays.asList;
-import static org.apache.jackrabbit.oak.util.ArrayUtils.contains;
-import static org.apache.jackrabbit.oak.util.ArrayUtils.toSet;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -1402,16 +1402,16 @@ public class RepositoryTest extends Abst
     public void testNamespaceRegistry() throws RepositoryException {
         NamespaceRegistry nsReg =
                 getSession().getWorkspace().getNamespaceRegistry();
-        assertFalse(contains(nsReg.getPrefixes(), "foo"));
-        assertFalse(contains(nsReg.getURIs(), "file:///foo"));
+        assertFalse(asList(nsReg.getPrefixes()).contains("foo"));
+        assertFalse(asList(nsReg.getURIs()).contains("file:///foo"));
 
         nsReg.registerNamespace("foo", "file:///foo");
-        assertTrue(contains(nsReg.getPrefixes(), "foo"));
-        assertTrue(contains(nsReg.getURIs(), "file:///foo"));
+        assertTrue(asList(nsReg.getPrefixes()).contains("foo"));
+        assertTrue(asList(nsReg.getURIs()).contains("file:///foo"));
 
         nsReg.unregisterNamespace("foo");
-        assertFalse(contains(nsReg.getPrefixes(), "foo"));
-        assertFalse(contains(nsReg.getURIs(), "file:///foo"));
+        assertFalse(asList(nsReg.getPrefixes()).contains("foo"));
+        assertFalse(asList(nsReg.getURIs()).contains("file:///foo"));
     }
 
     @Ignore // TODO implement node type support
@@ -1455,15 +1455,15 @@ public class RepositoryTest extends Abst
 
     @Test
     public void observation() throws RepositoryException, InterruptedException {
-        final Set<String> addNodes = toSet(
+        final Set<String> addNodes = Sets.newHashSet(
                 TEST_PATH + "/1",
                 TEST_PATH + "/2",
                 TEST_PATH + "/3");
 
-        final Set<String> removeNodes = toSet(
+        final Set<String> removeNodes = Sets.newHashSet(
                 TEST_PATH + "/2");
 
-        final Set<String> addProperties = toSet(
+        final Set<String> addProperties = Sets.newHashSet(
                 TEST_PATH + "/property",
                 TEST_PATH + "/prop0",
                 TEST_PATH + "/1/prop1",
@@ -1473,10 +1473,10 @@ public class RepositoryTest extends Abst
                 TEST_PATH + "/3/jcr:primaryType",
                 TEST_PATH + "/3/prop3");
 
-        final Set<String> setProperties = toSet(
+        final Set<String> setProperties = Sets.newHashSet(
                 TEST_PATH + "/1/prop1");
 
-        final Set<String> removeProperties = toSet(
+        final Set<String> removeProperties = Sets.newHashSet(
                 TEST_PATH + "/1/prop2",
                 TEST_PATH + "/2/jcr:primaryType");
 
@@ -1564,18 +1564,18 @@ public class RepositoryTest extends Abst
 
     @Test
     public void observation2() throws RepositoryException, InterruptedException {
-        final Set<String> addNodes = toSet(
+        final Set<String> addNodes = Sets.newHashSet(
                 TEST_PATH + "/1",
                 TEST_PATH + "/2");
 
-        final Set<String> removeNodes = toSet(
+        final Set<String> removeNodes = Sets.newHashSet(
                 TEST_PATH + "/1");
 
-        final Set<String> addProperties = toSet(
+        final Set<String> addProperties = Sets.newHashSet(
                 TEST_PATH + "/1/jcr:primaryType",
                 TEST_PATH + "/2/jcr:primaryType");
 
-        final Set<String> removeProperties = toSet(
+        final Set<String> removeProperties = Sets.newHashSet(
                 TEST_PATH + "/1/jcr:primaryType");
 
         final List<Event> failedEvents = new ArrayList<Event>();



Mime
View raw message