jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1342199 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: plugins/memory/MemoryChildNodeEntry.java plugins/memory/MemoryNodeState.java plugins/memory/ModifiedNodeState.java util/Predicates.java
Date Thu, 24 May 2012 11:30:43 GMT
Author: mduerig
Date: Thu May 24 11:30:43 2012
New Revision: 1342199

URL: http://svn.apache.org/viewvc?rev=1342199&view=rev
Log:
OAK-112: Refactor ModifiedNodeState and related classes to use type safe iterator utilities

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/Predicates.java
Modified:
    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

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=1342199&r1=1342198&r2=1342199&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
Thu May 24 11:30:43 2012
@@ -21,6 +21,8 @@ package org.apache.jackrabbit.oak.plugin
 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 org.apache.jackrabbit.oak.util.Function1;
+import org.apache.jackrabbit.oak.util.Iterators;
 
 import java.util.Iterator;
 import java.util.Map;
@@ -31,55 +33,17 @@ import java.util.Map.Entry;
  */
 public class MemoryChildNodeEntry extends AbstractChildNodeEntry {
 
-    public static Iterable<ChildNodeEntry> iterable(
-            final Map<String, NodeState> map) {
-        return new Iterable<ChildNodeEntry>() {
-            @Override
-            public Iterator<ChildNodeEntry> iterator() {
-                return MemoryChildNodeEntry.iterator(map);
-            }
-        };
-    }
-
-    public static Iterator<ChildNodeEntry> iterator(
-            Map<String, NodeState> map) {
-        final Iterator<Map.Entry<String, NodeState>> iterator =
-                map.entrySet().iterator();
-        return new Iterator<ChildNodeEntry>() {
-            @Override
-            public boolean hasNext() {
-                return iterator.hasNext();
-            }
-            @Override
-            public ChildNodeEntry next() {
-                return new MemoryChildNodeEntry(iterator.next());
-            }
-            @Override
-            public void remove() {
-                throw new UnsupportedOperationException();
-            }
-        };
-    }
-
     public static Iterator<ChildNodeEntry> iterator(
             final Iterator<Entry<String, NodeState>> iterator) {
 
-        return new Iterator<ChildNodeEntry>() {
-            @Override
-            public boolean hasNext() {
-                return iterator.hasNext();
-            }
-
-            @Override
-            public ChildNodeEntry next() {
-                return new MemoryChildNodeEntry(iterator.next());
-            }
+        return Iterators.map(iterator,
 
+                new Function1<Entry<String, NodeState>, ChildNodeEntry>() {
             @Override
-            public void remove() {
-                throw new UnsupportedOperationException();
+            public ChildNodeEntry apply(Entry<String, NodeState> entry) {
+                return new MemoryChildNodeEntry(entry);
             }
-        };
+        });
     }
 
     private final String name;

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=1342199&r1=1342198&r2=1342199&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
Thu May 24 11:30:43 2012
@@ -16,14 +16,15 @@
  */
 package org.apache.jackrabbit.oak.plugins.memory;
 
-import java.util.Collections;
-import java.util.Map;
-
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.spi.state.AbstractNodeState;
 import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
+
 /**
  * Basic in-memory node state implementation.
  */
@@ -85,7 +86,12 @@ public class MemoryNodeState extends Abs
 
     @Override
     public Iterable<ChildNodeEntry> getChildNodeEntries() {
-        return MemoryChildNodeEntry.iterable(nodes);
+        return new Iterable<ChildNodeEntry>() {
+            @Override
+            public Iterator<ChildNodeEntry> iterator() {
+                return MemoryChildNodeEntry.iterator(nodes.entrySet().iterator());
+            }
+        };
     }
 
 }

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=1342199&r1=1342198&r2=1342199&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
Thu May 24 11:30:43 2012
@@ -16,14 +16,14 @@
  */
 package org.apache.jackrabbit.oak.plugins.memory;
 
-import org.apache.commons.collections.IteratorUtils;
-import org.apache.commons.collections.Predicate;
-import org.apache.commons.collections.PredicateUtils;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.spi.state.AbstractNodeState;
 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.Iterators;
+import org.apache.jackrabbit.oak.util.Predicate;
+import org.apache.jackrabbit.oak.util.Predicates;
 
 import java.util.Iterator;
 import java.util.Map;
@@ -112,20 +112,19 @@ 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();
+        final Iterable<? extends PropertyState> unmodified = base.getProperties();
+        final Iterable<? extends PropertyState> modified = properties.values();
+
         return new Iterable<PropertyState>() {
-            @Override @SuppressWarnings("unchecked")
+            @Override
             public Iterator<PropertyState> iterator() {
-                Iterator<PropertyState> a = IteratorUtils.filteredIterator(
-                        unmodified.iterator(),
-                        new UnmodifiedPropertyPredicate());
-                Iterator<PropertyState> b = IteratorUtils.filteredIterator(
-                        modified.iterator(),
-                        PredicateUtils.notNullPredicate());
-                return IteratorUtils.chainedIterator(a, b);
+                Iterator<PropertyState> a = Iterators.filter(
+                        unmodified.iterator(), new UnmodifiedPropertyPredicate());
+
+                Iterator<PropertyState> b = Iterators.filter(
+                        modified.iterator(), Predicates.nonNull());
+
+                return Iterators.chain(a, b);
             }
         };
     }
