2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
17 * {@link Immutable} implementation of {@link PatternConstraint}
19 * Creates an instance of Range constraint based on supplied parameters with
20 * additional behaviour:
23 * <li>{@link PatternConstraint#getErrorAppTag()} returns
24 * <code>invalid-regular-expression</code>
28 final class PatternConstraintImpl implements PatternConstraint, Immutable {
30 private final String regex;
31 private final String description;
32 private final String reference;
34 private final String errorAppTag;
35 private final String errorMessage;
37 PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference) {
38 this(regex, description, reference, "invalid-regular-expression", String.format(
39 "String %s is not valid regular expression.", regex));
42 PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference,
43 final String errorAppTag, final String errorMessage) {
44 this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
45 this.description = description.orNull();
46 this.reference = reference.orNull();
47 this.errorAppTag = errorAppTag != null ? errorAppTag : "invalid-regular-expression";
48 this.errorMessage = errorMessage != null ? errorMessage : String.format(
49 "String %s is not valid regular expression.", regex);
53 public String getDescription() {
58 public String getErrorAppTag() {
63 public String getErrorMessage() {
68 public String getReference() {
73 public String getRegularExpression() {
78 public int hashCode() {
81 result = prime * result + Objects.hashCode(description);
82 result = prime * result + Objects.hashCode(errorAppTag);
83 result = prime * result + Objects.hashCode(errorMessage);
84 result = prime * result + Objects.hashCode(reference);
85 result = prime * result + regex.hashCode();
90 public boolean equals(final Object obj) {
97 if (getClass() != obj.getClass()) {
100 final PatternConstraintImpl other = (PatternConstraintImpl) obj;
101 if (!Objects.equals(description, other.description)) {
104 if (!Objects.equals(errorAppTag, other.errorAppTag)) {
107 if (!Objects.equals(errorMessage, other.errorMessage)) {
110 if (!Objects.equals(reference, other.reference)) {
113 if (!Objects.equals(regex, other.regex)) {
120 public String toString() {
121 StringBuilder builder = new StringBuilder();
122 builder.append("PatternConstraintImpl [regex=");
123 builder.append(regex);
124 builder.append(", description=");
125 builder.append(description);
126 builder.append(", reference=");
127 builder.append(reference);
128 builder.append(", errorAppTag=");
129 builder.append(errorAppTag);
130 builder.append(", errorMessage=");
131 builder.append(errorMessage);
133 return builder.toString();