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