You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by ja...@apache.org on 2014/05/10 02:14:20 UTC

[3/6] git commit: DRILL-655: Miscellaneous fixes for SchemaPath

DRILL-655: Miscellaneous fixes for SchemaPath

 - Fixed equals() and hashCode().
 - Made PathSegment immutable outside of package.
 - Made ArraySegment and NameSegment final.


Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/42763b65
Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/42763b65
Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/42763b65

Branch: refs/heads/master
Commit: 42763b650e7abbc8c8974fe5a7486fa32c5fb192
Parents: 16dc94f
Author: Aditya Kishore <ad...@maprtech.com>
Authored: Wed May 7 07:37:27 2014 -0700
Committer: Jacques Nadeau <ja...@apache.org>
Committed: Fri May 9 16:48:48 2014 -0700

----------------------------------------------------------------------
 .../drill/common/expression/PathSegment.java    | 88 ++++++++++++++------
 .../drill/common/expression/SchemaPath.java     | 19 ++---
 2 files changed, 69 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/42763b65/common/src/main/java/org/apache/drill/common/expression/PathSegment.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/drill/common/expression/PathSegment.java b/common/src/main/java/org/apache/drill/common/expression/PathSegment.java
index 448e50f..ffbe493 100644
--- a/common/src/main/java/org/apache/drill/common/expression/PathSegment.java
+++ b/common/src/main/java/org/apache/drill/common/expression/PathSegment.java
@@ -17,17 +17,16 @@
  */
 package org.apache.drill.common.expression;
 
-
-
-
 public abstract class PathSegment{
 
-  protected PathSegment child;
+  PathSegment child;
+
+  int hash;
 
   public abstract PathSegment cloneWithNewChild(PathSegment segment);
   public abstract PathSegment clone();
 
-  public static class ArraySegment extends PathSegment{
+  public static final class ArraySegment extends PathSegment {
     private final int index;
 
     public ArraySegment(String numberAsText, PathSegment child){
@@ -68,6 +67,22 @@ public abstract class PathSegment{
     }
 
     @Override
+    public int segmentHashCode() {
+      return index;
+    }
+
+    @Override
+    public boolean segmentEquals(PathSegment obj) {
+      if (this == obj)
+        return true;
+      else if (obj == null)
+        return false;
+      else if (obj instanceof ArraySegment)
+        return index == ((ArraySegment)obj).getIndex();
+      return false;
+    }
+
+    @Override
     public PathSegment clone() {
       PathSegment seg = new ArraySegment(index);
       if(child != null) seg.setChild(child.clone());
@@ -86,8 +101,7 @@ public abstract class PathSegment{
   }
 
 
-
-  public static class NameSegment extends PathSegment{
+  public static final class NameSegment extends PathSegment {
     private final String path;
 
     public NameSegment(CharSequence n, PathSegment child){
@@ -122,28 +136,24 @@ public abstract class PathSegment{
     }
 
     @Override
-    public int hashCode() {
-      final int prime = 31;
-      int result = 1;
-      result = prime * result + ((path == null) ? 0 : path.toLowerCase().hashCode());
-      return result;
+    public int segmentHashCode() {
+      return ((path == null) ? 0 : path.toLowerCase().hashCode());
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean segmentEquals(PathSegment obj) {
       if (this == obj)
         return true;
-      if (obj == null)
+      else if (obj == null)
         return false;
-      if (getClass() != obj.getClass())
+      else if (getClass() != obj.getClass())
         return false;
+
       NameSegment other = (NameSegment) obj;
       if (path == null) {
-        if (other.path != null)
-          return false;
-      } else if (!path.equalsIgnoreCase(other.path))
-        return false;
-      return true;
+        return other.path == null;
+      }
+      return path.equalsIgnoreCase(other.path);
     }
 
     @Override
@@ -153,7 +163,6 @@ public abstract class PathSegment{
       return s;
     }
 
-
     @Override
     public NameSegment cloneWithNewChild(PathSegment newChild) {
       NameSegment s = new NameSegment(this.path);
@@ -165,19 +174,19 @@ public abstract class PathSegment{
       return s;
     }
 
-
   }
 
   public NameSegment getNameSegment(){
     throw new UnsupportedOperationException();
   }
+
   public ArraySegment getArraySegment(){
     throw new UnsupportedOperationException();
   }
+
   public abstract boolean isArray();
   public abstract boolean isNamed();
 
-
   public boolean isLastPath(){
     return child == null;
   }
@@ -186,10 +195,39 @@ public abstract class PathSegment{
     return child;
   }
 
-  public void setChild(PathSegment child) {
+  void setChild(PathSegment child) {
     this.child = child;
   }
 
+  protected abstract int segmentHashCode();
+  protected abstract boolean segmentEquals(PathSegment other);
 
+  @Override
+  public int hashCode() {
+    int h = hash;
+    if (h == 0) {
+      h = segmentHashCode();
+      h = 31*h + ((child == null) ? 0 : child.hashCode());
+      hash = h;
+    }
+    return h;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+
+    PathSegment other = (PathSegment) obj;
+    if (!segmentEquals(other)) {
+      return false;
+    } else if (child == null) {
+      return (other.child == null);
+    } else return child.equals(other.child);
+  }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/42763b65/common/src/main/java/org/apache/drill/common/expression/SchemaPath.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/drill/common/expression/SchemaPath.java b/common/src/main/java/org/apache/drill/common/expression/SchemaPath.java
index df96ffc..fd75aeb 100644
--- a/common/src/main/java/org/apache/drill/common/expression/SchemaPath.java
+++ b/common/src/main/java/org/apache/drill/common/expression/SchemaPath.java
@@ -46,7 +46,6 @@ public class SchemaPath extends LogicalExpressionBase {
 
   private final NameSegment rootSegment;
 
-
   public static SchemaPath getSimplePath(String name){
     return getCompoundPath(name);
   }
@@ -61,8 +60,6 @@ public class SchemaPath extends LogicalExpressionBase {
     return new SchemaPath(s);
   }
 
-
-
   /**
    *
    * @param simpleName
@@ -115,10 +112,7 @@ public class SchemaPath extends LogicalExpressionBase {
 
   @Override
   public int hashCode() {
-    final int prime = 31;
-    int result = 1;
-    result = prime * result + ((rootSegment == null) ? 0 : rootSegment.hashCode());
-    return result;
+    return ((rootSegment == null) ? 0 : rootSegment.hashCode());
   }
 
   @Override
@@ -127,15 +121,14 @@ public class SchemaPath extends LogicalExpressionBase {
       return true;
     if (obj == null)
       return false;
-    if ( !(obj instanceof SchemaPath))
+    if (!(obj instanceof SchemaPath))
       return false;
+
     SchemaPath other = (SchemaPath) obj;
     if (rootSegment == null) {
-      if (other.rootSegment != null)
-        return false;
-    } else if (!rootSegment.equals(other.rootSegment))
-      return false;
-    return true;
+      return (other.rootSegment == null);
+    }
+    return rootSegment.equals(other.rootSegment);
   }
 
   @Override