BUG-1069: made InstanceIdentifier.PathArgument implement Comparable.
[yangtools.git] / yang / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / InstanceIdentifier.java
index be97e1fd47034df5e1adb80859b5fa9ca9e38466..b135a86234bdf8136f393cd7eb5b62f4e48e23ee 100644 (file)
@@ -7,20 +7,18 @@
  */
 package org.opendaylight.yangtools.yang.binding;
 
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
-import org.opendaylight.yangtools.concepts.Builder;
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.concepts.Path;
-
 import com.google.common.base.Objects;
 import com.google.common.base.Objects.ToStringHelper;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.concepts.Path;
 
 /**
  *
@@ -492,7 +490,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * Interface which implementations are used as path components of the
      * path in overall data tree.
      */
-    public interface PathArgument {
+    public interface PathArgument extends Comparable<PathArgument> {
         Class<? extends DataObject> getType();
     }
 
@@ -510,10 +508,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
 
         @Override
         public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((type == null) ? 0 : type.hashCode());
-            return result;
+            return type.hashCode();
         }
 
         @Override
@@ -528,14 +523,12 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
                 return false;
             }
             final AbstractPathArgument<?> other = (AbstractPathArgument<?>) obj;
-            if (type == null) {
-                if (other.type != null) {
-                    return false;
-                }
-            } else if (!type.equals(other.type)) {
-                return false;
-            }
-            return true;
+            return type.equals(other.type);
+        }
+
+        @Override
+        public int compareTo(PathArgument arg) {
+            return type.getCanonicalName().compareTo(arg.getType().getCanonicalName());
         }
     }
 
@@ -577,12 +570,12 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
 
         @Override
         public boolean equals(final Object obj) {
-            return super.equals(obj) && obj.hashCode() == hashCode() && key.equals(((IdentifiableItem<?, ?>) obj).getKey());
+            return super.equals(obj) && key.equals(((IdentifiableItem<?, ?>) obj).getKey());
         }
 
         @Override
         public int hashCode() {
-            return key.hashCode();
+            return super.hashCode() * 31 + key.hashCode();
         }
 
         @Override