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