feca985796763c8c220fd31cc764e660f788eba2
[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  * Length constraint based on supplied parameters with additional behaviour:
19  *
20  * <ul>
21  * <li>{@link LengthConstraint#getErrorAppTag()} returns
22  * <code>length-out-of-specified-bounds</code>
23  * <li>{@link LengthConstraint#getErrorMessage()} returns <code>The argument is
24  * out of bounds &lt;<i>min</i>, <i>max</i> &gt;</code>
25  * </ul>
26  */
27 final class LengthConstraintImpl implements LengthConstraint, Immutable {
28
29     private final Number min;
30     private final Number max;
31
32     private final String description;
33     private final String reference;
34
35     private final String errorAppTag;
36     private final String errorMessage;
37
38     LengthConstraintImpl(final Number min, final Number max, final Optional<String> description,
39             final Optional<String> reference) {
40         this(min, max, description, reference, "length-out-of-specified-bounds", "The argument is out of bounds <"
41                 + min + ", " + max + ">");
42     }
43
44     LengthConstraintImpl(final Number min, final Number max, final Optional<String> description,
45             final Optional<String> reference, final String errorAppTag, final String errorMessage) {
46         this.min = Preconditions.checkNotNull(min, "min must not be null.");
47         this.max = Preconditions.checkNotNull(max, "max must not be null");
48         this.description = description.orNull();
49         this.reference = reference.orNull();
50         this.errorAppTag = errorAppTag != null ? errorAppTag : "length-out-of-specified-bounds";
51         this.errorMessage = errorMessage != null ? errorMessage : "The argument is out of bounds <" + min + ", " + max
52                 + ">";
53     }
54
55     @Override
56     public String getDescription() {
57         return description;
58     }
59
60     @Override
61     public String getErrorAppTag() {
62         return errorAppTag;
63     }
64
65     @Override
66     public String getErrorMessage() {
67         return errorMessage;
68     }
69
70     @Override
71     public String getReference() {
72         return reference;
73     }
74
75     @Override
76     public Number getMin() {
77         return min;
78     }
79
80     @Override
81     public Number getMax() {
82         return max;
83     }
84
85     @Override
86     public int hashCode() {
87         final int prime = 31;
88         int result = 1;
89         result = prime * result + Objects.hashCode(description);
90         result = prime * result + errorAppTag.hashCode();
91         result = prime * result + errorMessage.hashCode();
92         result = prime * result + max.hashCode();
93         result = prime * result + min.hashCode();
94         result = prime * result + Objects.hashCode(reference);
95         return result;
96     }
97
98     @Override
99     public boolean equals(final Object obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (obj == null) {
104             return false;
105         }
106         if (getClass() != obj.getClass()) {
107             return false;
108         }
109         final LengthConstraintImpl other = (LengthConstraintImpl) obj;
110         if (!Objects.equals(description, other.description)) {
111             return false;
112         }
113         if (!Objects.equals(errorAppTag, other.errorAppTag)) {
114             return false;
115         }
116         if (!Objects.equals(errorMessage, other.errorMessage)) {
117             return false;
118         }
119         if (max != other.max) {
120             return false;
121         }
122         if (min != other.min) {
123             return false;
124         }
125         if (!Objects.equals(reference, other.reference)) {
126             return false;
127         }
128         return true;
129     }
130
131     @Override
132     public String toString() {
133         return "LengthConstraintImpl [min=" + min
134             + ", max=" + max
135             + ", description=" + description
136             + ", errorAppTag=" + errorAppTag
137             + ", reference=" + reference
138             + ", errorMessage=" + errorMessage
139             + "]";
140     }
141 }