Refactored base yang-java types.
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / 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.yangtools.yang.model.util;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.Status;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
19
20 public final class UnionType implements UnionTypeDefinition {
21     private final QName name = BaseTypes.constructQName("union");
22     private final SchemaPath path = BaseTypes.schemaPath(name);
23     private final String description = "The union built-in type represents a value that corresponds to one of its member types.";
24     private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.12";
25     private final List<TypeDefinition<?>> types;
26
27     public UnionType(List<TypeDefinition<?>> types) {
28         if (types == null) {
29             throw new NullPointerException(
30                     "When the type is 'union', the 'type' statement MUST be present.");
31         }
32         this.types = types;
33     }
34
35     @Override
36     public UnionTypeDefinition getBaseType() {
37         return this;
38     }
39
40     @Override
41     public String getUnits() {
42         return null;
43     }
44
45     @Override
46     public Object getDefaultValue() {
47         return null;
48     }
49
50     @Override
51     public QName getQName() {
52         return name;
53     }
54
55     @Override
56     public SchemaPath getPath() {
57         return path;
58     }
59
60     @Override
61     public String getDescription() {
62         return description;
63     }
64
65     @Override
66     public String getReference() {
67         return reference;
68     }
69
70     @Override
71     public Status getStatus() {
72         return Status.CURRENT;
73     }
74
75     @Override
76     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
77         return Collections.emptyList();
78     }
79
80     @Override
81     public List<TypeDefinition<?>> getTypes() {
82         return types;
83     }
84
85     @Override
86     public int hashCode() {
87         final int prime = 31;
88         int result = 1;
89         result = prime * result + ((types == null) ? 0 : types.hashCode());
90         return result;
91     }
92
93     @Override
94     public boolean equals(Object obj) {
95         if (this == obj) {
96             return true;
97         }
98         if (obj == null) {
99             return false;
100         }
101         if (getClass() != obj.getClass()) {
102             return false;
103         }
104         UnionType other = (UnionType) obj;
105         if (!types.equals(other.types)) {
106             return false;
107         }
108         return true;
109     }
110
111     @Override
112     public String toString() {
113         StringBuilder builder = new StringBuilder();
114         builder.append("type ");
115         builder.append(name);
116         builder.append(" (types=[");
117         for (TypeDefinition<?> td : types) {
118             builder.append(", " + td.getQName().getLocalName());
119         }
120         builder.append("]");
121         return builder.toString();
122     }
123
124 }