Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / YangInstanceIdentifier.java
index 08d51f4b1849b1ede20e253d2ce40c2fa3741c17..a8a02c84189d730ed8a0b55661c286d9a91bcd12 100644 (file)
@@ -233,6 +233,13 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
      *         the specified parent is not in fact an ancestor of this object.
      */
     public Optional<YangInstanceIdentifier> relativeTo(final YangInstanceIdentifier ancestor) {
+        if (this == ancestor) {
+            return Optional.of(EMPTY);
+        }
+        if (ancestor.isEmpty()) {
+            return Optional.of(this);
+        }
+
         final Iterator<?> lit = getPathArguments().iterator();
         final Iterator<?> oit = ancestor.getPathArguments().iterator();
         int common = 0;
@@ -257,9 +264,12 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
     }
 
     @Override
-    public final boolean contains(final YangInstanceIdentifier other) {
-        Preconditions.checkArgument(other != null, "other should not be null");
+    public final boolean contains(@Nonnull final YangInstanceIdentifier other) {
+        if (this == other) {
+            return true;
+        }
 
+        Preconditions.checkArgument(other != null, "other should not be null");
         final Iterator<?> lit = getPathArguments().iterator();
         final Iterator<?> oit = other.getPathArguments().iterator();
 
@@ -419,7 +429,7 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
         }
 
         @Override
-        public int compareTo(final PathArgument o) {
+        public int compareTo(@Nonnull final PathArgument o) {
             return nodeType.compareTo(o.getNodeType());
         }
 
@@ -457,7 +467,7 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
         @Override
         public String toRelativeString(final PathArgument previous) {
             if (previous instanceof AbstractPathArgument) {
-                final QNameModule mod = ((AbstractPathArgument)previous).getNodeType().getModule();
+                final QNameModule mod = previous.getNodeType().getModule();
                 if (getNodeType().getModule().equals(mod)) {
                     return getNodeType().getLocalName();
                 }
@@ -476,7 +486,7 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
         private static final LoadingCache<QName, NodeIdentifier> CACHE = CacheBuilder.newBuilder().weakValues()
                 .build(new CacheLoader<QName, NodeIdentifier>() {
                     @Override
-                    public NodeIdentifier load(final QName key) {
+                    public NodeIdentifier load(@Nonnull final QName key) {
                         return new NodeIdentifier(key);
                     }
                 });
@@ -691,7 +701,7 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
         }
 
         @Override
-        public int compareTo(final PathArgument o) {
+        public int compareTo(@Nonnull final PathArgument o) {
             if (!(o instanceof AugmentationIdentifier)) {
                 return -1;
             }