BUG-4322: Leafref should not have a default value
[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 java.util.Objects;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
20
21 /**
22  * The <code>default</code> implementation of Instance Leafref Type Definition
23  * interface.
24  *
25  * @see LeafrefTypeDefinition
26  */
27 public final class Leafref implements LeafrefTypeDefinition {
28     private static final QName NAME = BaseTypes.constructQName("leafref");
29     private static final SchemaPath PATH = SchemaPath.create(true, NAME);
30     private static final String DESCRIPTION = "The leafref type is used to reference a particular leaf instance in the data tree.";
31     private static final String REF = "https://tools.ietf.org/html/rfc6020#section-9.9";
32
33     private final RevisionAwareXPath xpath;
34     private final SchemaPath path;
35
36     @Deprecated
37     public Leafref(final RevisionAwareXPath xpath) {
38         this(PATH, xpath);
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 null;
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 + Objects.hashCode(xpath);
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         return Objects.equals(xpath, other.xpath);
121     }
122
123     @Override
124     public String toString() {
125         StringBuilder builder = new StringBuilder();
126         builder.append("type ");
127         builder.append(NAME);
128         builder.append(" [xpath=");
129         builder.append(xpath);
130         builder.append("]");
131         return builder.toString();
132     }
133 }