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