Update TypeDefinition design
[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(),
36             type.getUnits().orElse(null), type.getDefaultValue().orElse(null));
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().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));
61     }
62
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));
66     }
67
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());
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(),
80             type.getUnits().orElse(null), type.getDefaultValue().orElse(null), type.getFractionDigits(),
81             type.getRangeConstraints());
82     }
83
84     static int hashCode(final EmptyTypeDefinition type) {
85         return basicHashCode(type);
86     }
87
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());
92     }
93
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());
97     }
98
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());
102     }
103
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.getRangeConstraints());
107     }
108
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());
112     }
113
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());
118     }
119
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());
123     }
124
125     static int hashCode(final UnsignedIntegerTypeDefinition type) {
126         return Objects.hash(type.getPath(), type.getUnknownSchemaNodes(), type.getBaseType(), type.getUnits(),
127             type.getDefaultValue(), type.getRangeConstraints());
128     }
129
130     static boolean equals(final BinaryTypeDefinition type, final Object obj) {
131         if (type == obj) {
132             return true;
133         }
134
135         final BinaryTypeDefinition other = castIfEquals(BinaryTypeDefinition.class, type, obj);
136         return other != null && type.getLengthConstraint().equals(other.getLengthConstraint());
137     }
138
139     static boolean equals(final BitsTypeDefinition type, final Object obj) {
140         if (type == obj) {
141             return true;
142         }
143
144         final BitsTypeDefinition other = castIfEquals(BitsTypeDefinition.class, type, obj);
145         return other != null && type.getBits().equals(other.getBits());
146     }
147
148     static boolean equals(final BooleanTypeDefinition type, final Object obj) {
149         return type == obj || castIfEquals(BooleanTypeDefinition.class, type, obj) != null;
150     }
151
152     static boolean equals(final DecimalTypeDefinition type, final Object obj) {
153         if (type == obj) {
154             return true;
155         }
156
157         final DecimalTypeDefinition other = castIfEquals(DecimalTypeDefinition.class, type, obj);
158         return other != null && type.getFractionDigits().equals(other.getFractionDigits())
159                 && type.getRangeConstraints().equals(other.getRangeConstraints());
160     }
161
162     static boolean equals(final EmptyTypeDefinition type, final Object obj) {
163         return type == obj || castIfEquals(EmptyTypeDefinition.class, type, obj) != null;
164     }
165
166     static boolean equals(final EnumTypeDefinition type, final Object obj) {
167         if (type == obj) {
168             return true;
169         }
170
171         final EnumTypeDefinition other = castIfEquals(EnumTypeDefinition.class, type, obj);
172         return other != null && type.getValues().equals(other.getValues());
173     }
174
175     static boolean equals(final IdentityrefTypeDefinition type, final Object obj) {
176         if (type == obj) {
177             return true;
178         }
179
180         final IdentityrefTypeDefinition other = castIfEquals(IdentityrefTypeDefinition.class, type, obj);
181         return other != null && type.getIdentity().equals(other.getIdentity());
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 boolean equals(final IntegerTypeDefinition type, final Object obj) {
194         if (type == obj) {
195             return true;
196         }
197
198         final IntegerTypeDefinition other = castIfEquals(IntegerTypeDefinition.class, type, obj);
199         return other != null && type.getRangeConstraints().equals(other.getRangeConstraints());
200     }
201
202     static boolean equals(final LeafrefTypeDefinition type, final Object obj) {
203         if (type == obj) {
204             return true;
205         }
206
207         final LeafrefTypeDefinition other = castIfEquals(LeafrefTypeDefinition.class, type, obj);
208         return other != null && type.getPathStatement().equals(other.getPathStatement());
209     }
210
211     static boolean equals(final StringTypeDefinition type, final Object obj) {
212         if (type == obj) {
213             return true;
214         }
215
216         final StringTypeDefinition other = castIfEquals(StringTypeDefinition.class, type, obj);
217         return other != null && type.getLengthConstraint().equals(other.getLengthConstraint())
218                 && type.getPatternConstraints().equals(other.getPatternConstraints());
219     }
220
221     static boolean equals(final UnionTypeDefinition type, final Object obj) {
222         if (type == obj) {
223             return true;
224         }
225
226         final UnionTypeDefinition other = castIfEquals(UnionTypeDefinition.class, type, obj);
227         return other != null && type.getTypes().equals(other.getTypes());
228     }
229
230     static boolean equals(final UnsignedIntegerTypeDefinition type, final Object obj) {
231         if (type == obj) {
232             return true;
233         }
234
235         final UnsignedIntegerTypeDefinition other = castIfEquals(UnsignedIntegerTypeDefinition.class, type, obj);
236         return other != null && type.getRangeConstraints().equals(other.getRangeConstraints());
237     }
238
239     static String toString(final BinaryTypeDefinition type) {
240         return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null)).toString();
241     }
242
243     static String toString(final BitsTypeDefinition type) {
244         return toStringHelper(type).add("bits", type.getBits()).toString();
245     }
246
247     static String toString(final BooleanTypeDefinition type) {
248         return toStringHelper(type).toString();
249     }
250
251     static String toString(final DecimalTypeDefinition type) {
252         return toStringHelper(type).add("fractionDigits", type.getFractionDigits())
253                 .add("range", type.getRangeConstraints()).toString();
254     }
255
256     static String toString(final EmptyTypeDefinition type) {
257         return toStringHelper(type).toString();
258     }
259
260     static String toString(final EnumTypeDefinition type) {
261         return toStringHelper(type).add("values", type.getValues()).toString();
262     }
263
264     static String toString(final IdentityrefTypeDefinition type) {
265         return toStringHelper(type).add("identities", type.getIdentities()).toString();
266     }
267
268     static String toString(final InstanceIdentifierTypeDefinition type) {
269         return toStringHelper(type).add("requireInstance", type.requireInstance()).toString();
270     }
271
272     static String toString(final IntegerTypeDefinition type) {
273         return toStringHelper(type).add("range", type.getRangeConstraints()).toString();
274     }
275
276     static String toString(final LeafrefTypeDefinition type) {
277         return toStringHelper(type).add("pathStatement", type.getPathStatement()).toString();
278     }
279
280     static String toString(final StringTypeDefinition type) {
281         return toStringHelper(type).add("length", type.getLengthConstraint().orElse(null))
282                 .add("patterns", type.getPatternConstraints()).toString();
283     }
284
285     static String toString(final UnionTypeDefinition type) {
286         return toStringHelper(type).add("types", type.getTypes()).toString();
287     }
288
289     static String toString(final UnsignedIntegerTypeDefinition type) {
290         return toStringHelper(type).add("range", type.getRangeConstraints()).toString();
291     }
292 }