Bug 5200: Yang parser doesn't fill error-app-tag and error-message in constraints
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / PatternConstraintEffectiveImpl.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.PatternConstraint;
14
15 public class PatternConstraintEffectiveImpl implements PatternConstraint {
16
17     private final String regEx;
18     private final String description;
19     private final String reference;
20     private final String errorAppTag;
21     private final String errorMessage;
22
23     public PatternConstraintEffectiveImpl(final String regex, final Optional<String> description,
24             final Optional<String> reference) {
25         this(regex, description.orNull(), reference.orNull(), "invalid-regular-expression", String.format(
26                 "String %s is not valid regular expression.", regex));
27     }
28
29     public PatternConstraintEffectiveImpl(final String regex, final String description, final String reference,
30             final String errorAppTag, final String errorMessage) {
31         super();
32         this.regEx = Preconditions.checkNotNull(regex, "regex must not be null.");
33         this.description = description;
34         this.reference = reference;
35         this.errorAppTag = errorAppTag != null ? errorAppTag : "invalid-regular-expression";
36         this.errorMessage = errorMessage != null ? errorMessage : String.format(
37                 "String %s is not valid regular expression.", regex);
38     }
39
40     @Override
41     public String getRegularExpression() {
42         return regEx;
43     }
44
45     @Override
46     public String getDescription() {
47         return description;
48     }
49
50     @Override
51     public String getErrorAppTag() {
52         return errorAppTag;
53     }
54
55     @Override
56     public String getErrorMessage() {
57         return errorMessage;
58     }
59
60     @Override
61     public String getReference() {
62         return reference;
63     }
64
65     @Override
66     public int hashCode() {
67         final int prime = 31;
68         int result = 1;
69         result = prime * result + Objects.hashCode(description);
70         result = prime * result + Objects.hashCode(errorAppTag);
71         result = prime * result + Objects.hashCode(errorMessage);
72         result = prime * result + Objects.hashCode(reference);
73         result = prime * result + regEx.hashCode();
74         return result;
75     }
76
77     @Override
78     public boolean equals(final Object obj) {
79         if (this == obj) {
80             return true;
81         }
82         if (obj == null) {
83             return false;
84         }
85         if (getClass() != obj.getClass()) {
86             return false;
87         }
88         final PatternConstraintEffectiveImpl other = (PatternConstraintEffectiveImpl) obj;
89         if (!Objects.equals(description, other.description)) {
90             return false;
91         }
92         if (!Objects.equals(errorAppTag, other.errorAppTag)) {
93             return false;
94         }
95         if (!Objects.equals(errorMessage, other.errorMessage)) {
96             return false;
97         }
98         if (!Objects.equals(reference, other.reference)) {
99             return false;
100         }
101         if (!Objects.equals(regEx, other.regEx)) {
102             return false;
103         }
104         return true;
105     }
106
107     @Override
108     public String toString() {
109         StringBuilder builder = new StringBuilder();
110         builder.append(PatternConstraintEffectiveImpl.class.getSimpleName());
111         builder.append(" [regex=");
112         builder.append(regEx);
113         builder.append(", description=");
114         builder.append(description);
115         builder.append(", reference=");
116         builder.append(reference);
117         builder.append(", errorAppTag=");
118         builder.append(errorAppTag);
119         builder.append(", errorMessage=");
120         builder.append(errorMessage);
121         builder.append("]");
122         return builder.toString();
123     }
124 }