Cleanup DocumentedNode
[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.Preconditions;
12 import java.util.Objects;
13 import java.util.Optional;
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  * <p>
22  * Creates an instance of Range constraint based on supplied parameters with
23  * additional behaviour:
24  * <ul>
25  * <li>{@link PatternConstraint#getErrorAppTag()} returns
26  * <code>invalid-regular-expression</code>
27  * </ul>
28  */
29 final class PatternConstraintImpl implements PatternConstraint, Immutable {
30
31     private final String regex;
32     private final String description;
33     private final String reference;
34
35     private final String errorAppTag;
36     private final String errorMessage;
37     private final ModifierKind modifier;
38
39     PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference) {
40         this(regex, description, reference, null, null, Optional.empty());
41     }
42
43     PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference,
44             final String errorAppTag, final String errorMessage, final Optional<ModifierKind> modifier) {
45         this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
46         this.description = description.orElse(null);
47         this.reference = reference.orElse(null);
48         this.errorAppTag = errorAppTag != null ? errorAppTag : "invalid-regular-expression";
49         this.errorMessage = errorMessage != null ? errorMessage : String.format(
50                 "Supplied value does not match the regular expression %s.", regex);
51         this.modifier = modifier.orElse(null);
52     }
53
54     @Override
55     public Optional<String> getDescription() {
56         return Optional.ofNullable(description);
57     }
58
59     @Override
60     public Optional<String> getErrorAppTag() {
61         return Optional.ofNullable(errorAppTag);
62     }
63
64     @Override
65     public Optional<String> getErrorMessage() {
66         return Optional.ofNullable(errorMessage);
67     }
68
69     @Override
70     public Optional<String> getReference() {
71         return Optional.ofNullable(reference);
72     }
73
74     @Override
75     public String getRegularExpression() {
76         return regex;
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 instanceof PatternConstraintImpl)) {
90             return false;
91         }
92         final PatternConstraintImpl other = (PatternConstraintImpl) obj;
93         return Objects.equals(description, other.description) && Objects.equals(errorAppTag, other.errorAppTag)
94                 && Objects.equals(errorMessage, other.errorMessage) && Objects.equals(reference, other.reference)
95                 && Objects.equals(regex, other.regex) && Objects.equals(modifier, other.modifier);
96     }
97
98     @Override
99     public String toString() {
100         return MoreObjects.toStringHelper(this).add("regex", regex).add("description", description)
101                 .add("reference", reference).add("errorAppTag", errorAppTag).add("errorMessage", errorMessage)
102                 .add("modifier", modifier).toString();
103     }
104 }