44a4ed42e64a481a1551b5f0503499c14d17bcd0
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / 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.util.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.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;
27
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();
32     }
33
34     private static int basicHashCode(final TypeDefinition<?> type) {
35         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
36             type.getDefaultValue());
37     }
38
39     private static <T extends TypeDefinition<T>> T castIfEquals(final Class<T> clazz, final T type, final Object obj) {
40         if (!clazz.isInstance(obj)) {
41             return null;
42         }
43
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;
50     }
51
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());
61     }
62
63     static int hashCode(final BinaryTypeDefinition type) {
64         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
65             type.getDefaultValue(), type.getLengthConstraint());
66     }
67
68     static int hashCode(final BitsTypeDefinition type) {
69         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
70             type.getDefaultValue(), type.getBits());
71     }
72
73
74     static int hashCode(final BooleanTypeDefinition type) {
75         return basicHashCode(type);
76     }
77
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());
81     }
82
83     static int hashCode(final EmptyTypeDefinition type) {
84         return basicHashCode(type);
85     }
86
87     static int hashCode(final EnumTypeDefinition type) {
88         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
89             type.getDefaultValue(), type.getValues());
90     }
91
92     static int hashCode(final IdentityrefTypeDefinition type) {
93         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
94             type.getDefaultValue(), type.getIdentity());
95     }
96
97     static int hashCode(final InstanceIdentifierTypeDefinition type) {
98         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
99             type.getDefaultValue(), type.requireInstance());
100     }
101
102     static int hashCode(final IntegerTypeDefinition type) {
103         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
104             type.getDefaultValue(), type.getRangeConstraints());
105     }
106
107     static int hashCode(final LeafrefTypeDefinition type) {
108         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
109             type.getDefaultValue(), type.getPathStatement());
110     }
111
112     static int hashCode(final StringTypeDefinition type) {
113         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
114             type.getDefaultValue(), type.getLengthConstraint(), type.getPatternConstraints());
115     }
116
117     static int hashCode(final UnionTypeDefinition type) {
118         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
119             type.getDefaultValue(), type.getTypes());
120     }
121
122     static int hashCode(final UnsignedIntegerTypeDefinition type) {
123         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
124             type.getDefaultValue(), type.getRangeConstraints());
125     }
126
127     static boolean equals(final BinaryTypeDefinition type, final Object obj) {
128         if (type == obj) {
129             return true;
130         }
131
132         final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
133         return other != null && type.getLengthConstraint().equals(other.getLengthConstraint());
134     }
135
136     static boolean equals(final BitsTypeDefinition type, final Object obj) {
137         if (type == obj) {
138             return true;
139         }
140
141         final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
142         return other != null && type.getBits().equals(other.getBits());
143     }
144
145     static boolean equals(final BooleanTypeDefinition type, final Object obj) {
146         return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
147     }
148
149     static boolean equals(final DecimalTypeDefinition type, final Object obj) {
150         if (type == obj) {
151             return true;
152         }
153
154         final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
155         return other != null && type.getFractionDigits().equals(other.getFractionDigits())
156                 && type.getRangeConstraints().equals(other.getRangeConstraints());
157     }
158
159     static boolean equals(final EmptyTypeDefinition type, final Object obj) {
160         return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
161     }
162
163     static boolean equals(final EnumTypeDefinition type, final Object obj) {
164         if (type == obj) {
165             return true;
166         }
167
168         final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
169         return other != null && type.getValues().equals(other.getValues());
170     }
171
172     static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
173         if (type == obj) {
174             return true;
175         }
176
177         final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
178         return other != null && type.getIdentity().equals(other.getIdentity());
179     }
180
181     static boolean equals(final InstanceIdentifierTypeDefinition type, final Object obj) {
182         if (type == obj) {
183             return true;
184         }
185
186         final InstanceIdentifierTypeDefinition other = castIfEquals(InstanceIdentifierTypeDefinition.class, type, obj);
187         return other != null && type.requireInstance() == other.requireInstance();
188     }
189
190     static boolean equals(final IntegerTypeDefinition type, final Object obj) {
191         if (type == obj) {
192             return true;
193         }
194
195         final IntegerTypeDefinition other = castIfEquals(IntegerTypeDefinition.class, type, obj);
196         return other != null && type.getRangeConstraints().equals(other.getRangeConstraints());
197     }
198
199     static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
200         if (type == obj) {
201             return true;
202         }
203
204         final LeafrefTypeDefinition other = castIfEquals(LeafrefTypeDefinition.class, type, obj);
205         return other != null && type.getPathStatement().equals(other.getPathStatement());
206     }
207
208     static boolean equals(final StringTypeDefinition type, final Object obj) {
209         if (type == obj) {
210             return true;
211         }
212
213         final StringTypeDefinition other = castIfEquals(StringTypeDefinition.class, type, obj);
214         return other != null && type.getLengthConstraint().equals(other.getLengthConstraint())
215                 && type.getPatternConstraints().equals(other.getPatternConstraints());
216     }
217
218     static boolean equals(final UnionTypeDefinition type, final Object obj) {
219         if (type == obj) {
220             return true;
221         }
222
223         final UnionTypeDefinition other = castIfEquals(UnionTypeDefinition.class, type, obj);
224         return other != null && type.getTypes().equals(other.getTypes());
225     }
226
227     static boolean equals(final UnsignedIntegerTypeDefinition type, final Object obj) {
228         if (type == obj) {
229             return true;
230         }
231
232         final UnsignedIntegerTypeDefinition other = castIfEquals(UnsignedIntegerTypeDefinition.class, type, obj);
233         return other != null && type.getRangeConstraints().equals(other.getRangeConstraints());
234     }
235
236     static String toString(final BinaryTypeDefinition type) {
237         return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null)).toString();
238     }
239
240     static String toString(final BitsTypeDefinition type) {
241         return toStringHelper(type).add("bits", type.getBits()).toString();
242     }
243
244     static String toString(final BooleanTypeDefinition type) {
245         return toStringHelper(type).toString();
246     }
247
248     static String toString(final DecimalTypeDefinition type) {
249         return toStringHelper(type).add("fractionDigits", type.getFractionDigits())
250                 .add("range", type.getRangeConstraints()).toString();
251     }
252
253     static String toString(final EmptyTypeDefinition type) {
254         return toStringHelper(type).toString();
255     }
256
257     static String toString(final EnumTypeDefinition type) {
258         return toStringHelper(type).add("values", type.getValues()).toString();
259     }
260
261     static String toString(final IdentityrefTypeDefinition type) {
262         return toStringHelper(type).add("identity", type.getIdentity()).toString();
263     }
264
265     static String toString(final InstanceIdentifierTypeDefinition type) {
266         return toStringHelper(type).add("requireInstance", type.requireInstance()).toString();
267     }
268
269     static String toString(final IntegerTypeDefinition type) {
270         return toStringHelper(type).add("range", type.getRangeConstraints()).toString();
271     }
272
273     static String toString(final LeafrefTypeDefinition type) {
274         return toStringHelper(type).add("pathStatement", type.getPathStatement()).toString();
275     }
276
277     static String toString(final StringTypeDefinition type) {
278         return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null))
279                 .add("patterns", type.getPatternConstraints()).toString();
280     }
281
282     static String toString(final UnionTypeDefinition type) {
283         return toStringHelper(type).add("types", type.getTypes()).toString();
284     }
285
286     static String toString(final UnsignedIntegerTypeDefinition type) {
287         return toStringHelper(type).add("range", type.getRangeConstraints()).toString();
288     }
289 }