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