Encapsulate regexes in a non-capturing group
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / PatternConstraintImpl.java
1 /*
2  * Copyright (c) 2014 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.model.util;
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.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
16 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
17
18 /**
19  * {@link Immutable} implementation of {@link PatternConstraint}
20  *
21  * Creates an instance of Range constraint based on supplied parameters with
22  * additional behaviour:
23  *
24  * <ul>
25  * <li>{@link PatternConstraint#getErrorAppTag()} returns
26  * <code>invalid-regular-expression</code>
27  * </ul>
28  *
29  */
30 final class PatternConstraintImpl implements PatternConstraint, Immutable {
31
32     private final String regex;
33     private final String description;
34     private final String reference;
35
36     private final String errorAppTag;
37     private final String errorMessage;
38     private final ModifierKind modifier;
39
40     PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference) {
41         this(regex, description, reference, null, null, Optional.absent());
42     }
43
44     PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference,
45             final String errorAppTag, final String errorMessage, final Optional<ModifierKind> modifier) {
46         this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
47         this.description = description.orNull();
48         this.reference = reference.orNull();
49         this.errorAppTag = errorAppTag != null ? errorAppTag : "invalid-regular-expression";
50         this.errorMessage = errorMessage != null ? errorMessage : String.format(
51                 "Supplied value does not match the regular expression %s.", regex);
52         this.modifier = modifier.orNull();
53     }
54
55     @Override
56     public String getDescription() {
57         return description;
58     }
59
60     @Override
61     public String getErrorAppTag() {
62         return errorAppTag;
63     }
64
65     @Override
66     public String getErrorMessage() {
67         return errorMessage;
68     }
69
70     @Override
71     public String getReference() {
72         return reference;
73     }
74
75     @Override
76     public String getRegularExpression() {
77         return regex;
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 instanceof PatternConstraintImpl)) {
91             return false;
92         }
93         final PatternConstraintImpl other = (PatternConstraintImpl) obj;
94         return Objects.equals(description, other.description) && Objects.equals(errorAppTag, other.errorAppTag)
95                 && Objects.equals(errorMessage, other.errorMessage) && Objects.equals(reference, other.reference)
96                 && Objects.equals(regex, other.regex) && Objects.equals(modifier, other.modifier);
97     }
98
99     @Override
100     public String toString() {
101         return MoreObjects.toStringHelper(this).add("regex", regex).add("description", description)
102                 .add("reference", reference).add("errorAppTag", errorAppTag).add("errorMessage", errorMessage)
103                 .add("modifier", modifier).toString();
104     }
105 }