Merge branch 'master' of ../controller
[yangtools.git] / yang / 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 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;
16
17 final class TypeDefinitions {
18     private TypeDefinitions() {
19         throw new UnsupportedOperationException();
20     }
21
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));
25     }
26
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));
30     }
31
32     static <T extends RangeRestrictedTypeDefinition<T, ?>> boolean equals(final @NonNull Class<T> clazz,
33             final @NonNull T type, final @Nullable Object obj) {
34         if (type == obj) {
35             return true;
36         }
37
38         final @Nullable T other = castIfEquals(clazz, type, obj);
39         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
40     }
41
42     static @NonNull String toString(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
43         return toStringHelper(type).toString();
44     }
45
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)) {
49             return null;
50         }
51
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;
58     }
59
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));
69     }
70
71     static @NonNull ToStringHelper toStringHelper(final @NonNull RangeRestrictedTypeDefinition<?, ?> type) {
72         return toStringHelper((TypeDefinition<?>) type).add("range", type.getRangeConstraint().orElse(null));
73     }
74 }