Consider AugmentationNodes when direct descendant is not found
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / QNameWithPredicateImpl.java
index 1661d9bb98b5a1a0c8b376dc0ee4cd6abecf7da4..d0fb0193d1400a6ec92e29e8e3fb202ca4c93a8c 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,30 +7,33 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.leafref;
 
+import com.google.common.collect.ImmutableList;
 import java.io.Serializable;
-import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.Revision;
 
-final class QNameWithPredicateImpl implements Immutable, Serializable,
-        QNameWithPredicate {
+final class QNameWithPredicateImpl implements Immutable, Serializable, QNameWithPredicate {
 
     private static final long serialVersionUID = 1L;
 
-    private final LinkedList<QNamePredicate> qnamePredicates;
+    private final List<QNamePredicate> qnamePredicates;
     private final QNameModule moduleQname;
     private final String localName;
 
-    public QNameWithPredicateImpl(final QNameModule moduleQname, final String localName,
-            final LinkedList<QNamePredicate> qnamePredicates) {
+    QNameWithPredicateImpl(final QNameModule moduleQname, final String localName,
+            final List<QNamePredicate> qnamePredicates) {
         this.moduleQname = moduleQname;
         this.localName = localName;
-        this.qnamePredicates = qnamePredicates;
+        this.qnamePredicates = ImmutableList.copyOf(qnamePredicates);
     }
 
     @Override
-    public LinkedList<QNamePredicate> getQNamePredicates() {
+    public List<QNamePredicate> getQNamePredicates() {
         return qnamePredicates;
     }
 
@@ -59,14 +62,14 @@ final class QNameWithPredicateImpl implements Immutable, Serializable,
             return false;
         }
         final QNameWithPredicateImpl other = (QNameWithPredicateImpl) obj;
-        if (localName == null) {
-            if (other.localName != null) {
-                return false;
-            }
-        } else if (!localName.equals(other.localName)) {
-            return false;
-        }
-        return moduleQname.equals(other.moduleQname);
+        return Objects.equals(localName, other.localName) && moduleQname.equals(other.moduleQname);
+    }
+
+    @Override
+    public int hashCode() {
+        int result = moduleQname != null ? moduleQname.hashCode() : 0;
+        result = 31 * result + (localName != null ? localName.hashCode() : 0);
+        return result;
     }
 
     @Override
@@ -74,9 +77,12 @@ final class QNameWithPredicateImpl implements Immutable, Serializable,
         final StringBuilder sb = new StringBuilder();
 
         if (moduleQname != null) {
-            sb.append("(" + moduleQname.getNamespace());
-            sb.append("?revision=" + moduleQname.getRevision());
-            sb.append(")");
+            sb.append('(').append(moduleQname.getNamespace());
+            final Optional<Revision> rev = moduleQname.getRevision();
+            if (rev.isPresent()) {
+                sb.append("?revision=").append(rev.get());
+            }
+            sb.append(')');
         }
 
         sb.append(localName);
@@ -87,5 +93,4 @@ final class QNameWithPredicateImpl implements Immutable, Serializable,
 
         return sb.toString();
     }
-
 }