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