eecc2a01f5295f45bc14ed966c9b61740a94a50a
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / PatternConstraintImpl.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.PatternConstraint;
15
16 /**
17  * {@link Immutable} implementation of {@link PatternConstraint}
18  *
19  * Creates an instance of Range constraint based on supplied parameters with
20  * additional behaviour:
21  *
22  * <ul>
23  * <li>{@link PatternConstraint#getErrorAppTag()} returns
24  * <code>invalid-regular-expression</code>
25  * </ul>
26  *
27  */
28 final class PatternConstraintImpl implements PatternConstraint, Immutable {
29
30     private final String regex;
31     private final String description;
32     private final String reference;
33
34     private final String errorAppTag;
35     private final String errorMessage;
36
37     public PatternConstraintImpl(final String regex, final Optional<String> description,
38             final Optional<String> reference) {
39         super();
40         this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
41         this.description = description.orNull();
42         this.reference = reference.orNull();
43
44         // FIXME: Lookup better suitable error tag.
45         errorAppTag = "invalid-regular-expression";
46         // TODO: add erro message
47         errorMessage = "";
48     }
49
50     @Override
51     public String getDescription() {
52         return description;
53     }
54
55     @Override
56     public String getErrorAppTag() {
57         return errorAppTag;
58     }
59
60     @Override
61     public String getErrorMessage() {
62         return errorMessage;
63     }
64
65     @Override
66     public String getReference() {
67         return reference;
68     }
69
70     @Override
71     public String getRegularExpression() {
72         return regex;
73     }
74
75     @Override
76     public int hashCode() {
77         final int prime = 31;
78         int result = 1;
79         result = prime * result + Objects.hashCode(description);
80         result = prime * result + Objects.hashCode(errorAppTag);
81         result = prime * result + Objects.hashCode(errorMessage);
82         result = prime * result + Objects.hashCode(reference);
83         result = prime * result + regex.hashCode();
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 PatternConstraintImpl other = (PatternConstraintImpl) obj;
99         if (!Objects.equals(description, other.description)) {
100             return false;
101         }
102         if (!Objects.equals(errorAppTag, other.errorAppTag)) {
103             return false;
104         }
105         if (!Objects.equals(errorMessage, other.errorMessage)) {
106             return false;
107         }
108         if (!Objects.equals(reference, other.reference)) {
109             return false;
110         }
111         if (!Objects.equals(regex, other.regex)) {
112             return false;
113         }
114         return true;
115     }
116
117     @Override
118     public String toString() {
119         StringBuilder builder = new StringBuilder();
120         builder.append("PatternConstraintImpl [regex=");
121         builder.append(regex);
122         builder.append(", description=");
123         builder.append(description);
124         builder.append(", reference=");
125         builder.append(reference);
126         builder.append(", errorAppTag=");
127         builder.append(errorAppTag);
128         builder.append(", errorMessage=");
129         builder.append(errorMessage);
130         builder.append("]");
131         return builder.toString();
132     }
133 }