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.util.type;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
28 // TODO: this should be in the API package, as it defines equality for TypeDefinitions
29 final class TypeDefinitions {
30 private TypeDefinitions() {
31 throw new UnsupportedOperationException();
34 private static int basicHashCode(final TypeDefinition<?> type) {
35 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
36 type.getDefaultValue());
39 private static <T extends TypeDefinition<T>> T castIfEquals(final Class<T> clazz, final T type, final Object obj) {
40 if (!clazz.isInstance(obj)) {
44 final T other = clazz.cast(obj);
45 return Objects.equals(type.getPath(), other.getPath())
46 && Objects.equals(type.getBaseType(), other.getBaseType())
47 && Objects.equals(type.getDefaultValue(), other.getDefaultValue())
48 && Objects.equals(type.getUnknownSchemaNodes(), other.getUnknownSchemaNodes())
49 && Objects.equals(type.getUnits(), other.getUnits()) ? other : null;
52 private static ToStringHelper toStringHelper(final TypeDefinition<?> type) {
53 return MoreObjects.toStringHelper(type).omitNullValues()
54 .add("baseType", type.getBaseType())
55 .add("default", type.getDefaultValue())
56 .add("description", type.getDescription())
57 .add("path", type.getPath())
58 .add("reference", type.getReference())
59 .add("status", type.getStatus())
60 .add("units", type.getUnits());
63 static int hashCode(final BinaryTypeDefinition type) {
64 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
65 type.getDefaultValue(), type.getLengthConstraints());
68 static boolean equals(final BinaryTypeDefinition type, final Object obj) {
73 final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
74 return other != null && type.getLengthConstraints().equals(other.getLengthConstraints());
77 static String toString(final BinaryTypeDefinition type) {
78 return toStringHelper(type).add("length", type.getLengthConstraints()).toString();
81 static int hashCode(final BitsTypeDefinition type) {
82 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
83 type.getDefaultValue(), type.getBits());
86 static boolean equals(final BitsTypeDefinition type, final Object obj) {
91 final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
92 return other != null && type.getBits().equals(other.getBits());
95 static String toString(final BitsTypeDefinition type) {
96 return toStringHelper(type).add("bits", type.getBits()).toString();
99 static int hashCode(final BooleanTypeDefinition type) {
100 return basicHashCode(type);
103 static boolean equals(final BooleanTypeDefinition type, final Object obj) {
104 return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
107 static String toString(final BooleanTypeDefinition type) {
108 return toStringHelper(type).toString();
111 static int hashCode(final DecimalTypeDefinition type) {
112 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
113 type.getDefaultValue(), type.getFractionDigits(), type.getRangeConstraints());
116 static boolean equals(final DecimalTypeDefinition type, final Object obj) {
121 final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
122 return other != null && type.getFractionDigits().equals(other.getFractionDigits())
123 && type.getRangeConstraints().equals(other.getRangeConstraints());
126 static String toString(final DecimalTypeDefinition type) {
127 return toStringHelper(type).add("fractionDigits", type.getFractionDigits())
128 .add("range", type.getRangeConstraints()).toString();
131 static int hashCode(final EmptyTypeDefinition type) {
132 return basicHashCode(type);
135 static boolean equals(final EmptyTypeDefinition type, final Object obj) {
136 return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
139 static String toString(final EmptyTypeDefinition type) {
140 return toStringHelper(type).toString();
143 static int hashCode(final EnumTypeDefinition type) {
144 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
145 type.getDefaultValue(), type.getValues());
148 static boolean equals(final EnumTypeDefinition type, final Object obj) {
153 final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
154 return other != null && type.getValues().equals(other.getValues());
157 static String toString(final EnumTypeDefinition type) {
158 return toStringHelper(type).add("values", type.getValues()).toString();
161 static int hashCode(final IdentityrefTypeDefinition type) {
162 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
163 type.getDefaultValue(), type.getIdentity());
166 static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
171 final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
172 return other != null && type.getIdentity().equals(other.getIdentity());
175 static String toString(final IdentityrefTypeDefinition type) {
176 return toStringHelper(type).add("identity", type.getIdentity()).toString();
179 static int hashCode(final InstanceIdentifierTypeDefinition type) {
180 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
181 type.getDefaultValue(), type.requireInstance());
184 static boolean equals(final InstanceIdentifierTypeDefinition type, final Object obj) {
189 final InstanceIdentifierTypeDefinition other = castIfEquals(InstanceIdentifierTypeDefinition.class, type, obj);
190 return other != null && type.requireInstance() == other.requireInstance();
193 static String toString(final InstanceIdentifierTypeDefinition type) {
194 return toStringHelper(type).add("requireInstance", type.requireInstance()).toString();
197 static int hashCode(final IntegerTypeDefinition type) {
198 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
199 type.getDefaultValue(), type.getRangeConstraints());
202 static boolean equals(final IntegerTypeDefinition type, final Object obj) {
207 final IntegerTypeDefinition other = castIfEquals(IntegerTypeDefinition.class, type, obj);
208 return other != null && type.getRangeConstraints().equals(other.getRangeConstraints());
211 static String toString(final IntegerTypeDefinition type) {
212 return toStringHelper(type).add("range", type.getRangeConstraints()).toString();
215 static int hashCode(final LeafrefTypeDefinition type) {
216 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
217 type.getDefaultValue(), type.getPathStatement());
220 static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
225 final LeafrefTypeDefinition other =castIfEquals(LeafrefTypeDefinition.class, type, obj);
226 return other != null && type.getPathStatement().equals(other.getPathStatement());
229 static String toString(final LeafrefTypeDefinition type) {
230 return toStringHelper(type).add("pathStatement", type.getPathStatement()).toString();
233 static int hashCode(final StringTypeDefinition type) {
234 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
235 type.getDefaultValue(), type.getLengthConstraints(), type.getPatternConstraints());
238 static boolean equals(final StringTypeDefinition type, final Object obj) {
243 final StringTypeDefinition other = castIfEquals(StringTypeDefinition.class, type, obj);
244 return other != null && type.getLengthConstraints().equals(other.getLengthConstraints())
245 && type.getPatternConstraints().equals(other.getPatternConstraints());
248 static String toString(final StringTypeDefinition type) {
249 return toStringHelper(type).add("length", type.getLengthConstraints())
250 .add("patterns", type.getPatternConstraints()).toString();
253 static int hashCode(final UnionTypeDefinition type) {
254 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
255 type.getDefaultValue(), type.getTypes());
258 static boolean equals(final UnionTypeDefinition type, final Object obj) {
263 final UnionTypeDefinition other = castIfEquals(UnionTypeDefinition.class, type, obj);
264 return other != null && type.getTypes().equals(other.getTypes());
267 static String toString(final UnionTypeDefinition type) {
268 return toStringHelper(type).add("types", type.getTypes()).toString();
271 static int hashCode(final UnsignedIntegerTypeDefinition type) {
272 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
273 type.getDefaultValue(), type.getRangeConstraints());
276 static boolean equals(final UnsignedIntegerTypeDefinition type, final Object obj) {
281 final UnsignedIntegerTypeDefinition other = castIfEquals(UnsignedIntegerTypeDefinition.class, type, obj);
282 return other != null && type.getRangeConstraints().equals(other.getRangeConstraints());
285 static String toString(final UnsignedIntegerTypeDefinition type) {
286 return toStringHelper(type).add("range", type.getRangeConstraints()).toString();