You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2017/05/07 08:26:53 UTC

curator git commit: removed ZPath 'resolving'. It doesn't add much value and muddies up the code

Repository: curator
Updated Branches:
  refs/heads/CURATOR-397 e95b885eb -> c002e22e5


removed ZPath 'resolving'. It doesn't add much value and muddies up the code


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/c002e22e
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/c002e22e
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/c002e22e

Branch: refs/heads/CURATOR-397
Commit: c002e22e5a613cd8426c2e8b407ab06957908526
Parents: e95b885
Author: randgalt <ra...@apache.org>
Authored: Sun May 7 10:26:48 2017 +0200
Committer: randgalt <ra...@apache.org>
Committed: Sun May 7 10:26:48 2017 +0200

----------------------------------------------------------------------
 .../apache/curator/x/async/modeled/ZPath.java   | 22 --------
 .../x/async/modeled/details/ZPathImpl.java      | 57 ++++++++++----------
 .../curator/x/async/modeled/TestZPath.java      | 13 -----
 3 files changed, 28 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/c002e22e/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ZPath.java
----------------------------------------------------------------------
diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ZPath.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ZPath.java
index 27811f1..fae4e3a 100644
--- a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ZPath.java
+++ b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ZPath.java
@@ -21,7 +21,6 @@ package org.apache.curator.x.async.modeled;
 import org.apache.curator.x.async.modeled.details.ZPathImpl;
 import java.util.Arrays;
 import java.util.List;
-import java.util.function.Supplier;
 import java.util.function.UnaryOperator;
 import java.util.regex.Pattern;
 
