Bug 5200: Yang parser doesn't fill error-app-tag and error-message in constraints
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / RangeConstraintEffectiveImpl.java
1 /**
2  * Copyright (c) 2015 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.parser.stmt.rfc6020.effective.type;
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.yang.model.api.type.RangeConstraint;
14
15 public class RangeConstraintEffectiveImpl implements RangeConstraint {
16
17     public static final String DEFAULT_REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.2.4";
18     private final Number min;
19     private final Number max;
20
21     private final String description;
22     private final String reference;
23
24     private final String errorAppTag;
25     private final String errorMessage;
26
27     public RangeConstraintEffectiveImpl(final Number min, final Number max, final Optional<String> description,
28             final Optional<String> reference) {
29         this(min, max, description.orNull(), reference.orNull(), "range-out-of-specified-bounds",
30                 "The argument is out of bounds <" + min + ", " + max + ">");
31     }
32
33     public RangeConstraintEffectiveImpl(final Number min, final Number max, final String description,
34             final String reference, final String errorAppTag, final String errorMessage) {
35         super();
36         this.min = Preconditions.checkNotNull(min, "min must not be null.");
37         this.max = Preconditions.checkNotNull(max, "max must not be null.");
38         this.description = description;
39         this.reference = reference;
40         this.errorAppTag = errorAppTag != null ? errorAppTag : "range-out-of-specified-bounds";
41         this.errorMessage = errorMessage != null ? errorMessage : "The argument is out of bounds <" + min + ", " + max
42                 + ">";
43     }
44
45     @Override
46     public String getDescription() {
47         return description;
48     }
49
50     @Override
51     public String getErrorAppTag() {
52         return errorAppTag;
53     }
54
55     @Override
56     public String getErrorMessage() {
57         return errorMessage;
58     }
59
60     @Override
61     public String getReference() {
62         return reference;
63     }
64
65     @Override
66     public Number getMin() {
67         return min;
68     }
69
70     @Override
71     public Number getMax() {
72         return max;
73     }
74
75     @Override
76     public int hashCode() {
77         final int prime = 31;
78         int result = 1;
79         result = prime * result + Objects.hashCode(description);
80         result = prime * result + errorAppTag.hashCode();
81         result = prime * result + errorMessage.hashCode();
82         result = prime * result + max.hashCode();
83         result = prime * result + min.hashCode();
84         result = prime * result + Objects.hashCode(reference);
85         return result;
86     }
87
88     @Override
89     public boolean equals(final Object obj) {
90         if (this == obj) {
91             return true;
92         }
93         if (obj == null) {
94             return false;
95         }
96         if (getClass() != obj.getClass()) {
97             return false;
98         }
99         final RangeConstraintEffectiveImpl other = (RangeConstraintEffectiveImpl) obj;
100         if (!Objects.equals(description, other.description)) {
101             return false;
102         }
103         if (!Objects.equals(max, other.max)) {
104             return false;
105         }
106         if (!Objects.equals(min, other.min)) {
107             return false;
108         }
109         if (!Objects.equals(reference, other.reference)) {
110             return false;
111         }
112         return true;
113     }
114
115     @Override
116     public String toString() {
117         final StringBuilder builder = new StringBuilder();
118         builder.append(RangeConstraintEffectiveImpl.class.getSimpleName());
119         builder.append(" [min=");
120         builder.append(min);
121         builder.append(", max=");
122         builder.append(max);
123         builder.append(", description=");
124         builder.append(description);
125         builder.append(", reference=");
126         builder.append(reference);
127         builder.append(", errorAppTag=");
128         builder.append(errorAppTag);
129         builder.append(", errorMessage=");
130         builder.append(errorMessage);
131         builder.append("]");
132         return builder.toString();
133     }
134 }