Merge branch 'master' of ../controller
[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 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.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
17 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
18
19 /**
20  * {@link Immutable} implementation of {@link PatternConstraint}.
21  *
22  * <p>
23  * Creates an instance of Range constraint based on supplied parameters with
24  * additional behaviour:
25  * <ul>
26  * <li>{@link PatternConstraint#getErrorAppTag()} returns
27  * <code>invalid-regular-expression</code>
28  * </ul>
29  */
30 final class PatternConstraintImpl implements PatternConstraint, Immutable {
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 = requireNonNull(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;
50         this.modifier = modifier.orElse(null);
51     }
52
53     @Override
54     public Optional<String> getDescription() {
55         return Optional.ofNullable(description);
56     }
57
58     @Override
59     public Optional<String> getErrorAppTag() {
60         return Optional.ofNullable(errorAppTag);
61     }
62
63     @Override
64     public Optional<String> getErrorMessage() {
65         return Optional.ofNullable(errorMessage);
66     }
67
68     @Override
69     public Optional<String> getReference() {
70         return Optional.ofNullable(reference);
71     }
72
73     @Override
74     public String getJavaPatternString() {
75         return regex;
76     }
77
78     @Override
79     public String getRegularExpressionString() {
80         throw new UnsupportedOperationException();
81     }
82
83     @Override
84     public Optional<ModifierKind> getModifier() {
85         return Optional.ofNullable(modifier);
86     }
87
88     @Override
89     public int hashCode() {
90         return Objects.hash(description, errorAppTag, errorMessage, reference, regex, modifier);
91     }
92
93     @Override
94     public boolean equals(final Object obj) {
95         if (this == obj) {
96             return true;
97         }
98         if (!(obj instanceof PatternConstraintImpl)) {
99             return false;
100         }
101         final PatternConstraintImpl other = (PatternConstraintImpl) obj;
102         return Objects.equals(description, other.description) && Objects.equals(errorAppTag, other.errorAppTag)
103                 && Objects.equals(errorMessage, other.errorMessage) && Objects.equals(reference, other.reference)
104                 && Objects.equals(regex, other.regex) && Objects.equals(modifier, other.modifier);
105     }
106
107     @Override
108     public String toString() {
109         return MoreObjects.toStringHelper(this).omitNullValues().add("regex", regex).add("description", description)
110                 .add("reference", reference).add("errorAppTag", errorAppTag).add("errorMessage", errorMessage)
111                 .add("modifier", modifier).toString();
112     }
113 }