Bug 5200: Yang parser doesn't fill error-app-tag and error-message in constraints
[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     PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference) {
38         this(regex, description, reference, "invalid-regular-expression", String.format(
39                 "String %s is not valid regular expression.", regex));
40     }
41
42     PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference,
43             final String errorAppTag, final String errorMessage) {
44         this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
45         this.description = description.orNull();
46         this.reference = reference.orNull();
47         this.errorAppTag = errorAppTag != null ? errorAppTag : "invalid-regular-expression";
48         this.errorMessage = errorMessage != null ? errorMessage : String.format(
49                 "String %s is not valid regular expression.", regex);
50     }
51
52     @Override
53     public String getDescription() {
54         return description;
55     }
56
57     @Override
58     public String getErrorAppTag() {
59         return errorAppTag;
60     }
61
62     @Override
63     public String getErrorMessage() {
64         return errorMessage;
65     }
66
67     @Override
68     public String getReference() {
69         return reference;
70     }
71
72     @Override
73     public String getRegularExpression() {
74         return regex;
75     }
76
77     @Override
78     public int hashCode() {
79         final int prime = 31;
80         int result = 1;
81         result = prime * result + Objects.hashCode(description);
82         result = prime * result + Objects.hashCode(errorAppTag);
83         result = prime * result + Objects.hashCode(errorMessage);
84         result = prime * result + Objects.hashCode(reference);
85         result = prime * result + regex.hashCode();
86         return result;
87     }
88
89     @Override
90     public boolean equals(final Object obj) {
91         if (this == obj) {
92             return true;
93         }
94         if (obj == null) {
95             return false;
96         }
97         if (getClass() != obj.getClass()) {
98             return false;
99         }
100         final PatternConstraintImpl other = (PatternConstraintImpl) obj;
101         if (!Objects.equals(description, other.description)) {
102             return false;
103         }
104         if (!Objects.equals(errorAppTag, other.errorAppTag)) {
105             return false;
106         }
107         if (!Objects.equals(errorMessage, other.errorMessage)) {
108             return false;
109         }
110         if (!Objects.equals(reference, other.reference)) {
111             return false;
112         }
113         if (!Objects.equals(regex, other.regex)) {
114             return false;
115         }
116         return true;
117     }
118
119     @Override
120     public String toString() {
121         StringBuilder builder = new StringBuilder();
122         builder.append("PatternConstraintImpl [regex=");
123         builder.append(regex);
124         builder.append(", description=");
125         builder.append(description);
126         builder.append(", reference=");
127         builder.append(reference);
128         builder.append(", errorAppTag=");
129         builder.append(errorAppTag);
130         builder.append(", errorMessage=");
131         builder.append(errorMessage);
132         builder.append("]");
133         return builder.toString();
134     }
135 }