ca75b0c6158a7c554aa09dc6f71d19f46934f90b
[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         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 = "length-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 + Objects.hashCode(description);
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 + Objects.hashCode(reference);
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 LengthConstraintImpl other = (LengthConstraintImpl) obj;
106         if (!Objects.equals(description, other.description)) {
107             return false;
108         }
109         if (!Objects.equals(errorAppTag, other.errorAppTag)) {
110             return false;
111         }
112         if (!Objects.equals(errorMessage, other.errorMessage)) {
113             return false;
114         }
115         if (max != other.max) {
116             return false;
117         }
118         if (min != other.min) {
119             return false;
120         }
121         if (!Objects.equals(reference, other.reference)) {
122             return false;
123         }
124         return true;
125     }
126
127     @Override
128     public String toString() {
129         StringBuilder builder = new StringBuilder();
130         builder.append("LengthConstraintImpl [min=");
131         builder.append(min);
132         builder.append(", max=");
133         builder.append(max);
134         builder.append(", description=");
135         builder.append(description);
136         builder.append(", errorAppTag=");
137         builder.append(errorAppTag);
138         builder.append(", reference=");
139         builder.append(reference);
140         builder.append(", errorMessage=");
141         builder.append(errorMessage);
142         builder.append("]");
143         return builder.toString();
144     }
145 }