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