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