4308ecb07b80cca83b9ac4f6762f04f65c31c1d4
[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
30         super();
31
32         this.min = Preconditions.checkNotNull(min, "min must not be null.");
33         this.max = Preconditions.checkNotNull(max, "max must not be null.");
34         this.description = description.orNull();
35         this.reference = reference.orNull();
36
37         this.errorAppTag = "range-out-of-specified-bounds";
38         this.errorMessage = "The argument is out of bounds <" + min + ", " + max + ">";
39     }
40
41     @Override
42     public String getDescription() {
43         return description;
44     }
45
46     @Override
47     public String getErrorAppTag() {
48         return errorAppTag;
49     }
50
51     @Override
52     public String getErrorMessage() {
53         return errorMessage;
54     }
55
56     @Override
57     public String getReference() {
58         return reference;
59     }
60
61     @Override
62     public Number getMin() {
63         return min;
64     }
65
66     @Override
67     public Number getMax() {
68         return max;
69     }
70
71     @Override
72     public int hashCode() {
73         final int prime = 31;
74         int result = 1;
75         result = prime * result + Objects.hashCode(description);
76         result = prime * result + errorAppTag.hashCode();
77         result = prime * result + errorMessage.hashCode();
78         result = prime * result + max.hashCode();
79         result = prime * result + min.hashCode();
80         result = prime * result + Objects.hashCode(reference);
81         return result;
82     }
83
84     @Override
85     public boolean equals(final Object obj) {
86         if (this == obj) {
87             return true;
88         }
89         if (obj == null) {
90             return false;
91         }
92         if (getClass() != obj.getClass()) {
93             return false;
94         }
95         final RangeConstraintEffectiveImpl other = (RangeConstraintEffectiveImpl) obj;
96         if (!Objects.equals(description, other.description)) {
97             return false;
98         }
99         if (!Objects.equals(max, other.max)) {
100             return false;
101         }
102         if (!Objects.equals(min, other.min)) {
103             return false;
104         }
105         if (!Objects.equals(reference, other.reference)) {
106             return false;
107         }
108         return true;
109     }
110
111     @Override
112     public String toString() {
113         final StringBuilder builder = new StringBuilder();
114         builder.append(RangeConstraintEffectiveImpl.class.getSimpleName());
115         builder.append(" [min=");
116         builder.append(min);
117         builder.append(", max=");
118         builder.append(max);
119         builder.append(", description=");
120         builder.append(description);
121         builder.append(", reference=");
122         builder.append(reference);
123         builder.append(", errorAppTag=");
124         builder.append(errorAppTag);
125         builder.append(", errorMessage=");
126         builder.append(errorMessage);
127         builder.append("]");
128         return builder.toString();
129     }
130 }