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 int hashCode(final BitsTypeDefinition type) {
69 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
70 type.getDefaultValue(), type.getBits());
74 static int hashCode(final BooleanTypeDefinition type) {
75 return basicHashCode(type);
78 static int hashCode(final DecimalTypeDefinition type) {
79 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
80 type.getDefaultValue(), type.getFractionDigits(), type.getRangeConstraints());
83 static int hashCode(final EmptyTypeDefinition type) {
84 return basicHashCode(type);
87 static int hashCode(final EnumTypeDefinition type) {
88 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
89 type.getDefaultValue(), type.getValues());
92 static int hashCode(final IdentityrefTypeDefinition type) {
93 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
94 type.getDefaultValue(), type.getIdentity());
97 static int hashCode(final InstanceIdentifierTypeDefinition type) {
98 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
99 type.getDefaultValue(), type.requireInstance());
102 static int hashCode(final IntegerTypeDefinition type) {
103 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
104 type.getDefaultValue(), type.getRangeConstraints());
107 static int hashCode(final LeafrefTypeDefinition type) {
108 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
109 type.getDefaultValue(), type.getPathStatement());
112 static int hashCode(final StringTypeDefinition type) {
113 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
114 type.getDefaultValue(), type.getLengthConstraints(), type.getPatternConstraints());
117 static int hashCode(final UnionTypeDefinition type) {
118 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
119 type.getDefaultValue(), type.getTypes());
122 static int hashCode(final UnsignedIntegerTypeDefinition type) {
123 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
124 type.getDefaultValue(), type.getRangeConstraints());
127 static boolean equals(final BinaryTypeDefinition type, final Object obj) {
132 final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
133 return other != null && type.getLengthConstraints().equals(other.getLengthConstraints());
136 static boolean equals(final BitsTypeDefinition type, final Object obj) {
141 final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
142 return other != null && type.getBits().equals(other.getBits());
145 static boolean equals(final BooleanTypeDefinition type, final Object obj) {
146 return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
149 static boolean equals(final DecimalTypeDefinition type, final Object obj) {
154 final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
155 return other != null && type.getFractionDigits().equals(other.getFractionDigits())
156 && type.getRangeConstraints().equals(other.getRangeConstraints());
159 static boolean equals(final EmptyTypeDefinition type, final Object obj) {
160 return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
163 static boolean equals(final EnumTypeDefinition type, final Object obj) {
168 final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
169 return other != null && type.getValues().equals(other.getValues());
172 static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
177 final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
178 return other != null && type.getIdentity().equals(other.getIdentity());
181 static boolean equals(final InstanceIdentifierTypeDefinition type, final Object obj) {
186 final InstanceIdentifierTypeDefinition other = castIfEquals(InstanceIdentifierTypeDefinition.class, type, obj);
187 return other != null && type.requireInstance() == other.requireInstance();
190 static boolean equals(final IntegerTypeDefinition type, final Object obj) {
195 final IntegerTypeDefinition other = castIfEquals(IntegerTypeDefinition.class, type, obj);
196 return other != null && type.getRangeConstraints().equals(other.getRangeConstraints());
199 static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
204 final LeafrefTypeDefinition other = castIfEquals(LeafrefTypeDefinition.class, type, obj);
205 return other != null && type.getPathStatement().equals(other.getPathStatement());
208 static boolean equals(final StringTypeDefinition type, final Object obj) {
213 final StringTypeDefinition other = castIfEquals(StringTypeDefinition.class, type, obj);
214 return other != null && type.getLengthConstraints().equals(other.getLengthConstraints())
215 && type.getPatternConstraints().equals(other.getPatternConstraints());
218 static boolean equals(final UnionTypeDefinition type, final Object obj) {
223 final UnionTypeDefinition other = castIfEquals(UnionTypeDefinition.class, type, obj);
224 return other != null && type.getTypes().equals(other.getTypes());
227 static boolean equals(final UnsignedIntegerTypeDefinition type, final Object obj) {
232 final UnsignedIntegerTypeDefinition other = castIfEquals(UnsignedIntegerTypeDefinition.class, type, obj);
233 return other != null && type.getRangeConstraints().equals(other.getRangeConstraints());
236 static String toString(final BinaryTypeDefinition type) {
237 return toStringHelper(type).add("length", type.getLengthConstraints()).toString();
240 static String toString(final BitsTypeDefinition type) {
241 return toStringHelper(type).add("bits", type.getBits()).toString();
244 static String toString(final BooleanTypeDefinition type) {
245 return toStringHelper(type).toString();
248 static String toString(final DecimalTypeDefinition type) {
249 return toStringHelper(type).add("fractionDigits", type.getFractionDigits())
250 .add("range", type.getRangeConstraints()).toString();
253 static String toString(final EmptyTypeDefinition type) {
254 return toStringHelper(type).toString();
257 static String toString(final EnumTypeDefinition type) {
258 return toStringHelper(type).add("values", type.getValues()).toString();
261 static String toString(final IdentityrefTypeDefinition type) {
262 return toStringHelper(type).add("identity", type.getIdentity()).toString();
265 static String toString(final InstanceIdentifierTypeDefinition type) {
266 return toStringHelper(type).add("requireInstance", type.requireInstance()).toString();
269 static String toString(final IntegerTypeDefinition type) {
270 return toStringHelper(type).add("range", type.getRangeConstraints()).toString();
273 static String toString(final LeafrefTypeDefinition type) {
274 return toStringHelper(type).add("pathStatement", type.getPathStatement()).toString();
277 static String toString(final StringTypeDefinition type) {
278 return toStringHelper(type).add("length", type.getLengthConstraints())
279 .add("patterns", type.getPatternConstraints()).toString();
282 static String toString(final UnionTypeDefinition type) {
283 return toStringHelper(type).add("types", type.getTypes()).toString();
286 static String toString(final UnsignedIntegerTypeDefinition type) {
287 return toStringHelper(type).add("range", type.getRangeConstraints()).toString();