2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.api.type;
10 import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_MODULE;
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;
22 public final class TypeDefinitions {
24 * Well-known QName of the {@code binary} built-in type.
26 public static final @NonNull QName BINARY = QName.create(RFC6020_YANG_MODULE, "binary").intern();
29 * Well-known QName of the {@code bits} built-in type.
31 public static final @NonNull QName BITS = QName.create(RFC6020_YANG_MODULE, "bits").intern();
34 * Well-known QName of the {@code boolean} built-in type.
36 public static final @NonNull QName BOOLEAN = QName.create(RFC6020_YANG_MODULE, "boolean").intern();
39 * Well-known QName of the {@code decimal64} built-in type.
41 public static final @NonNull QName DECIMAL64 = QName.create(RFC6020_YANG_MODULE, "decimal64").intern();
44 * Well-known QName of the {@code empty} built-in type.
46 public static final @NonNull QName EMPTY = QName.create(RFC6020_YANG_MODULE, "empty").intern();
49 * Well-known QName of the {@code enumeration} built-in type.
51 public static final @NonNull QName ENUMERATION = QName.create(RFC6020_YANG_MODULE, "enumeration").intern();
54 * Well-known QName of the {@code identityref} built-in type.
56 public static final @NonNull QName IDENTITYREF = QName.create(RFC6020_YANG_MODULE, "identityref").intern();
59 * Well-known QName of the {@code int8} built-in type.
61 public static final @NonNull QName INT8 = QName.create(RFC6020_YANG_MODULE, "int8").intern();
64 * Well-known QName of the {@code int16} built-in type.
66 public static final @NonNull QName INT16 = QName.create(RFC6020_YANG_MODULE, "int16").intern();
69 * Well-known QName of the {@code int32} built-in type.
71 public static final @NonNull QName INT32 = QName.create(RFC6020_YANG_MODULE, "int32").intern();
74 * Well-known QName of the {@code int64} built-in type.
76 public static final @NonNull QName INT64 = QName.create(RFC6020_YANG_MODULE, "int64").intern();
79 * Well-known QName of the {@code string} built-in type.
81 public static final @NonNull QName STRING = QName.create(RFC6020_YANG_MODULE, "string").intern();
84 * Well-known QName of the {@code union} built-in type.
86 public static final @NonNull QName UNION = QName.create(RFC6020_YANG_MODULE, "union").intern();
89 * Well-known QName of the {@code leafref} built-in type.
91 public static final @NonNull QName LEAFREF = QName.create(RFC6020_YANG_MODULE, "leafref").intern();
94 * Well-known QName of the {@code instance-identifier} built-in type.
96 public static final @NonNull QName INSTANCE_IDENTIFIER = QName.create(RFC6020_YANG_MODULE, "instance-identifier")
100 * Well-known QName of the {@code uint8} built-in type.
102 public static final @NonNull QName UINT8 = QName.create(RFC6020_YANG_MODULE, "uint8").intern();
105 * Well-known QName of the {@code uint16} built-in type.
107 public static final @NonNull QName UINT16 = QName.create(RFC6020_YANG_MODULE, "uint16").intern();
110 * Well-known QName of the {@code uint32} built-in type.
112 public static final @NonNull QName UINT32 = QName.create(RFC6020_YANG_MODULE, "uint32").intern();
115 * Well-known QName of the {@code uint64} built-in type.
117 public static final @NonNull QName UINT64 = QName.create(RFC6020_YANG_MODULE, "uint64").intern();
119 private TypeDefinitions() {
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));
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));
133 static <T extends RangeRestrictedTypeDefinition<T, ?>> boolean equals(final @NonNull Class<T> clazz,
134 final @NonNull T type, final @Nullable Object obj) {
139 final @Nullable T other = castIfEquals(clazz, type, obj);
140 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
143 static @NonNull String toString(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
144 return toStringHelper(type).toString();
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)) {
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;
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));
172 static @NonNull ToStringHelper toStringHelper(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
173 return toStringHelper((TypeDefinition<?>) type).add("range", type.getRangeConstraint().orElse(null));