Merge "Improved sorting of augmentations before code generation."
[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 org.opendaylight.yangtools.concepts.Immutable;
11 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
12
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15
16 /**
17  * {@link Immutable} implementation of {@link PatternConstraint}
18  *
19  * Creates an instance of Range constraint based on supplied parameters with
20  * additional behaviour:
21  *
22  * <ul>
23  * <li>{@link PatternConstraint#getErrorAppTag()} returns
24  * <code>invalid-regular-expression</code>
25  * </ul>
26  *
27  */
28 final class PatternConstraintImpl implements PatternConstraint, Immutable {
29
30     private final String regex;
31     private final String description;
32     private final String reference;
33
34     private final String errorAppTag;
35     private final String errorMessage;
36
37     public PatternConstraintImpl(final String regex, final Optional<String> description,
38             final Optional<String> reference) {
39         super();
40         this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
41         this.description = description.orNull();
42         this.reference = reference.orNull();
43
44         // FIXME: Lookup better suitable error tag.
45         errorAppTag = "invalid-regular-expression";
46         // TODO: add erro message
47         errorMessage = "";
48     }
49
50     @Override
51     public String getDescription() {
52         return description;
53     }
54
55     @Override
56     public String getErrorAppTag() {
57         return errorAppTag;
58     }
59
60     @Override
61     public String getErrorMessage() {
62         return errorMessage;
63     }
64
65     @Override
66     public String getReference() {
67         return reference;
68     }
69
70     @Override
71     public String getRegularExpression() {
72         return regex;
73     }
74
75     @Override
76     public int hashCode() {
77         final int prime = 31;
78         int result = 1;
79         result = prime * result + ((description == null) ? 0 : description.hashCode());
80         result = prime * result + ((errorAppTag == null) ? 0 : errorAppTag.hashCode());
81         result = prime * result + ((errorMessage == null) ? 0 : errorMessage.hashCode());
82         result = prime * result + ((reference == null) ? 0 : reference.hashCode());
83         result = prime * result + regex.hashCode();
84         return result;
85     }
86
87     @Override
88     public boolean equals(final Object obj) {
89         if (this == obj) {
90             return true;
91         }
92         if (obj == null) {
93             return false;
94         }
95         if (getClass() != obj.getClass()) {
96             return false;
97         }
98         final PatternConstraintImpl other = (PatternConstraintImpl) obj;
99         if (description == null) {
100             if (other.description != null) {
101                 return false;
102             }
103         } else if (!description.equals(other.description)) {
104             return false;
105         }
106         if (errorAppTag == null) {
107             if (other.errorAppTag != null) {
108                 return false;
109             }
110         } else if (!errorAppTag.equals(other.errorAppTag)) {
111             return false;
112         }
113         if (errorMessage == null) {
114             if (other.errorMessage != null) {
115                 return false;
116             }
117         } else if (!errorMessage.equals(other.errorMessage)) {
118             return false;
119         }
120         if (reference == null) {
121             if (other.reference != null) {
122                 return false;
123             }
124         } else if (!reference.equals(other.reference)) {
125             return false;
126         }
127         if (regex == null) {
128             if (other.regex != null) {
129                 return false;
130             }
131         } else if (!regex.equals(other.regex)) {
132             return false;
133         }
134         return true;
135     }
136
137     @Override
138     public String toString() {
139         StringBuilder builder = new StringBuilder();
140         builder.append("PatternConstraintImpl [regex=");
141         builder.append(regex);
142         builder.append(", description=");
143         builder.append(description);
144         builder.append(", reference=");
145         builder.append(reference);
146         builder.append(", errorAppTag=");
147         builder.append(errorAppTag);
148         builder.append(", errorMessage=");
149         builder.append(errorMessage);
150         builder.append("]");
151         return builder.toString();
152     }
153 }