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.Int16TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
35 // TODO: this should be in the API package, as it defines equality for TypeDefinitions
36 final class TypeDefinitions {
37 private TypeDefinitions() {
38 throw new UnsupportedOperationException();
41 private static int basicHashCode(final TypeDefinition<?> type) {
42 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
43 type.getUnits().orElse(null), type.getDefaultValue().orElse(null));
46 private static <T extends TypeDefinition<T>> T castIfEquals(final Class<T> clazz, final T type, final Object obj) {
47 if (!clazz.isInstance(obj)) {
51 final T other = clazz.cast(obj);
52 return Objects.equals(type.getPath(), other.getPath())
53 && Objects.equals(type.getBaseType(), other.getBaseType())
54 && Objects.equals(type.getDefaultValue(), other.getDefaultValue())
55 && Objects.equals(type.getUnknownSchemaNodes(), other.getUnknownSchemaNodes())
56 && Objects.equals(type.getUnits(), other.getUnits()) ? other : null;
59 private static ToStringHelper toStringHelper(final TypeDefinition<?> type) {
60 return MoreObjects.toStringHelper(type).omitNullValues()
61 .add("baseType", type.getBaseType())
62 .add("default", type.getDefaultValue().orElse(null))
63 .add("description", type.getDescription().orElse(null))
64 .add("path", type.getPath())
65 .add("reference", type.getReference().orElse(null))
66 .add("status", type.getStatus())
67 .add("units", type.getUnits().orElse(null));
70 static int hashCode(final BinaryTypeDefinition type) {
71 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
72 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getLengthConstraint().orElse(null));
75 static int hashCode(final BitsTypeDefinition type) {
76 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
77 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getBits());
81 static int hashCode(final BooleanTypeDefinition type) {
82 return basicHashCode(type);
85 static int hashCode(final DecimalTypeDefinition type) {
86 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
87 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getFractionDigits(),
88 type.getRangeConstraint().orElse(null));
91 static int hashCode(final EmptyTypeDefinition type) {
92 return basicHashCode(type);
95 static int hashCode(final EnumTypeDefinition type) {
96 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
97 type.getUnits().orElse(null),
98 type.getDefaultValue(), type.getValues());
101 static int hashCode(final IdentityrefTypeDefinition type) {
102 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
103 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getIdentities());
106 static int hashCode(final InstanceIdentifierTypeDefinition type) {
107 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
108 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.requireInstance());
111 private static int hashCode(final RangeRestrictedTypeDefinition<?, ?> type) {
112 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
113 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getRangeConstraint().orElse(null));
116 static int hashCode(final Int8TypeDefinition type) {
117 return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
120 static int hashCode(final Int16TypeDefinition type) {
121 return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
124 static int hashCode(final Int32TypeDefinition type) {
125 return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
128 static int hashCode(final Int64TypeDefinition type) {
129 return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
132 static int hashCode(final LeafrefTypeDefinition type) {
133 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
134 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getPathStatement());
137 static int hashCode(final StringTypeDefinition type) {
138 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
139 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getLengthConstraint().orElse(null),
140 type.getPatternConstraints());
143 static int hashCode(final UnionTypeDefinition type) {
144 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
145 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getTypes());
148 static int hashCode(final Uint8TypeDefinition type) {
149 return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
152 static int hashCode(final Uint16TypeDefinition type) {
153 return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
156 static int hashCode(final Uint32TypeDefinition type) {
157 return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
160 static int hashCode(final Uint64TypeDefinition type) {
161 return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
164 static boolean equals(final BinaryTypeDefinition type, final Object obj) {
169 final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
170 return other != null && type.getLengthConstraint().equals(other.getLengthConstraint());
173 static boolean equals(final BitsTypeDefinition type, final Object obj) {
178 final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
179 return other != null && type.getBits().equals(other.getBits());
182 static boolean equals(final BooleanTypeDefinition type, final Object obj) {
183 return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
186 static boolean equals(final DecimalTypeDefinition type, final Object obj) {
191 final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
192 return other != null && type.getFractionDigits() == other.getFractionDigits()
193 && type.getRangeConstraint().equals(other.getRangeConstraint());
196 static boolean equals(final EmptyTypeDefinition type, final Object obj) {
197 return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
200 static boolean equals(final EnumTypeDefinition type, final Object obj) {
205 final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
206 return other != null && type.getValues().equals(other.getValues());
209 static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
214 final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
215 return other != null && type.getIdentities().equals(other.getIdentities());
218 static boolean equals(final InstanceIdentifierTypeDefinition type, final Object obj) {
223 final InstanceIdentifierTypeDefinition other = castIfEquals(InstanceIdentifierTypeDefinition.class, type, obj);
224 return other != null && type.requireInstance() == other.requireInstance();
227 static boolean equals(final Int8TypeDefinition type, final Object obj) {
232 final Int8TypeDefinition other = castIfEquals(Int8TypeDefinition.class, type, obj);
233 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
236 static boolean equals(final Int16TypeDefinition type, final Object obj) {
241 final Int16TypeDefinition other = castIfEquals(Int16TypeDefinition.class, type, obj);
242 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
245 static boolean equals(final Int32TypeDefinition type, final Object obj) {
250 final Int32TypeDefinition other = castIfEquals(Int32TypeDefinition.class, type, obj);
251 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
254 static boolean equals(final Int64TypeDefinition type, final Object obj) {
259 final Int64TypeDefinition other = castIfEquals(Int64TypeDefinition.class, type, obj);
260 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
263 static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
268 final LeafrefTypeDefinition other = castIfEquals(LeafrefTypeDefinition.class, type, obj);
269 return other != null && type.getPathStatement().equals(other.getPathStatement());
272 static boolean equals(final StringTypeDefinition type, final Object obj) {
277 final StringTypeDefinition other = castIfEquals(StringTypeDefinition.class, type, obj);
278 return other != null && type.getLengthConstraint().equals(other.getLengthConstraint())
279 && type.getPatternConstraints().equals(other.getPatternConstraints());
282 static boolean equals(final UnionTypeDefinition type, final Object obj) {
287 final UnionTypeDefinition other = castIfEquals(UnionTypeDefinition.class, type, obj);
288 return other != null && type.getTypes().equals(other.getTypes());
291 static boolean equals(final Uint8TypeDefinition type, final Object obj) {
296 final Uint8TypeDefinition other = castIfEquals(Uint8TypeDefinition.class, type, obj);
297 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
300 static boolean equals(final Uint16TypeDefinition type, final Object obj) {
305 final Uint16TypeDefinition other = castIfEquals(Uint16TypeDefinition.class, type, obj);
306 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
309 static boolean equals(final Uint32TypeDefinition type, final Object obj) {
314 final Uint32TypeDefinition other = castIfEquals(Uint32TypeDefinition.class, type, obj);
315 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
318 static boolean equals(final Uint64TypeDefinition type, final Object obj) {
323 final Uint64TypeDefinition other = castIfEquals(Uint64TypeDefinition.class, type, obj);
324 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
327 static String toString(final BinaryTypeDefinition type) {
328 return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null)).toString();
331 static String toString(final BitsTypeDefinition type) {
332 return toStringHelper(type).add("bits", type.getBits()).toString();
335 static String toString(final BooleanTypeDefinition type) {
336 return toStringHelper(type).toString();
339 static String toString(final DecimalTypeDefinition type) {
340 return toStringHelper(type).add("fractionDigits", type.getFractionDigits())
341 .add("range", type.getRangeConstraint().orElse(null)).toString();
344 static String toString(final EmptyTypeDefinition type) {
345 return toStringHelper(type).toString();
348 static String toString(final EnumTypeDefinition type) {
349 return toStringHelper(type).add("values", type.getValues()).toString();
352 static String toString(final IdentityrefTypeDefinition type) {
353 return toStringHelper(type).add("identities", type.getIdentities()).toString();
356 static String toString(final InstanceIdentifierTypeDefinition type) {
357 return toStringHelper(type).add("requireInstance", type.requireInstance()).toString();
360 static String toString(final Int8TypeDefinition type) {
361 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
364 static String toString(final Int16TypeDefinition type) {
365 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
368 static String toString(final Int32TypeDefinition type) {
369 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
372 static String toString(final Int64TypeDefinition type) {
373 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
376 static String toString(final LeafrefTypeDefinition type) {
377 return toStringHelper(type).add("pathStatement", type.getPathStatement()).toString();
380 static String toString(final StringTypeDefinition type) {
381 return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null))
382 .add("patterns", type.getPatternConstraints()).toString();
385 static String toString(final UnionTypeDefinition type) {
386 return toStringHelper(type).add("types", type.getTypes()).toString();
389 static String toString(final Uint8TypeDefinition type) {
390 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
393 static String toString(final Uint16TypeDefinition type) {
394 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
397 static String toString(final Uint32TypeDefinition type) {
398 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
401 static String toString(final Uint64TypeDefinition type) {
402 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();