Merge "Fix for messags at the boot up time This commit proposes following two sets...
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / BaseConstraints.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.yang.model.util;
9
10 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
11 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
12 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
13
14 public final class BaseConstraints {
15
16     private BaseConstraints() {
17     }
18
19     public static LengthConstraint lengthConstraint(final long min,
20             final long max, final String description, final String reference) {
21         return new LengthConstraintImpl(min, max, description, reference);
22     }
23
24     public static RangeConstraint rangeConstraint(final Number min,
25             final Number max, final String description, final String reference) {
26         return new RangeConstraintImpl(min, max, description, reference);
27     }
28
29     public static PatternConstraint patternConstraint(final String pattern,
30             final String description, final String reference) {
31         return new PatternConstraintImpl(pattern, description, reference);
32     }
33
34     private static final class LengthConstraintImpl implements LengthConstraint {
35
36         private final long min;
37         private final long max;
38
39         private final String description;
40         private final String reference;
41
42         private final String errorAppTag;
43         private final String errorMessage;
44
45         public LengthConstraintImpl(long min, long max,
46                 final String description, final String reference) {
47             super();
48             this.min = min;
49             this.max = max;
50             this.description = description;
51             this.reference = reference;
52
53             this.errorAppTag = "length-out-of-specified-bounds";
54             this.errorMessage = "The argument is out of bounds <" + min + ", "
55                     + max + ">";
56         }
57
58         @Override
59         public String getDescription() {
60             return description;
61         }
62
63         @Override
64         public String getErrorAppTag() {
65             return errorAppTag;
66         }
67
68         @Override
69         public String getErrorMessage() {
70             return errorMessage;
71         }
72
73         @Override
74         public String getReference() {
75             return reference;
76         }
77
78         @Override
79         public Long getMin() {
80             return min;
81         }
82
83         @Override
84         public Long getMax() {
85             return max;
86         }
87
88         @Override
89         public int hashCode() {
90             final int prime = 31;
91             int result = 1;
92             result = prime * result
93                     + ((description == null) ? 0 : description.hashCode());
94             result = prime * result
95                     + ((errorAppTag == null) ? 0 : errorAppTag.hashCode());
96             result = prime * result
97                     + ((errorMessage == null) ? 0 : errorMessage.hashCode());
98             result = prime * result + (int) (max ^ (max >>> 32));
99             result = prime * result + (int) (min ^ (min >>> 32));
100             result = prime * result
101                     + ((reference == null) ? 0 : reference.hashCode());
102             return result;
103         }
104
105         @Override
106         public boolean equals(final Object obj) {
107             if (this == obj) {
108                 return true;
109             }
110             if (obj == null) {
111                 return false;
112             }
113             if (getClass() != obj.getClass()) {
114                 return false;
115             }
116             final LengthConstraintImpl other = (LengthConstraintImpl) obj;
117             if (description == null) {
118                 if (other.description != null) {
119                     return false;
120                 }
121             } else if (!description.equals(other.description)) {
122                 return false;
123             }
124             if (errorAppTag == null) {
125                 if (other.errorAppTag != null) {
126                     return false;
127                 }
128             } else if (!errorAppTag.equals(other.errorAppTag)) {
129                 return false;
130             }
131             if (errorMessage == null) {
132                 if (other.errorMessage != null) {
133                     return false;
134                 }
135             } else if (!errorMessage.equals(other.errorMessage)) {
136                 return false;
137             }
138             if (max != other.max) {
139                 return false;
140             }
141             if (min != other.min) {
142                 return false;
143             }
144             if (reference == null) {
145                 if (other.reference != null) {
146                     return false;
147                 }
148             } else if (!reference.equals(other.reference)) {
149                 return false;
150             }
151             return true;
152         }
153
154         @Override
155         public String toString() {
156             StringBuilder builder = new StringBuilder();
157             builder.append("LengthConstraintImpl [min=");
158             builder.append(min);
159             builder.append(", max=");
160             builder.append(max);
161             builder.append(", description=");
162             builder.append(description);
163             builder.append(", errorAppTag=");
164             builder.append(errorAppTag);
165             builder.append(", reference=");
166             builder.append(reference);
167             builder.append(", errorMessage=");
168             builder.append(errorMessage);
169             builder.append("]");
170             return builder.toString();
171         }
172     }
173
174     private final static class RangeConstraintImpl implements RangeConstraint {
175         private final Number min;
176         private final Number max;
177
178         private final String description;
179         private final String reference;
180
181         private final String errorAppTag;
182         private final String errorMessage;
183
184         public RangeConstraintImpl(Number min, Number max, String description,
185                 String reference) {
186             super();
187             this.min = min;
188             this.max = max;
189             this.description = description;
190             this.reference = reference;
191
192             this.errorAppTag = "range-out-of-specified-bounds";
193             this.errorMessage = "The argument is out of bounds <" + min + ", "
194                     + max + ">";
195         }
196
197         @Override
198         public String getDescription() {
199             return description;
200         }
201
202         @Override
203         public String getErrorAppTag() {
204             return errorAppTag;
205         }
206
207         @Override
208         public String getErrorMessage() {
209             return errorMessage;
210         }
211
212         @Override
213         public String getReference() {
214             return reference;
215         }
216
217         @Override
218         public Number getMin() {
219             return min;
220         }
221
222         @Override
223         public Number getMax() {
224             return max;
225         }
226
227         @Override
228         public int hashCode() {
229             final int prime = 31;
230             int result = 1;
231             result = prime * result
232                     + ((description == null) ? 0 : description.hashCode());
233             result = prime * result
234                     + ((errorAppTag == null) ? 0 : errorAppTag.hashCode());
235             result = prime * result
236                     + ((errorMessage == null) ? 0 : errorMessage.hashCode());
237             result = prime * result + ((max == null) ? 0 : max.hashCode());
238             result = prime * result + ((min == null) ? 0 : min.hashCode());
239             result = prime * result
240                     + ((reference == null) ? 0 : reference.hashCode());
241             return result;
242         }
243
244         @Override
245         public boolean equals(final Object obj) {
246             if (this == obj) {
247                 return true;
248             }
249             if (obj == null) {
250                 return false;
251             }
252             if (getClass() != obj.getClass()) {
253                 return false;
254             }
255             final RangeConstraintImpl other = (RangeConstraintImpl) obj;
256             if (description == null) {
257                 if (other.description != null) {
258                     return false;
259                 }
260             } else if (!description.equals(other.description)) {
261                 return false;
262             }
263             if (errorAppTag == null) {
264                 if (other.errorAppTag != null) {
265                     return false;
266                 }
267             } else if (!errorAppTag.equals(other.errorAppTag)) {
268                 return false;
269             }
270             if (errorMessage == null) {
271                 if (other.errorMessage != null) {
272                     return false;
273                 }
274             } else if (!errorMessage.equals(other.errorMessage)) {
275                 return false;
276             }
277             if (max == null) {
278                 if (other.max != null) {
279                     return false;
280                 }
281             } else if (!max.equals(other.max)) {
282                 return false;
283             }
284             if (min == null) {
285                 if (other.min != null) {
286                     return false;
287                 }
288             } else if (!min.equals(other.min)) {
289                 return false;
290             }
291             if (reference == null) {
292                 if (other.reference != null) {
293                     return false;
294                 }
295             } else if (!reference.equals(other.reference)) {
296                 return false;
297             }
298             return true;
299         }
300
301         @Override
302         public String toString() {
303             final StringBuilder builder = new StringBuilder();
304             builder.append("RangeConstraintImpl [min=");
305             builder.append(min);
306             builder.append(", max=");
307             builder.append(max);
308             builder.append(", description=");
309             builder.append(description);
310             builder.append(", reference=");
311             builder.append(reference);
312             builder.append(", errorAppTag=");
313             builder.append(errorAppTag);
314             builder.append(", errorMessage=");
315             builder.append(errorMessage);
316             builder.append("]");
317             return builder.toString();
318         }
319     }
320
321     private final static class PatternConstraintImpl implements
322             PatternConstraint {
323
324         private final String regex;
325         private final String description;
326         private final String reference;
327
328         private final String errorAppTag;
329         private final String errorMessage;
330
331         public PatternConstraintImpl(final String regex,
332                 final String description, final String reference) {
333             super();
334             this.regex = regex;
335             this.description = description;
336             this.reference = reference;
337
338             errorAppTag = "invalid-regular-expression";
339             // TODO: add erro message
340             errorMessage = "";
341         }
342
343         @Override
344         public String getDescription() {
345             return description;
346         }
347
348         @Override
349         public String getErrorAppTag() {
350             return errorAppTag;
351         }
352
353         @Override
354         public String getErrorMessage() {
355             return errorMessage;
356         }
357
358         @Override
359         public String getReference() {
360             return reference;
361         }
362
363         @Override
364         public String getRegularExpression() {
365             return regex;
366         }
367
368         @Override
369         public int hashCode() {
370             final int prime = 31;
371             int result = 1;
372             result = prime * result
373                     + ((description == null) ? 0 : description.hashCode());
374             result = prime * result
375                     + ((errorAppTag == null) ? 0 : errorAppTag.hashCode());
376             result = prime * result
377                     + ((errorMessage == null) ? 0 : errorMessage.hashCode());
378             result = prime * result
379                     + ((reference == null) ? 0 : reference.hashCode());
380             result = prime * result + ((regex == null) ? 0 : regex.hashCode());
381             return result;
382         }
383
384         @Override
385         public boolean equals(final Object obj) {
386             if (this == obj) {
387                 return true;
388             }
389             if (obj == null) {
390                 return false;
391             }
392             if (getClass() != obj.getClass()) {
393                 return false;
394             }
395             final PatternConstraintImpl other = (PatternConstraintImpl) obj;
396             if (description == null) {
397                 if (other.description != null) {
398                     return false;
399                 }
400             } else if (!description.equals(other.description)) {
401                 return false;
402             }
403             if (errorAppTag == null) {
404                 if (other.errorAppTag != null) {
405                     return false;
406                 }
407             } else if (!errorAppTag.equals(other.errorAppTag)) {
408                 return false;
409             }
410             if (errorMessage == null) {
411                 if (other.errorMessage != null) {
412                     return false;
413                 }
414             } else if (!errorMessage.equals(other.errorMessage)) {
415                 return false;
416             }
417             if (reference == null) {
418                 if (other.reference != null) {
419                     return false;
420                 }
421             } else if (!reference.equals(other.reference)) {
422                 return false;
423             }
424             if (regex == null) {
425                 if (other.regex != null) {
426                     return false;
427                 }
428             } else if (!regex.equals(other.regex)) {
429                 return false;
430             }
431             return true;
432         }
433
434         @Override
435         public String toString() {
436             StringBuilder builder = new StringBuilder();
437             builder.append("PatternConstraintImpl [regex=");
438             builder.append(regex);
439             builder.append(", description=");
440             builder.append(description);
441             builder.append(", reference=");
442             builder.append(reference);
443             builder.append(", errorAppTag=");
444             builder.append(errorAppTag);
445             builder.append(", errorMessage=");
446             builder.append(errorMessage);
447             builder.append("]");
448             return builder.toString();
449         }
450     }
451 }