Fixing sonar issues 4
[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 static final String DESCRIPTION = "The union built-in type represents a value that corresponds to one of its member types.";
24     private static 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 IllegalArgumentException("When the type is 'union', the 'type' statement MUST be present.");
30         }
31         this.types = types;
32     }
33
34     @Override
35     public UnionTypeDefinition getBaseType() {
36         return this;
37     }
38
39     @Override
40     public String getUnits() {
41         return null;
42     }
43
44     @Override
45     public Object getDefaultValue() {
46         return null;
47     }
48
49     @Override
50     public QName getQName() {
51         return name;
52     }
53
54     @Override
55     public SchemaPath getPath() {
56         return path;
57     }
58
59     @Override
60     public String getDescription() {
61         return DESCRIPTION;
62     }
63
64     @Override
65     public String getReference() {
66         return REFERENCE;
67     }
68
69     @Override
70     public Status getStatus() {
71         return Status.CURRENT;
72     }
73
74     @Override
75     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
76         return Collections.emptyList();
77     }
78
79     @Override
80     public List<TypeDefinition<?>> getTypes() {
81         return types;
82     }
83
84     @Override
85     public int hashCode() {
86         final int prime = 31;
87         int result = 1;
88         result = prime * result + ((types == null) ? 0 : types.hashCode());
89         return result;
90     }
91
92     @Override
93     public boolean equals(Object obj) {
94         if (this == obj) {
95             return true;
96         }
97         if (obj == null) {
98             return false;
99         }
100         if (getClass() != obj.getClass()) {
101             return false;
102         }
103         UnionType other = (UnionType) obj;
104         if (!types.equals(other.types)) {
105             return false;
106         }
107         return true;
108     }
109
110     @Override
111     public String toString() {
112         StringBuilder builder = new StringBuilder();
113         builder.append("type ");
114         builder.append(name);
115         builder.append(" (types=[");
116         for (TypeDefinition<?> td : types) {
117             builder.append(", " + td.getQName().getLocalName());
118         }
119         builder.append("]");
120         return builder.toString();
121     }
122
123 }