Merge "Bug 2363, Bug 2205. Beta version of LeafRefContext tree computation"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / QNameWithPredicateImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.leafref;
9
10 import java.io.Serializable;
11 import java.util.LinkedList;
12 import org.opendaylight.yangtools.concepts.Immutable;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15
16 final class QNameWithPredicateImpl implements Immutable, Serializable,
17         QNameWithPredicate {
18
19     private static final long serialVersionUID = 1L;
20
21     private final LinkedList<QNamePredicate> qnamePredicates;
22     private final QNameModule moduleQname;
23     private final String localName;
24
25     public QNameWithPredicateImpl(final QNameModule moduleQname, final String localName,
26             final LinkedList<QNamePredicate> qnamePredicates) {
27         this.moduleQname = moduleQname;
28         this.localName = localName;
29         this.qnamePredicates = qnamePredicates;
30     }
31
32     @Override
33     public LinkedList<QNamePredicate> getQNamePredicates() {
34         return qnamePredicates;
35     }
36
37     @Override
38     public QNameModule getModuleQname() {
39         return moduleQname;
40     }
41
42     @Override
43     public String getLocalName() {
44         return localName;
45     }
46
47     @Override
48     public QName getQName() {
49         return QName.create(moduleQname, localName);
50     }
51
52     // FIXME: check also predicates ...
53     @Override
54     public boolean equals(final Object obj) {
55         if (this == obj) {
56             return true;
57         }
58         if (!(obj instanceof QNameWithPredicateImpl)) {
59             return false;
60         }
61         final QNameWithPredicateImpl other = (QNameWithPredicateImpl) obj;
62         if (localName == null) {
63             if (other.localName != null) {
64                 return false;
65             }
66         } else if (!localName.equals(other.localName)) {
67             return false;
68         }
69         return moduleQname.equals(other.moduleQname);
70     }
71
72     @Override
73     public String toString() {
74         final StringBuilder sb = new StringBuilder();
75
76         if (moduleQname != null) {
77             sb.append("(" + moduleQname.getNamespace());
78             sb.append("?revision=" + moduleQname.getRevision());
79             sb.append(")");
80         }
81
82         sb.append(localName);
83
84         for (final QNamePredicate predicate : qnamePredicates) {
85             sb.append(predicate);
86         }
87
88         return sb.toString();
89     }
90
91 }