Bug 5200: Yang parser doesn't fill error-app-tag and error-message in constraints
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / LengthConstraintImpl.java
1 /*
2  * Copyright (c) 2014 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.yangtools.yang.model.util;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
15
16 /**
17  * {@link Immutable} implementation of {@link LengthConstraint}.
18  *
19  * Length constraint based on supplied parameters with additional behaviour:
20  *
21  * <ul>
22  * <li>{@link LengthConstraint#getErrorAppTag()} returns
23  * <code>length-out-of-specified-bounds</code>
24  * <li>{@link LengthConstraint#getErrorMessage() returns <code>The argument is
25  * out of bounds &lt;<i>min</i>, <i>max</i> &gt;</code>
26  * </ul
27  */
28 final class LengthConstraintImpl implements LengthConstraint, Immutable {
29
30     private final Number min;
31     private final Number max;
32
33     private final String description;
34     private final String reference;
35
36     private final String errorAppTag;
37     private final String errorMessage;
38
39     LengthConstraintImpl(final Number min, final Number max, final Optional<String> description,
40             final Optional<String> reference) {
41         this(min, max, description, reference, "length-out-of-specified-bounds", "The argument is out of bounds <"
42                 + min + ", " + max + ">");
43     }
44
45     LengthConstraintImpl(final Number min, final Number max, final Optional<String> description,
46             final Optional<String> reference, final String errorAppTag, final String errorMessage) {
47         this.min = Preconditions.checkNotNull(min, "min must not be null.");
48         this.max = Preconditions.checkNotNull(max, "max must not be null");
49         this.description = description.orNull();
50         this.reference = reference.orNull();
51         this.errorAppTag = errorAppTag != null ? errorAppTag : "length-out-of-specified-bounds";
52         this.errorMessage = errorMessage != null ? errorMessage : "The argument is out of bounds <" + min + ", " + max
53                 + ">";
54     }
55
56     @Override
57     public String getDescription() {
58         return description;
59     }
60
61     @Override
62     public String getErrorAppTag() {
63         return errorAppTag;
64     }
65
66     @Override
67     public String getErrorMessage() {
68         return errorMessage;
69     }
70
71     @Override
72     public String getReference() {
73         return reference;
74     }
75
76     @Override
77     public Number getMin() {
78         return min;
79     }
80
81     @Override
82     public Number getMax() {
83         return max;
84     }
85
86     @Override
87     public int hashCode() {
88         final int prime = 31;
89         int result = 1;
90         result = prime * result + Objects.hashCode(description);
91         result = prime * result + errorAppTag.hashCode();
92         result = prime * result + errorMessage.hashCode();
93         result = prime * result + max.hashCode();
94         result = prime * result + min.hashCode();
95         result = prime * result + Objects.hashCode(reference);
96         return result;
97     }
98
99     @Override
100     public boolean equals(final Object obj) {
101         if (this == obj) {
102             return true;
103         }
104         if (obj == null) {
105             return false;
106         }
107         if (getClass() != obj.getClass()) {
108             return false;
109         }
110         final LengthConstraintImpl other = (LengthConstraintImpl) obj;
111         if (!Objects.equals(description, other.description)) {
112             return false;
113         }
114         if (!Objects.equals(errorAppTag, other.errorAppTag)) {
115             return false;
116         }
117         if (!Objects.equals(errorMessage, other.errorMessage)) {
118             return false;
119         }
120         if (max != other.max) {
121             return false;
122         }
123         if (min != other.min) {
124             return false;
125         }
126         if (!Objects.equals(reference, other.reference)) {
127             return false;
128         }
129         return true;
130     }
131
132     @Override
133     public String toString() {
134         StringBuilder builder = new StringBuilder();
135         builder.append("LengthConstraintImpl [min=");
136         builder.append(min);
137         builder.append(", max=");
138         builder.append(max);
139         builder.append(", description=");
140         builder.append(description);
141         builder.append(", errorAppTag=");
142         builder.append(errorAppTag);
143         builder.append(", reference=");
144         builder.append(reference);
145         builder.append(", errorMessage=");
146         builder.append(errorMessage);
147         builder.append("]");
148         return builder.toString();
149     }
150 }