dbc963ff522f07d00c184b0f6103eaab1f944a95
[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.MoreObjects;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.Objects;
14 import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
15 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
16
17 public class PatternConstraintEffectiveImpl implements PatternConstraint {
18
19     private final String regEx;
20     private final String rawRegEx;
21     private final String description;
22     private final String reference;
23     private final String errorAppTag;
24     private final String errorMessage;
25     private final ModifierKind modifier;
26
27     public PatternConstraintEffectiveImpl(final String regex, final String rawRegex,
28             final Optional<String> description, final Optional<String> reference) {
29         this(regex, rawRegex, description.orNull(), reference.orNull(), null, null, null);
30     }
31
32     public PatternConstraintEffectiveImpl(final String regex, final String rawRegex, final String description,
33             final String reference, final String errorAppTag, final String errorMessage, final ModifierKind modifier) {
34         this.regEx = Preconditions.checkNotNull(regex, "regex must not be null.");
35         this.rawRegEx = Preconditions.checkNotNull(rawRegex, "raw regex must not be null.");
36         this.description = description;
37         this.reference = reference;
38         this.errorAppTag = errorAppTag != null ? errorAppTag : "invalid-regular-expression";
39         this.errorMessage = errorMessage != null ? errorMessage : String.format(
40                 "Supplied value does not match the regular expression %s.", regex);
41         this.modifier = modifier;
42     }
43
44     @Override
45     public String getRegularExpression() {
46         return regEx;
47     }
48
49     @Override
50     public String getRawRegularExpression() {
51         return rawRegEx;
52     }
53
54     @Override
55     public String getDescription() {
56         return description;
57     }
58
59     @Override
60     public String getErrorAppTag() {
61         return errorAppTag;
62     }
63
64     @Override
65     public String getErrorMessage() {
66         return errorMessage;
67     }
68
69     @Override
70     public ModifierKind getModifier() {
71         return modifier;
72     }
73
74     @Override
75     public String getReference() {
76         return reference;
77     }
78
79     @Override
80     public int hashCode() {
81         return Objects.hash(description, errorAppTag, errorMessage, reference, regEx, modifier);
82     }
83
84     @Override
85     public boolean equals(final Object obj) {
86         if (this == obj) {
87             return true;
88         }
89         if (obj == null) {
90             return false;
91         }
92         if (getClass() != obj.getClass()) {
93             return false;
94         }
95         final PatternConstraintEffectiveImpl other = (PatternConstraintEffectiveImpl) obj;
96         return Objects.equals(description, other.description) && Objects.equals(errorAppTag, other.errorAppTag)
97                 && Objects.equals(errorMessage, other.errorMessage) && Objects.equals(reference, other.reference)
98                 && Objects.equals(regEx, other.regEx) && Objects.equals(modifier, other.modifier);
99     }
100
101     @Override
102     public String toString() {
103         return MoreObjects.toStringHelper(this).add("regex", regEx).add("description", description)
104                 .add("reference", reference).add("errorAppTag", errorAppTag).add("errorMessage", errorMessage)
105                 .add("modifier", modifier).toString();
106     }
107 }