@@ -161,63 +160,42 @@ public class ModifiedNodeState extends A
         if (nodes.isEmpty()) {
             return base.getChildNodeEntries(); // shortcut
         }
-        final Iterable<? extends ChildNodeEntry> unmodified =
-                base.getChildNodeEntries();
+        final Iterable<? extends ChildNodeEntry> unmodified = base.getChildNodeEntries();
         final Iterator<Entry<String, NodeState>> modified = nodes.entrySet().iterator();
+
         return new Iterable<ChildNodeEntry>() {
-            @Override @SuppressWarnings("unchecked")
+            @Override
             public Iterator<ChildNodeEntry> iterator() {
-                Iterator<ChildNodeEntry> a = IteratorUtils.filteredIterator(
-                        unmodified.iterator(),
-                        new UnmodifiedChildNodePredicate());
-                Iterator<Entry<String, NodeState>> b = IteratorUtils.filteredIterator(
-                        modified,
-                        new UndeletedChildNodePredicate());
-                return IteratorUtils.chainedIterator(a, MemoryChildNodeEntry.iterator(b));
+                Iterator<ChildNodeEntry> a = Iterators.filter(
+                        unmodified.iterator(), new UnmodifiedChildNodePredicate());
+
+                Iterator<Entry<String, NodeState>> b = Iterators.filter(
+                        modified, new UndeletedChildNodePredicate());
+
+                return Iterators.chain(a, MemoryChildNodeEntry.iterator(b));
             }
         };
     }
 
-    private class UnmodifiedPropertyPredicate implements Predicate {
-
+    private class UnmodifiedPropertyPredicate implements Predicate<PropertyState> {
         @Override
-        public boolean evaluate(Object object) {
-            if (object instanceof PropertyState) {
-                PropertyState property = ((PropertyState) object);
-                return !properties.containsKey(property.getName());
-            } else {
-                return false;
-            }
+        public boolean evaluate(PropertyState property) {
+            return !properties.containsKey(property.getName());
         }
-
     }
 
-    private class UnmodifiedChildNodePredicate implements Predicate {
-
+    private class UnmodifiedChildNodePredicate implements Predicate<ChildNodeEntry>
{
         @Override
-        public boolean evaluate(Object object) {
-            if (object instanceof ChildNodeEntry) {
-                ChildNodeEntry entry = ((ChildNodeEntry) object);
-                return !nodes.containsKey(entry.getName());
-            } else {
-                return false;
-            }
+        public boolean evaluate(ChildNodeEntry entry) {
+            return !nodes.containsKey(entry.getName());
         }
-
     }
 
-    private static class UndeletedChildNodePredicate implements Predicate {
-
+    private static class UndeletedChildNodePredicate implements Predicate<Entry<?,
?>> {
         @Override
-        public boolean evaluate(Object object) {
-            if (object instanceof Entry) {
-                Entry<?, ?> entry = ((Entry<?, ?>) object);
-                return entry.getValue() != null;
-            } else {
-                return false;
-            }
+        public boolean evaluate(Entry<?, ?> entry) {
+            return entry.getValue() != null;
         }
-
     }
 
 }

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/Predicates.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/Predicates.java?rev=1342199&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/Predicates.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/Predicates.java
Thu May 24 11:30:43 2012
@@ -0,0 +1,22 @@
+package org.apache.jackrabbit.oak.util;
+
+/**
+ * Utility class containing type safe counterparts for some of the predicates of
+ * commons-collections.
+ */
+public class Predicates {
+    private static final Predicate<?> NOT_NULL = new Predicate<Object>() {
+        @Override
+        public boolean evaluate(Object arg) {
+            return arg != null;
+        }
+    };
+
+    private Predicates() {
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <T> Predicate<T> nonNull() {
+        return (Predicate<T>) NOT_NULL;
+    }
+}



Mime
View raw message