BUG-2304 Fix SchemaContextUtil for augmented nodes.
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / Leafref.java
1 /*
2  * Copyright (c) 2013 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.model.util;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
19
20 /**
21  * The <code>default</code> implementation of Instance Leafref Type Definition
22  * interface.
23  *
24  * @see LeafrefTypeDefinition
25  */
26 public final class Leafref implements LeafrefTypeDefinition {
27     private static final QName NAME = BaseTypes.constructQName("leafref");
28     private static final SchemaPath PATH = SchemaPath.create(true, NAME);
29     private static final String DESCRIPTION = "The leafref type is used to reference a particular leaf instance in the data tree.";
30     private static final String REF = "https://tools.ietf.org/html/rfc6020#section-9.9";
31
32     private final RevisionAwareXPath xpath;
33     private final SchemaPath path;
34
35     @Deprecated
36     public Leafref(final RevisionAwareXPath xpath) {
37         this(PATH,xpath);
38
39     }
40
41     private Leafref(final SchemaPath path, final RevisionAwareXPath target) {
42         this.path = Preconditions.checkNotNull(path,"path must be specified");
43         this.xpath = Preconditions.checkNotNull(target,"target must not be null.");
44     }
45
46     public static Leafref create(final SchemaPath path,final RevisionAwareXPath target) {
47         return new Leafref(path,target);
48     }
49
50     @Override
51     public LeafrefTypeDefinition getBaseType() {
52         return null;
53     }
54
55     @Override
56     public String getUnits() {
57         return "";
58     }
59
60     @Override
61     public Object getDefaultValue() {
62         return this;
63     }
64
65     @Override
66     public QName getQName() {
67         return NAME;
68     }
69
70     @Override
71     public SchemaPath getPath() {
72         return path;
73     }
74
75     @Override
76     public String getDescription() {
77         return DESCRIPTION;
78     }
79
80     @Override
81     public String getReference() {
82         return REF;
83     }
84
85     @Override
86     public Status getStatus() {
87         return Status.CURRENT;
88     }
89
90     @Override
91     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
92         return Collections.emptyList();
93     }
94
95     @Override
96     public RevisionAwareXPath getPathStatement() {
97         return xpath;
98     }
99
100     @Override
101     public int hashCode() {
102         final int prime = 31;
103         int result = 1;
104         result = prime * result + ((xpath == null) ? 0 : xpath.hashCode());
105         return result;
106     }
107
108     @Override
109     public boolean equals(final Object obj) {
110         if (this == obj) {
111             return true;
112         }
113         if (obj == null) {
114             return false;
115         }
116         if (getClass() != obj.getClass()) {
117             return false;
118         }
119         Leafref other = (Leafref) obj;
120         if (xpath == null) {
121             if (other.xpath != null) {
122                 return false;
123             }
124         } else if (!xpath.equals(other.xpath)) {
125             return false;
126         }
127         return true;
128     }
129
130     @Override
131     public String toString() {
132         StringBuilder builder = new StringBuilder();
133         builder.append("type ");
134         builder.append(NAME);
135         builder.append(" [xpath=");
136         builder.append(xpath);
137         builder.append("]");
138         return builder.toString();
139     }
140 }