@@ -196,27 +195,6 @@ public interface ZPath extends Resolvable
 
     /**
      * <p>
-     *     An "auto" resolving version of this ZPath. i.e. if any of the path names is
-     *     the {@link #parameterNodeName} the ZPath must be resolved. This method
-     *     creates a new ZPath that auto resolves by using the given parameter suppliers
-     *     whenever needed.
-     * </p>
-     *
-     * <p>
-     *     The replacement is the <code>toString()</code> value of the parameter object or,
-     *     if the object implements {@link org.apache.curator.x.async.modeled.NodeName},
-     *     the value of <code>nodeName()</code>.
-     * </p>
-     *
-     * @param parameterSuppliers parameter suppliers
-     * @return new auto resolving ZNode
-     * @see #resolved(Object...)
-     * @see #parameterNodeName
-     */
-    ZPath resolving(List<Supplier<Object>> parameterSuppliers);
-
-    /**
-     * <p>
      *     Return a ZPath that represents a child ZNode of this ZPath. e.g.
      *     <code>ZPath.from("a", "b").at("c")</code> represents the path "/a/b/c"
      * </p>

http://git-wip-us.apache.org/repos/asf/curator/blob/c002e22e/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ZPathImpl.java
----------------------------------------------------------------------
diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ZPathImpl.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ZPathImpl.java
index 40d23a3..ce47a60 100644
--- a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ZPathImpl.java
+++ b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ZPathImpl.java
@@ -30,7 +30,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Objects;
-import java.util.function.Supplier;
 import java.util.function.UnaryOperator;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -39,11 +38,12 @@ import static org.apache.curator.utils.ZKPaths.PATH_SEPARATOR;
 
 public class ZPathImpl implements ZPath
 {
-    public static final ZPath root = new ZPathImpl(Collections.singletonList(PATH_SEPARATOR), null, null);
+    public static final ZPath root = new ZPathImpl(Collections.singletonList(PATH_SEPARATOR), null);
 
     private final List<String> nodes;
     private final boolean isResolved;
-    private final List<Supplier<Object>> parameterSuppliers;
+    private volatile String fullPathCache = null;
+    private volatile  String parentPathCache = null;
 
     public static ZPath parse(String fullPath, UnaryOperator<String> nameFilter)
     {
@@ -64,7 +64,7 @@ public class ZPathImpl implements ZPath
              )
             .build();
         nodes.forEach(ZPathImpl::validate);
-        return new ZPathImpl(nodes, null, null);
+        return new ZPathImpl(nodes, null);
     }
 
     public static ZPath from(String[] names)
@@ -103,20 +103,20 @@ public class ZPathImpl implements ZPath
             builder.add(PATH_SEPARATOR);
         }
         List<String> nodes = builder.addAll(names).build();
-        return new ZPathImpl(nodes, null, null);
+        return new ZPathImpl(nodes, null);
     }
 
     @Override
     public ZPath at(Object child)
     {
-        return new ZPathImpl(nodes, NodeName.nameFrom(child), parameterSuppliers);
+        return new ZPathImpl(nodes, NodeName.nameFrom(child));
     }
 
     @Override
     public ZPath parent()
     {
         checkRootAccess();
-        return new ZPathImpl(nodes.subList(0, nodes.size() - 1), null, parameterSuppliers);
+        return new ZPathImpl(nodes.subList(0, nodes.size() - 1), null);
     }
 
     @Override
@@ -215,19 +215,11 @@ public class ZPathImpl implements ZPath
                 return name;
             })
             .collect(Collectors.toList());
-        return new ZPathImpl(nodeNames, null, parameterSuppliers);
+        return new ZPathImpl(nodeNames, null);
     }
 
-    @Override
-    public ZPath resolving(List<Supplier<Object>> parameterSuppliers)
+    private ZPathImpl(List<String> nodes, String child)
     {
-        parameterSuppliers = Objects.requireNonNull(parameterSuppliers, "parameterSuppliers cannot be null");
-        return new ZPathImpl(nodes, null, parameterSuppliers);
-    }
-
-    private ZPathImpl(List<String> nodes, String child, List<Supplier<Object>> parameterSuppliers)
-    {
-        this.parameterSuppliers = parameterSuppliers;
         ImmutableList.Builder<String> builder = ImmutableList.<String>builder().addAll(nodes);
         if ( child != null )
         {
@@ -235,7 +227,7 @@ public class ZPathImpl implements ZPath
             builder.add(child);
         }
         this.nodes = builder.build();
-        isResolved = (parameterSuppliers != null) || !this.nodes.contains(parameterNodeName);
+        isResolved = !this.nodes.contains(parameterNodeName);
     }
 
     private void checkRootAccess()
@@ -253,6 +245,12 @@ public class ZPathImpl implements ZPath
 
     private String buildFullPath(boolean parent)
     {
+        String cachedValue = parent ? parentPathCache : fullPathCache;
+        if ( cachedValue != null )
+        {
+            return cachedValue;
+        }
+
         boolean addSeparator = false;
         StringBuilder str = new StringBuilder();
         int size = parent ? (nodes.size() - 1) : nodes.size();
@@ -263,18 +261,19 @@ public class ZPathImpl implements ZPath
             {
                 str.append(PATH_SEPARATOR);
             }
-            String value = nodes.get(i);
-            if ( value.equals(parameterNodeName) )
-            {
-                if ( (parameterSuppliers == null) || (parameterSuppliers.size() <= parameterIndex) )
-                {
-                    throw new IllegalStateException(String.format("Parameter supplier missing at index [%d] for [%s]", parameterIndex, nodes.toString()));
-                }
-                value = NodeName.nameFrom(parameterSuppliers.get(parameterIndex++).get());
-            }
-            str.append(value);
+            str.append(nodes.get(i));
+        }
+
+        String value = str.toString();
+        if ( parent )
+        {
+            parentPathCache = value;
+        }
+        else
+        {
+            fullPathCache = value;
         }
-        return str.toString();
+        return value;
     }
 
     private static void validate(String nodeName)

http://git-wip-us.apache.org/repos/asf/curator/blob/c002e22e/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestZPath.java
----------------------------------------------------------------------
diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestZPath.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestZPath.java
index 4df9ed1..1e98e9f 100644
--- a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestZPath.java
+++ b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestZPath.java
@@ -22,8 +22,6 @@ import org.apache.curator.utils.ZKPaths;
 import org.apache.curator.x.async.modeled.details.ZPathImpl;
 import org.testng.Assert;
 import org.testng.annotations.Test;
-import java.util.Arrays;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import static org.apache.curator.x.async.modeled.ZPath.parameterNodeName;
 
@@ -76,15 +74,4 @@ public class TestZPath
         ZPath path = ZPath.from("one", parameterNodeName, "two", parameterNodeName);
         Assert.assertEquals(path.resolved("a", "b"), ZPath.from("one", "a", "two", "b"));
     }
-
-    @Test
-    public void testResolving()
-    {
-        ZPath path = ZPath.from("one", parameterNodeName, "two", parameterNodeName);
-        AtomicInteger count = new AtomicInteger(0);
-        ZPath resolving = path.resolving(Arrays.asList(() -> "x" + count.get(), () -> "y" + count.get()));
-        Assert.assertEquals(resolving.fullPath(), "/one/x0/two/y0");
-        count.incrementAndGet();
-        Assert.assertEquals(resolving.fullPath(), "/one/x1/two/y1");
-    }
 }