1063f3b22b6bfe4720af2539803f6296875c9c72
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / UnionType.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.controller.yang.model.util;
9
10 import java.net.URI;
11 import java.util.Collections;
12 import java.util.Date;
13 import java.util.List;
14
15 import org.opendaylight.controller.yang.common.QName;
16 import org.opendaylight.controller.yang.model.api.SchemaPath;
17 import org.opendaylight.controller.yang.model.api.Status;
18 import org.opendaylight.controller.yang.model.api.TypeDefinition;
19 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.controller.yang.model.api.type.UnionTypeDefinition;
21
22 public class UnionType implements UnionTypeDefinition {
23
24     private final QName name = BaseTypes.constructQName("union");
25     private final SchemaPath path;
26     private final String description = "The union built-in type represents a value that corresponds to one of its member types.";
27     private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.12";
28     private final UnionTypeDefinition baseType;
29     private final List<TypeDefinition<?>> types;
30
31     private UnionType(List<TypeDefinition<?>> types) {
32         if(types == null) {
33             throw new NullPointerException("When the type is 'union', the 'type' statement MUST be present.");
34         }
35         path = BaseTypes.schemaPath(name);
36         this.types = types;
37         this.baseType = this;
38     }
39
40     public UnionType(final List<String> actualPath, final URI namespace,
41             final Date revision, List<TypeDefinition<?>> types) {
42         if(types == null) {
43             throw new NullPointerException("When the type is 'union', the 'type' statement MUST be present.");
44         }
45         path = BaseTypes.schemaPath(actualPath, namespace, revision);
46         this.types = types;
47         this.baseType = new UnionType(types);
48     }
49
50     @Override
51     public UnionTypeDefinition getBaseType() {
52         return baseType;
53     }
54
55     @Override
56     public String getUnits() {
57         return null;
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 reference;
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 List<TypeDefinition<?>> getTypes() {
97         return types;
98     }
99
100     @Override
101     public int hashCode() {
102         final int prime = 31;
103         int result = 1;
104         result = prime * result + ((name == null) ? 0 : name.hashCode());
105         result = prime * result + ((path == null) ? 0 : path.hashCode());
106         result = prime * result + ((types == null) ? 0 : types.hashCode());
107         return result;
108     }
109
110     @Override
111     public boolean equals(Object obj) {
112         if (this == obj) {
113             return true;
114         }
115         if (obj == null) {
116             return false;
117         }
118         if (getClass() != obj.getClass()) {
119             return false;
120         }
121         UnionType other = (UnionType) obj;
122         if (types == null) {
123             if (other.types != null) {
124                 return false;
125             }
126         } else if (!types.equals(other.types)) {
127             return false;
128         }
129         return true;
130     }
131
132     @Override
133     public String toString() {
134         StringBuilder builder = new StringBuilder();
135         builder.append("UnionType [name=");
136         builder.append(name);
137         builder.append(", types=[");
138         for(TypeDefinition<?> td : types) {
139             builder.append(", "+ td.getQName().getLocalName());
140         }
141         builder.append("]");
142         builder.append("]");
143         return builder.toString();
144     }
145
146 }