Bug 5200: Yang parser doesn't fill error-app-tag and error-message in constraints
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / PatternConstraintImpl.java
index 8ab67a7636c02d72813cbc7dacea29b4de6fe2bc..cd0ca2cf2431afb1977e82ab1cf45ebcad300e10 100644 (file)
@@ -7,11 +7,11 @@
  */
 package org.opendaylight.yangtools.yang.model.util;
 
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
-
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import java.util.Objects;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
 
 /**
  * {@link Immutable} implementation of {@link PatternConstraint}
@@ -34,17 +34,19 @@ final class PatternConstraintImpl implements PatternConstraint, Immutable {
     private final String errorAppTag;
     private final String errorMessage;
 
-    public PatternConstraintImpl(final String regex, final Optional<String> description,
-            final Optional<String> reference) {
-        super();
+    PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference) {
+        this(regex, description, reference, "invalid-regular-expression", String.format(
+                "String %s is not valid regular expression.", regex));
+    }
+
+    PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference,
+            final String errorAppTag, final String errorMessage) {
         this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
         this.description = description.orNull();
         this.reference = reference.orNull();
-
-        // FIXME: Lookup better suitable error tag.
-        errorAppTag = "invalid-regular-expression";
-        // TODO: add erro message
-        errorMessage = "";
+        this.errorAppTag = errorAppTag != null ? errorAppTag : "invalid-regular-expression";
+        this.errorMessage = errorMessage != null ? errorMessage : String.format(
+                "String %s is not valid regular expression.", regex);
     }
 
     @Override
@@ -76,10 +78,10 @@ final class PatternConstraintImpl implements PatternConstraint, Immutable {
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((description == null) ? 0 : description.hashCode());
-        result = prime * result + ((errorAppTag == null) ? 0 : errorAppTag.hashCode());
-        result = prime * result + ((errorMessage == null) ? 0 : errorMessage.hashCode());
-        result = prime * result + ((reference == null) ? 0 : reference.hashCode());
+        result = prime * result + Objects.hashCode(description);
+        result = prime * result + Objects.hashCode(errorAppTag);
+        result = prime * result + Objects.hashCode(errorMessage);
+        result = prime * result + Objects.hashCode(reference);
         result = prime * result + regex.hashCode();
         return result;
     }
@@ -96,39 +98,19 @@ final class PatternConstraintImpl implements PatternConstraint, Immutable {
             return false;
         }
         final PatternConstraintImpl other = (PatternConstraintImpl) obj;
-        if (description == null) {
-            if (other.description != null) {
-                return false;
-            }
-        } else if (!description.equals(other.description)) {
+        if (!Objects.equals(description, other.description)) {
             return false;
         }
-        if (errorAppTag == null) {
-            if (other.errorAppTag != null) {
-                return false;
-            }
-        } else if (!errorAppTag.equals(other.errorAppTag)) {
+        if (!Objects.equals(errorAppTag, other.errorAppTag)) {
             return false;
         }
-        if (errorMessage == null) {
-            if (other.errorMessage != null) {
-                return false;
-            }
-        } else if (!errorMessage.equals(other.errorMessage)) {
+        if (!Objects.equals(errorMessage, other.errorMessage)) {
             return false;
         }
-        if (reference == null) {
-            if (other.reference != null) {
-                return false;
-            }
-        } else if (!reference.equals(other.reference)) {
+        if (!Objects.equals(reference, other.reference)) {
             return false;
         }
-        if (regex == null) {
-            if (other.regex != null) {
-                return false;
-            }
-        } else if (!regex.equals(other.regex)) {
+        if (!Objects.equals(regex, other.regex)) {
             return false;
         }
         return true;