Populate data/ hierarchy
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / type / TypeDefinitions.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.api.type;
9
10 import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_MODULE;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import java.util.Objects;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20
21 @Beta
22 public final class TypeDefinitions {
23     /**
24      * Well-known QName of the {@code binary} built-in type.
25      */
26     public static final @NonNull QName BINARY = QName.create(RFC6020_YANG_MODULE, "binary").intern();
27
28     /**
29      * Well-known QName of the {@code bits} built-in type.
30      */
31     public static final @NonNull QName BITS = QName.create(RFC6020_YANG_MODULE, "bits").intern();
32
33     /**
34      * Well-known QName of the {@code boolean} built-in type.
35      */
36     public static final @NonNull QName BOOLEAN = QName.create(RFC6020_YANG_MODULE, "boolean").intern();
37
38     /**
39      * Well-known QName of the {@code decimal64} built-in type.
40      */
41     public static final @NonNull QName DECIMAL64 = QName.create(RFC6020_YANG_MODULE, "decimal64").intern();
42
43     /**
44      * Well-known QName of the {@code empty} built-in type.
45      */
46     public static final @NonNull QName EMPTY = QName.create(RFC6020_YANG_MODULE, "empty").intern();
47
48     /**
49      * Well-known QName of the {@code enumeration} built-in type.
50      */
51     public static final @NonNull QName ENUMERATION = QName.create(RFC6020_YANG_MODULE, "enumeration").intern();
52
53     /**
54      * Well-known QName of the {@code identityref} built-in type.
55      */
56     public static final @NonNull QName IDENTITYREF = QName.create(RFC6020_YANG_MODULE, "identityref").intern();
57
58     /**
59      * Well-known QName of the {@code int8} built-in type.
60      */
61     public static final @NonNull QName INT8 = QName.create(RFC6020_YANG_MODULE, "int8").intern();
62
63     /**
64      * Well-known QName of the {@code int16} built-in type.
65      */
66     public static final @NonNull QName INT16 = QName.create(RFC6020_YANG_MODULE, "int16").intern();
67
68     /**
69      * Well-known QName of the {@code int32} built-in type.
70      */
71     public static final @NonNull QName INT32 = QName.create(RFC6020_YANG_MODULE, "int32").intern();
72
73     /**
74      * Well-known QName of the {@code int64} built-in type.
75      */
76     public static final @NonNull QName INT64 = QName.create(RFC6020_YANG_MODULE, "int64").intern();
77
78     /**
79      * Well-known QName of the {@code string} built-in type.
80      */
81     public static final @NonNull QName STRING = QName.create(RFC6020_YANG_MODULE, "string").intern();
82
83     /**
84      * Well-known QName of the {@code union} built-in type.
85      */
86     public static final @NonNull QName UNION = QName.create(RFC6020_YANG_MODULE, "union").intern();
87
88     /**
89      * Well-known QName of the {@code leafref} built-in type.
90      */
91     public static final @NonNull QName LEAFREF = QName.create(RFC6020_YANG_MODULE, "leafref").intern();
92
93     /**
94      * Well-known QName of the {@code instance-identifier} built-in type.
95      */
96     public static final @NonNull QName INSTANCE_IDENTIFIER = QName.create(RFC6020_YANG_MODULE, "instance-identifier")
97         .intern();
98
99     /**
100      * Well-known QName of the {@code uint8} built-in type.
101      */
102     public static final @NonNull QName UINT8 = QName.create(RFC6020_YANG_MODULE, "uint8").intern();
103
104     /**
105      * Well-known QName of the {@code uint16} built-in type.
106      */
107     public static final @NonNull QName UINT16 = QName.create(RFC6020_YANG_MODULE, "uint16").intern();
108
109     /**
110      * Well-known QName of the {@code uint32} built-in type.
111      */
112     public static final @NonNull QName UINT32 = QName.create(RFC6020_YANG_MODULE, "uint32").intern();
113
114     /**
115      * Well-known QName of the {@code uint64} built-in type.
116      */
117     public static final @NonNull QName UINT64 = QName.create(RFC6020_YANG_MODULE, "uint64").intern();
118
119     private TypeDefinitions() {
120         // Hidden on purpose
121     }
122
123     static int basicHashCode(final @NonNull TypeDefinition<?> type) {
124         return Objects.hash(type.getQName(), type.getUnknownSchemaNodes(), type.getBaseType(),
125             type.getUnits().orElse(null), type.getDefaultValue().orElse(null));
126     }
127
128     static int hashCode(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
129         return Objects.hash(type.getQName(), type.getUnknownSchemaNodes(), type.getBaseType(),
130             type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getRangeConstraint().orElse(null));
131     }
132
133     static <T extends RangeRestrictedTypeDefinition<T, ?>> boolean equals(final @NonNull Class<T> clazz,
134             final @NonNull T type, final @Nullable Object obj) {
135         if (type == obj) {
136             return true;
137         }
138
139         final @Nullable T other = castIfEquals(clazz, type, obj);
140         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
141     }
142
143     static @NonNull String toString(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
144         return toStringHelper(type).toString();
145     }
146
147     static <T extends TypeDefinition<T>> @Nullable T castIfEquals(final @NonNull Class<T> clazz, final @NonNull T type,
148             final @Nullable Object obj) {
149         if (!clazz.isInstance(obj)) {
150             return null;
151         }
152
153         final @NonNull T other = clazz.cast(obj);
154         return Objects.equals(type.getQName(), other.getQName())
155                 && Objects.equals(type.getBaseType(), other.getBaseType())
156                 && Objects.equals(type.getDefaultValue(), other.getDefaultValue())
157                 && Objects.equals(type.getUnknownSchemaNodes(), other.getUnknownSchemaNodes())
158                 && Objects.equals(type.getUnits(), other.getUnits()) ? other : null;
159     }
160
161     static @NonNull ToStringHelper toStringHelper(final @NonNull TypeDefinition<?> type) {
162         return MoreObjects.toStringHelper(type).omitNullValues()
163                 .add("name", type.getQName())
164                 .add("baseType", type.getBaseType())
165                 .add("default", type.getDefaultValue().orElse(null))
166                 .add("description", type.getDescription().orElse(null))
167                 .add("reference", type.getReference().orElse(null))
168                 .add("status", type.getStatus())
169                 .add("units", type.getUnits().orElse(null));
170     }
171
172     static @NonNull ToStringHelper toStringHelper(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
173         return toStringHelper((TypeDefinition<?>) type).add("range", type.getRangeConstraint().orElse(null));
174     }
175 }