Eliminate (Unsigned)IntegerTypeDefinition
[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.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;
34
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();
39     }
40
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));
44     }
45
46     private static <T extends TypeDefinition<T>> T castIfEquals(final Class<T> clazz, final T type, final Object obj) {
47         if (!clazz.isInstance(obj)) {
48             return null;
49         }
50
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;
57     }
58
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));
68     }
69
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));
73     }
74
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());
78     }
79
80
81     static int hashCode(final BooleanTypeDefinition type) {
82         return basicHashCode(type);
83     }
84
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));
89     }
90
91     static int hashCode(final EmptyTypeDefinition type) {
92         return basicHashCode(type);
93     }
94
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());
99     }
100
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());
104     }
105
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());
109     }
110
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));
114     }
115
116     static int hashCode(final Int8TypeDefinition type) {
117         return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
118     }
119
120     static int hashCode(final Int16TypeDefinition type) {
121         return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
122     }
123
124     static int hashCode(final Int32TypeDefinition type) {
125         return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
126     }
127
128     static int hashCode(final Int64TypeDefinition type) {
129         return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
130     }
131
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());
135     }
136
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());
141     }
142
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());
146     }
147
148     static int hashCode(final Uint8TypeDefinition type) {
149         return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
150     }
151
152     static int hashCode(final Uint16TypeDefinition type) {
153         return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
154     }
155
156     static int hashCode(final Uint32TypeDefinition type) {
157         return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
158     }
159
160     static int hashCode(final Uint64TypeDefinition type) {
161         return hashCode((RangeRestrictedTypeDefinition<?, ?>) type);
162     }
163
164     static boolean equals(final BinaryTypeDefinition type, final Object obj) {
165         if (type == obj) {
166             return true;
167         }
168
169         final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
170         return other != null && type.getLengthConstraint().equals(other.getLengthConstraint());
171     }
172
173     static boolean equals(final BitsTypeDefinition type, final Object obj) {
174         if (type == obj) {
175             return true;
176         }
177
178         final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
179         return other != null && type.getBits().equals(other.getBits());
180     }
181
182     static boolean equals(final BooleanTypeDefinition type, final Object obj) {
183         return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
184     }
185
186     static boolean equals(final DecimalTypeDefinition type, final Object obj) {
187         if (type == obj) {
188             return true;
189         }
190
191         final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
192         return other != null && type.getFractionDigits() == other.getFractionDigits()
193                 && type.getRangeConstraint().equals(other.getRangeConstraint());
194     }
195
196     static boolean equals(final EmptyTypeDefinition type, final Object obj) {
197         return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
198     }
199
200     static boolean equals(final EnumTypeDefinition type, final Object obj) {
201         if (type == obj) {
202             return true;
203         }
204
205         final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
206         return other != null && type.getValues().equals(other.getValues());
207     }
208
209     static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
210         if (type == obj) {
211             return true;
212         }
213
214         final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
215         return other != null && type.getIdentities().equals(other.getIdentities());
216     }
217
218     static boolean equals(final InstanceIdentifierTypeDefinition type, final Object obj) {
219         if (type == obj) {
220             return true;
221         }
222
223         final InstanceIdentifierTypeDefinition other = castIfEquals(InstanceIdentifierTypeDefinition.class, type, obj);
224         return other != null && type.requireInstance() == other.requireInstance();
225     }
226
227     static boolean equals(final Int8TypeDefinition type, final Object obj) {
228         if (type == obj) {
229             return true;
230         }
231
232         final Int8TypeDefinition other = castIfEquals(Int8TypeDefinition.class, type, obj);
233         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
234     }
235
236     static boolean equals(final Int16TypeDefinition type, final Object obj) {
237         if (type == obj) {
238             return true;
239         }
240
241         final Int16TypeDefinition other = castIfEquals(Int16TypeDefinition.class, type, obj);
242         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
243     }
244
245     static boolean equals(final Int32TypeDefinition type, final Object obj) {
246         if (type == obj) {
247             return true;
248         }
249
250         final Int32TypeDefinition other = castIfEquals(Int32TypeDefinition.class, type, obj);
251         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
252     }
253
254     static boolean equals(final Int64TypeDefinition type, final Object obj) {
255         if (type == obj) {
256             return true;
257         }
258
259         final Int64TypeDefinition other = castIfEquals(Int64TypeDefinition.class, type, obj);
260         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
261     }
262
263     static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
264         if (type == obj) {
265             return true;
266         }
267
268         final LeafrefTypeDefinition other = castIfEquals(LeafrefTypeDefinition.class, type, obj);
269         return other != null && type.getPathStatement().equals(other.getPathStatement());
270     }
271
272     static boolean equals(final StringTypeDefinition type, final Object obj) {
273         if (type == obj) {
274             return true;
275         }
276
277         final StringTypeDefinition other = castIfEquals(StringTypeDefinition.class, type, obj);
278         return other != null && type.getLengthConstraint().equals(other.getLengthConstraint())
279                 && type.getPatternConstraints().equals(other.getPatternConstraints());
280     }
281
282     static boolean equals(final UnionTypeDefinition type, final Object obj) {
283         if (type == obj) {
284             return true;
285         }
286
287         final UnionTypeDefinition other = castIfEquals(UnionTypeDefinition.class, type, obj);
288         return other != null && type.getTypes().equals(other.getTypes());
289     }
290
291     static boolean equals(final Uint8TypeDefinition type, final Object obj) {
292         if (type == obj) {
293             return true;
294         }
295
296         final Uint8TypeDefinition other = castIfEquals(Uint8TypeDefinition.class, type, obj);
297         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
298     }
299
300     static boolean equals(final Uint16TypeDefinition type, final Object obj) {
301         if (type == obj) {
302             return true;
303         }
304
305         final Uint16TypeDefinition other = castIfEquals(Uint16TypeDefinition.class, type, obj);
306         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
307     }
308
309     static boolean equals(final Uint32TypeDefinition type, final Object obj) {
310         if (type == obj) {
311             return true;
312         }
313
314         final Uint32TypeDefinition other = castIfEquals(Uint32TypeDefinition.class, type, obj);
315         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
316     }
317
318     static boolean equals(final Uint64TypeDefinition type, final Object obj) {
319         if (type == obj) {
320             return true;
321         }
322
323         final Uint64TypeDefinition other = castIfEquals(Uint64TypeDefinition.class, type, obj);
324         return other != null && type.getRangeConstraint().equals(other.getRangeConstraint());
325     }
326
327     static String toString(final BinaryTypeDefinition type) {
328         return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null)).toString();
329     }
330
331     static String toString(final BitsTypeDefinition type) {
332         return toStringHelper(type).add("bits", type.getBits()).toString();
333     }
334
335     static String toString(final BooleanTypeDefinition type) {
336         return toStringHelper(type).toString();
337     }
338
339     static String toString(final DecimalTypeDefinition type) {
340         return toStringHelper(type).add("fractionDigits", type.getFractionDigits())
341                 .add("range", type.getRangeConstraint().orElse(null)).toString();
342     }
343
344     static String toString(final EmptyTypeDefinition type) {
345         return toStringHelper(type).toString();
346     }
347
348     static String toString(final EnumTypeDefinition type) {
349         return toStringHelper(type).add("values", type.getValues()).toString();
350     }
351
352     static String toString(final IdentityrefTypeDefinition type) {
353         return toStringHelper(type).add("identities", type.getIdentities()).toString();
354     }
355
356     static String toString(final InstanceIdentifierTypeDefinition type) {
357         return toStringHelper(type).add("requireInstance", type.requireInstance()).toString();
358     }
359
360     static String toString(final Int8TypeDefinition type) {
361         return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
362     }
363
364     static String toString(final Int16TypeDefinition type) {
365         return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
366     }
367
368     static String toString(final Int32TypeDefinition type) {
369         return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
370     }
371
372     static String toString(final Int64TypeDefinition type) {
373         return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
374     }
375
376     static String toString(final LeafrefTypeDefinition type) {
377         return toStringHelper(type).add("pathStatement", type.getPathStatement()).toString();
378     }
379
380     static String toString(final StringTypeDefinition type) {
381         return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null))
382                 .add("patterns", type.getPatternConstraints()).toString();
383     }
384
385     static String toString(final UnionTypeDefinition type) {
386         return toStringHelper(type).add("types", type.getTypes()).toString();
387     }
388
389     static String toString(final Uint8TypeDefinition type) {
390         return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
391     }
392
393     static String toString(final Uint16TypeDefinition type) {
394         return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
395     }
396
397     static String toString(final Uint32TypeDefinition type) {
398         return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
399     }
400
401     static String toString(final Uint64TypeDefinition type) {
402         return toStringHelper(type).add("range", type.getRangeConstraint().orElse(null)).toString();
403     }
404
405 }