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 com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import java.util.Objects;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 final class TypeDefinitions {
18 private TypeDefinitions() {
19 throw new UnsupportedOperationException();
22 static int basicHashCode(final @NonNull TypeDefinition<?> type) {
23 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
24 type.getUnits().orElse(null), type.getDefaultValue().orElse(null));
27 static int hashCode(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
28 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
29 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getRangeConstraint().orElse(null));
32 static <T extends RangeRestrictedTypeDefinition<T, ?>> boolean equals(final @NonNull Class<T> clazz,
33 final @NonNull T type, final @Nullable Object obj) {
38 final @Nullable T other = castIfEquals(clazz, type, obj);
39 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
42 static @NonNull String toString(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
43 return toStringHelper(type).toString();
46 static <T extends TypeDefinition<T>> @Nullable T castIfEquals(final @NonNull Class<T> clazz, final @NonNull T type,
47 final @Nullable Object obj) {
48 if (!clazz.isInstance(obj)) {
52 final @NonNull T other = clazz.cast(obj);
53 return Objects.equals(type.getPath(), other.getPath())
54 && Objects.equals(type.getBaseType(), other.getBaseType())
55 && Objects.equals(type.getDefaultValue(), other.getDefaultValue())
56 && Objects.equals(type.getUnknownSchemaNodes(), other.getUnknownSchemaNodes())
57 && Objects.equals(type.getUnits(), other.getUnits()) ? other : null;
60 static @NonNull ToStringHelper toStringHelper(final @NonNull TypeDefinition<?> type) {
61 return MoreObjects.toStringHelper(type).omitNullValues()
62 .add("path", type.getPath())
63 .add("baseType", type.getBaseType())
64 .add("default", type.getDefaultValue().orElse(null))
65 .add("description", type.getDescription().orElse(null))
66 .add("reference", type.getReference().orElse(null))
67 .add("status", type.getStatus())
68 .add("units", type.getUnits().orElse(null));
71 static @NonNull ToStringHelper toStringHelper(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
72 return toStringHelper((TypeDefinition<?>) type).add("range", type.getRangeConstraint().orElse(null));