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