Drop unneeded generic type specifiers
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / BaseTypes.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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import com.google.common.collect.ImmutableSet;
14 import java.net.URI;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.YangConstants;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20
21 /**
22  * Utility methods and constants to work with built-in YANG types
23  *
24  *
25  */
26 public final class BaseTypes {
27
28     private BaseTypes() {
29     }
30
31     /**
32      * @deprecated Use {@link YangConstants#RFC6020_YANG_NAMESPACE} instead. To be removed in Boron release.
33      */
34     @Deprecated
35     public static final URI BASE_TYPES_NAMESPACE = YangConstants.RFC6020_YANG_NAMESPACE;
36     /**
37      * @deprecated Use {@link YangConstants#RFC6020_YANG_MODULE} instead. To be removed in Boron release.
38      */
39     @Deprecated
40     public static final QNameModule BASE_TYPES_MODULE = YangConstants.RFC6020_YANG_MODULE;
41
42     public static final QName BINARY_QNAME = constructQName("binary");
43     public static final QName BITS_QNAME = constructQName("bits");
44     public static final QName BOOLEAN_QNAME = constructQName("boolean");
45     public static final QName DECIMAL64_QNAME = constructQName("decimal64");
46     public static final QName EMPTY_QNAME = constructQName("empty");
47     public static final QName ENUMERATION_QNAME = constructQName("enumeration");
48     public static final QName IDENTITYREF_QNAME = constructQName("identityref");
49     public static final QName INSTANCE_IDENTIFIER_QNAME = constructQName("instance-identifier");
50     public static final QName INT8_QNAME = constructQName("int8");
51     public static final QName INT16_QNAME = constructQName("int16");
52     public static final QName INT32_QNAME = constructQName("int32");
53     public static final QName INT64_QNAME = constructQName("int64");
54     public static final QName LEAFREF_QNAME = constructQName("leafref");
55     public static final QName STRING_QNAME = constructQName("string");
56     public static final QName UINT8_QNAME = constructQName("uint8");
57     public static final QName UINT16_QNAME = constructQName("uint16");
58     public static final QName UINT32_QNAME = constructQName("uint32");
59     public static final QName UINT64_QNAME = constructQName("uint64");
60     public static final QName UNION_QNAME = constructQName("union");
61
62     private static final Set<QName> BUILT_IN_TYPES = ImmutableSet.<QName>builder()
63             .add(BINARY_QNAME)
64             .add(BITS_QNAME)
65             .add(BOOLEAN_QNAME)
66             .add(DECIMAL64_QNAME)
67             .add(EMPTY_QNAME)
68             .add(ENUMERATION_QNAME)
69             .add(IDENTITYREF_QNAME)
70             .add(INSTANCE_IDENTIFIER_QNAME)
71             .add(INT8_QNAME)
72             .add(INT16_QNAME)
73             .add(INT32_QNAME)
74             .add(INT64_QNAME)
75             .add(LEAFREF_QNAME)
76             .add(STRING_QNAME)
77             .add(UINT8_QNAME)
78             .add(UINT16_QNAME)
79             .add(UINT32_QNAME)
80             .add(UINT64_QNAME)
81             .add(UNION_QNAME)
82             .build();
83
84     /**
85      * Construct QName for Built-in base Yang type. The namespace for built-in
86      * base yang types is defined as: urn:ietf:params:xml:ns:yang:1
87      *
88      * @param typeName
89      *            yang type name
90      * @return built-in base yang type QName.
91      */
92     public static QName constructQName(final String typeName) {
93         return QName.create(YangConstants.RFC6020_YANG_MODULE, typeName).intern();
94     }
95
96     /**
97      * Returns true if supplied type is representation of built-in YANG type as
98      * per RFC 6020.
99      *
100      * See package documentation for description of base types.
101      *
102      * @param type
103      * @return true if type is built-in YANG Types.
104      */
105     public static boolean isYangBuildInType(final String type) {
106         return !Strings.isNullOrEmpty(type) && BUILT_IN_TYPES.contains(
107                 QName.create(YangConstants.RFC6020_YANG_MODULE, type));
108     }
109
110     /**
111      * Returns true if supplied type is representation of built-in YANG type as
112      * per RFC 6020.
113      *
114      * See package documentation for description of base types.
115      *
116      * @param type
117      * @return true if type is built-in YANG Types.
118      */
119     public static boolean isYangBuildInType(final TypeDefinition<?> type) {
120         return type != null && BUILT_IN_TYPES.contains(type.getQName());
121     }
122
123     /**
124      * Returns default instance of built-in for supplied type
125      *
126      * See package documentation for description of base build-in types
127      * with default instance.
128      *
129      * @param typeName
130      * @return Returns default instance or {@link Optional#absent()} if default
131      *         instance does not exists
132      *
133      * @deprecated Use parser-specific lookup tables.
134      */
135     @Deprecated
136     public static Optional<TypeDefinition<?>> defaultBaseTypeFor(final String typeName) {
137         return Optional.fromNullable(defaultBaseTypeForImpl(typeName));
138     }
139
140     @Deprecated
141     private static TypeDefinition<?> defaultBaseTypeForImpl(final String typeName) {
142         Preconditions.checkNotNull(typeName, "typeName must not be null.");
143
144         if (typeName.startsWith("int")) {
145             if ("int8".equals(typeName)) {
146                 return Int8.getInstance();
147             } else if ("int16".equals(typeName)) {
148                 return Int16.getInstance();
149             } else if ("int32".equals(typeName)) {
150                 return Int32.getInstance();
151             } else if ("int64".equals(typeName)) {
152                 return Int64.getInstance();
153             }
154         } else if (typeName.startsWith("uint")) {
155             if ("uint8".equals(typeName)) {
156                 return Uint8.getInstance();
157             } else if ("uint16".equals(typeName)) {
158                 return Uint16.getInstance();
159             } else if ("uint32".equals(typeName)) {
160                 return Uint32.getInstance();
161             } else if ("uint64".equals(typeName)) {
162                 return Uint64.getInstance();
163             }
164         } else if ("string".equals(typeName)) {
165             return StringType.getInstance();
166         } else if ("binary".equals(typeName)) {
167             return BinaryType.getInstance();
168         } else if ("boolean".equals(typeName)) {
169             return BooleanType.getInstance();
170         } else if ("empty".equals(typeName)) {
171             return EmptyType.getInstance();
172         } else if ("instance-identifier".equals(typeName)) {
173             return InstanceIdentifierType.getInstance();
174         }
175         return null;
176     }
177
178 }