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