d98fbe3c16e97c699e5551b4113576f6fdb60842
[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.List;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16
17 final class QNameWithPredicateImpl implements Immutable, Serializable,
18         QNameWithPredicate {
19
20     private static final long serialVersionUID = 1L;
21
22     private final List<QNamePredicate> qnamePredicates;
23     private final QNameModule moduleQname;
24     private final String localName;
25
26     public QNameWithPredicateImpl(final QNameModule moduleQname, final String localName,
27             final List<QNamePredicate> qnamePredicates) {
28         this.moduleQname = moduleQname;
29         this.localName = localName;
30         this.qnamePredicates = qnamePredicates;
31     }
32
33     @Override
34     public List<QNamePredicate> getQNamePredicates() {
35         return qnamePredicates;
36     }
37
38     @Override
39     public QNameModule getModuleQname() {
40         return moduleQname;
41     }
42
43     @Override
44     public String getLocalName() {
45         return localName;
46     }
47
48     @Override
49     public QName getQName() {
50         return QName.create(moduleQname, localName);
51     }
52
53     // FIXME: check also predicates ...
54     @Override
55     public boolean equals(final Object obj) {
56         if (this == obj) {
57             return true;
58         }
59         if (!(obj instanceof QNameWithPredicateImpl)) {
60             return false;
61         }
62         final QNameWithPredicateImpl other = (QNameWithPredicateImpl) obj;
63         return Objects.equals(localName, other.localName) && moduleQname.equals(other.moduleQname);
64     }
65
66     @Override
67     public int hashCode() {
68         int result = moduleQname != null ? moduleQname.hashCode() : 0;
69         result = 31 * result + (localName != null ? localName.hashCode() : 0);
70         return result;
71     }
72
73     @Override
74     public String toString() {
75         final StringBuilder sb = new StringBuilder();
76
77         if (moduleQname != null) {
78             sb.append('(').append(moduleQname.getNamespace());
79             sb.append("?revision=").append(moduleQname.getRevision());
80             sb.append(')');
81         }
82
83         sb.append(localName);
84
85         for (final QNamePredicate predicate : qnamePredicates) {
86             sb.append(predicate);
87         }
88
89         return sb.toString();
90     }
91
92 }