You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2012/08/24 13:29:57 UTC

svn commit: r1376888 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: ItemDelegate.java NodeDelegate.java PropertyDelegate.java

Author: mduerig
Date: Fri Aug 24 11:29:56 2012
New Revision: 1376888

URL: http://svn.apache.org/viewvc?rev=1376888&view=rev
Log:
OAK-275 Introduce TreeLocation interface
refactor: pull common code up to ItemDelegate

Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java?rev=1376888&r1=1376887&r2=1376888&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemDelegate.java Fri Aug 24 11:29:56 2012
@@ -17,12 +17,14 @@
 
 package org.apache.jackrabbit.oak.jcr;
 
-import org.apache.jackrabbit.oak.api.Tree.Status;
-
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.jcr.InvalidItemStateException;
 
+import org.apache.jackrabbit.oak.api.Tree.Status;
+import org.apache.jackrabbit.oak.api.TreeLocation;
+import org.apache.jackrabbit.oak.commons.PathUtils;
+
 /**
  * Abstract base class for {@link NodeDelegate} and {@link PropertyDelegate}
  */
@@ -30,10 +32,15 @@ public abstract class ItemDelegate {
 
     protected final SessionDelegate sessionDelegate;
 
-    protected ItemDelegate(SessionDelegate sessionDelegate) {
+    /** The underlying {@link org.apache.jackrabbit.oak.api.TreeLocation} of this item. */
+    protected TreeLocation location;
+
+    protected ItemDelegate(SessionDelegate sessionDelegate, TreeLocation location) {
         assert sessionDelegate != null;
+        assert location != null;
 
         this.sessionDelegate = sessionDelegate;
+        this.location = location;
     }
 
     /**
@@ -41,14 +48,19 @@ public abstract class ItemDelegate {
      * @return oak name of this item
      */
     @Nonnull
-    public abstract String getName() throws InvalidItemStateException;
+    public String getName() throws InvalidItemStateException {
+        return PathUtils.getName(getPath());
+    }
 
     /**
      * Get the path of this item
      * @return oak path of this item
      */
     @Nonnull
-    public abstract String getPath() throws InvalidItemStateException;
+    public String getPath() throws InvalidItemStateException {
+        checkStale();
+        return location.getPath();
+    }
 
     /**
      * Get the parent of this item or {@code null}.
@@ -56,27 +68,55 @@ public abstract class ItemDelegate {
      * is not accessible.
      */
     @CheckForNull
-    public abstract NodeDelegate getParent() throws InvalidItemStateException;
+    public NodeDelegate getParent() throws InvalidItemStateException {
+        checkStale();
+        return NodeDelegate.create(sessionDelegate, location.getParent());
+    }
 
     /**
      * Determine whether this item is stale
      * @return  {@code true} iff stale
      */
-    public abstract boolean isStale();
+    public boolean isStale() {
+        if (location.getStatus() == Status.REMOVED) {
+            return true;
+        }
+        else {
+            resolve();
+            return location.getStatus() == Status.REMOVED;
+        }
+    }
+
+    public void checkStale() throws InvalidItemStateException {
+        if (isStale()) {
+            throw new InvalidItemStateException("Item is stale");
+        }
+    }
 
     /**
      * Get the status of this item
      * @return  {@link Status} of this item
      */
     @Nonnull
-    public abstract Status getStatus() throws InvalidItemStateException;
+    public Status getStatus() throws InvalidItemStateException {
+        checkStale();
+        return location.getStatus();
+    }
 
     /**
      * Get the session delegate with which this item is associated
      * @return  {@link SessionDelegate} to which this item belongs
      */
     @Nonnull
-    public SessionDelegate getSessionDelegate() {
+    public final SessionDelegate getSessionDelegate() {
         return sessionDelegate;
     }
+
+    @Override
+    public String toString() {
+        // don't disturb the state: avoid resolving the tree
+        return getClass().getSimpleName() + '[' + location.getPath() + ']';
+    }
+
+    protected abstract void resolve();
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java?rev=1376888&r1=1376887&r2=1376888&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeDelegate.java Fri Aug 24 11:29:56 2012
@@ -38,7 +38,6 @@ import org.apache.jackrabbit.oak.api.Cor
 import org.apache.jackrabbit.oak.api.CoreValueFactory;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
-import org.apache.jackrabbit.oak.api.Tree.Status;
 import org.apache.jackrabbit.oak.api.TreeLocation;
 
 /**
@@ -49,9 +48,6 @@ import org.apache.jackrabbit.oak.api.Tre
  */
 public class NodeDelegate extends ItemDelegate {
 
-    /** The underlying {@link TreeLocation} of this node. */
-    private TreeLocation location;
-
     /**
      * Create a new {@code NodeDelegate} instance for a valid {@code TreeLocation}. That
      * is for one where {@code getTree() != null}.
@@ -66,45 +62,11 @@ public class NodeDelegate extends ItemDe
     }
 
     NodeDelegate(SessionDelegate sessionDelegate, Tree tree) {
-        this(sessionDelegate, tree.getLocation());
+        super(sessionDelegate, tree.getLocation());
     }
 
     private NodeDelegate(SessionDelegate sessionDelegate, TreeLocation location) {
-        super(sessionDelegate);
-        assert location != null;
-        this.location = location;
-    }
-
-    @Override
-    public String getName() throws InvalidItemStateException {
-        return getTree().getName();
-    }
-
-    @Override
-    public String getPath() throws InvalidItemStateException {
-        return getTree().getPath();
-    }
-
-    @Override
-    public NodeDelegate getParent() throws InvalidItemStateException {
-        TreeLocation parentLocation = getLocation().getParent();
-        return create(sessionDelegate, parentLocation);
-    }
-
-    @Override
-    public boolean isStale() {
-        return location.getStatus() == Status.REMOVED;
-    }
-
-    @Override
-    public Status getStatus() throws InvalidItemStateException {
-        return getTree().getStatus();
-    }
-
-    @Override
-    public String toString() {
-        // don't disturb the state: avoid resolving the tree
-        return "NodeDelegate[" + location.getPath() + ']';
+        super(sessionDelegate, location);
     }
 
     @Nonnull
@@ -362,7 +324,8 @@ public class NodeDelegate extends ItemDe
         return location;
     }
 
-    private synchronized void resolve() {
+    @Override
+    protected synchronized void resolve() {
         String path = location.getPath();
         if (path != null) {
             Tree tree = sessionDelegate.getTree(path);

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java?rev=1376888&r1=1376887&r2=1376888&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyDelegate.java Fri Aug 24 11:29:56 2012
@@ -27,7 +27,6 @@ import javax.jcr.nodetype.PropertyDefini
 import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
-import org.apache.jackrabbit.oak.api.Tree.Status;
 import org.apache.jackrabbit.oak.api.TreeLocation;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 
@@ -39,56 +38,8 @@ import org.apache.jackrabbit.oak.commons
  */
 public class PropertyDelegate extends ItemDelegate {
 
-    /** The underlying {@link TreeLocation} of this node. */
-    private TreeLocation location;
-
     PropertyDelegate(SessionDelegate sessionDelegate, TreeLocation location) {
-        super(sessionDelegate);
-        assert location != null;
-        this.location = location;
-    }
-
-    @Override
-    public String getName() throws InvalidItemStateException {
-        return getPropertyState().getName();
-    }
-
-    @Override
-    public String getPath() throws InvalidItemStateException {
-        resolve();
-        String path = location.getPath();
-        if (path == null) {
-            throw new InvalidItemStateException("Property is stale");
-        }
-        return path;
-    }
-
-    @Override
-    public NodeDelegate getParent() throws InvalidItemStateException {
-        return NodeDelegate.create(sessionDelegate, location.getParent());
-    }
-
-    @Override
-    public boolean isStale() {
-        resolve();
-        return location.getStatus() == Status.REMOVED;
-    }
-
-    @Override
-    public Status getStatus() throws InvalidItemStateException {
-        resolve();
-        Status propertyStatus = location.getStatus();
-        if (propertyStatus == null) {
-            throw new InvalidItemStateException("Property is stale");
-        }
-
-        return propertyStatus;
-    }
-
-    @Override
-    public String toString() {
-        // don't disturb the state: avoid resolving the tree
-        return "PropertyDelegate[" + location.getPath() + ']';
+        super(sessionDelegate, location);
     }
 
     /**
@@ -263,7 +214,8 @@ public class PropertyDelegate extends It
         return tree;
     }
 
-    synchronized void resolve() {
+    @Override
+    protected synchronized void resolve() {
         String path = location.getPath();
         if (path != null) {
             Tree parent = sessionDelegate.getTree(PathUtils.getParentPath(path));