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(),
36 type.getUnits().orElse(null), type.getDefaultValue().orElse(null));
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().orElse(null))
56 .add("description", type.getDescription().orElse(null))
57 .add("path", type.getPath())
58 .add("reference", type.getReference().orElse(null))
59 .add("status", type.getStatus())
60 .add("units", type.getUnits().orElse(null));
63 static int hashCode(final BinaryTypeDefinition type) {
64 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
65 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getLengthConstraint().orElse(null));
68 static int hashCode(final BitsTypeDefinition type) {
69 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
70 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), 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(),
80 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getFractionDigits(),
81 type.getRangeConstraint().orElse(null));
84 static int hashCode(final EmptyTypeDefinition type) {
85 return basicHashCode(type);
88 static int hashCode(final EnumTypeDefinition type) {
89 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
90 type.getUnits().orElse(null),
91 type.getDefaultValue(), type.getValues());
94 static int hashCode(final IdentityrefTypeDefinition type) {
95 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
96 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getIdentities());
99 static int hashCode(final InstanceIdentifierTypeDefinition type) {
100 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
101 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.requireInstance());
104 static int hashCode(final IntegerTypeDefinition type) {
105 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
106 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getRangeConstraint().orElse(null));
109 static int hashCode(final LeafrefTypeDefinition type) {
110 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
111 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getPathStatement());
114 static int hashCode(final StringTypeDefinition type) {
115 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
116 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getLengthConstraint().orElse(null),
117 type.getPatternConstraints());
120 static int hashCode(final UnionTypeDefinition type) {
121 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(),
122 type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getTypes());
125 static int hashCode(final UnsignedIntegerTypeDefinition type) {
126 return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
127 type.getDefaultValue(), type.getRangeConstraint());
130 static boolean equals(final BinaryTypeDefinition type, final Object obj) {
135 final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
136 return other != null && type.getLengthConstraint().equals(other.getLengthConstraint());
139 static boolean equals(final BitsTypeDefinition type, final Object obj) {
144 final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
145 return other != null && type.getBits().equals(other.getBits());
148 static boolean equals(final BooleanTypeDefinition type, final Object obj) {
149 return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
152 static boolean equals(final DecimalTypeDefinition type, final Object obj) {
157 final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
158 return other != null && type.getFractionDigits() == other.getFractionDigits()
159 && type.getRangeConstraint().equals(other.getRangeConstraint());
162 static boolean equals(final EmptyTypeDefinition type, final Object obj) {
163 return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
166 static boolean equals(final EnumTypeDefinition type, final Object obj) {
171 final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
172 return other != null && type.getValues().equals(other.getValues());
175 static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
180 final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
181 return other != null && type.getIdentities().equals(other.getIdentities());
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 boolean equals(final IntegerTypeDefinition type, final Object obj) {
198 final IntegerTypeDefinition other = castIfEquals(IntegerTypeDefinition.class, type, obj);
199 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
202 static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
207 final LeafrefTypeDefinition other = castIfEquals(LeafrefTypeDefinition.class, type, obj);
208 return other != null && type.getPathStatement().equals(other.getPathStatement());
211 static boolean equals(final StringTypeDefinition type, final Object obj) {
216 final StringTypeDefinition other = castIfEquals(StringTypeDefinition.class, type, obj);
217 return other != null && type.getLengthConstraint().equals(other.getLengthConstraint())
218 && type.getPatternConstraints().equals(other.getPatternConstraints());
221 static boolean equals(final UnionTypeDefinition type, final Object obj) {
226 final UnionTypeDefinition other = castIfEquals(UnionTypeDefinition.class, type, obj);
227 return other != null && type.getTypes().equals(other.getTypes());
230 static boolean equals(final UnsignedIntegerTypeDefinition type, final Object obj) {
235 final UnsignedIntegerTypeDefinition other = castIfEquals(UnsignedIntegerTypeDefinition.class, type, obj);
236 return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
239 static String toString(final BinaryTypeDefinition type) {
240 return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null)).toString();
243 static String toString(final BitsTypeDefinition type) {
244 return toStringHelper(type).add("bits", type.getBits()).toString();
247 static String toString(final BooleanTypeDefinition type) {
248 return toStringHelper(type).toString();
251 static String toString(final DecimalTypeDefinition type) {
252 return toStringHelper(type).add("fractionDigits", type.getFractionDigits())
253 .add("range", type.getRangeConstraint().orElse(null)).toString();
256 static String toString(final EmptyTypeDefinition type) {
257 return toStringHelper(type).toString();
260 static String toString(final EnumTypeDefinition type) {
261 return toStringHelper(type).add("values", type.getValues()).toString();
264 static String toString(final IdentityrefTypeDefinition type) {
265 return toStringHelper(type).add("identities", type.getIdentities()).toString();
268 static String toString(final InstanceIdentifierTypeDefinition type) {
269 return toStringHelper(type).add("requireInstance", type.requireInstance()).toString();
272 static String toString(final IntegerTypeDefinition type) {
273 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
276 static String toString(final LeafrefTypeDefinition type) {
277 return toStringHelper(type).add("pathStatement", type.getPathStatement()).toString();
280 static String toString(final StringTypeDefinition type) {
281 return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null))
282 .add("patterns", type.getPatternConstraints()).toString();
285 static String toString(final UnionTypeDefinition type) {
286 return toStringHelper(type).add("types", type.getTypes()).toString();
289 static String toString(final UnsignedIntegerTypeDefinition type) {
290 return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();