Do not fake an errorMessage for pattern constraints
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / PatternConstraintImpl.java
index eecc2a01f5295f45bc14ed966c9b61740a94a50a..780a59f5c19abbc17f04b4d7bf3242ab1ef8a16e 100644 (file)
@@ -7,23 +7,24 @@
  */
 package org.opendaylight.yangtools.yang.model.util;
 
-import com.google.common.base.Optional;
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
 import java.util.Objects;
+import java.util.Optional;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.model.api.type.ModifierKind;
 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
 
 /**
- * {@link Immutable} implementation of {@link PatternConstraint}
+ * {@link Immutable} implementation of {@link PatternConstraint}.
  *
+ * <p>
  * Creates an instance of Range constraint based on supplied parameters with
  * additional behaviour:
- *
  * <ul>
  * <li>{@link PatternConstraint#getErrorAppTag()} returns
  * <code>invalid-regular-expression</code>
  * </ul>
- *
  */
 final class PatternConstraintImpl implements PatternConstraint, Immutable {
 
@@ -33,55 +34,60 @@ final class PatternConstraintImpl implements PatternConstraint, Immutable {
 
     private final String errorAppTag;
     private final String errorMessage;
+    private final ModifierKind modifier;
 
-    public PatternConstraintImpl(final String regex, final Optional<String> description,
-            final Optional<String> reference) {
-        super();
-        this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
-        this.description = description.orNull();
-        this.reference = reference.orNull();
+    PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference) {
+        this(regex, description, reference, null, null, Optional.empty());
+    }
 
-        // FIXME: Lookup better suitable error tag.
-        errorAppTag = "invalid-regular-expression";
-        // TODO: add erro message
-        errorMessage = "";
+    PatternConstraintImpl(final String regex, final Optional<String> description, final Optional<String> reference,
+            final String errorAppTag, final String errorMessage, final Optional<ModifierKind> modifier) {
+        this.regex = Preconditions.checkNotNull(regex, "regex must not be null.");
+        this.description = description.orElse(null);
+        this.reference = reference.orElse(null);
+        this.errorAppTag = errorAppTag != null ? errorAppTag : "invalid-regular-expression";
+        this.errorMessage = errorMessage;
+        this.modifier = modifier.orElse(null);
     }
 
     @Override
-    public String getDescription() {
-        return description;
+    public Optional<String> getDescription() {
+        return Optional.ofNullable(description);
     }
 
     @Override
-    public String getErrorAppTag() {
-        return errorAppTag;
+    public Optional<String> getErrorAppTag() {
+        return Optional.ofNullable(errorAppTag);
     }
 
     @Override
-    public String getErrorMessage() {
-        return errorMessage;
+    public Optional<String> getErrorMessage() {
+        return Optional.ofNullable(errorMessage);
     }
 
     @Override
-    public String getReference() {
-        return reference;
+    public Optional<String> getReference() {
+        return Optional.ofNullable(reference);
     }
 
     @Override
-    public String getRegularExpression() {
+    public String getJavaPatternString() {
         return regex;
     }
 
+    @Override
+    public String getRegularExpressionString() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Optional<ModifierKind> getModifier() {
+        return Optional.ofNullable(modifier);
+    }
+
     @Override
     public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        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;
+        return Objects.hash(description, errorAppTag, errorMessage, reference, regex, modifier);
     }
 
     @Override
@@ -89,45 +95,19 @@ final class PatternConstraintImpl implements PatternConstraint, Immutable {
         if (this == obj) {
             return true;
         }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
+        if (!(obj instanceof PatternConstraintImpl)) {
             return false;
         }
         final PatternConstraintImpl other = (PatternConstraintImpl) obj;
-        if (!Objects.equals(description, other.description)) {
-            return false;
-        }
-        if (!Objects.equals(errorAppTag, other.errorAppTag)) {
-            return false;
-        }
-        if (!Objects.equals(errorMessage, other.errorMessage)) {
-            return false;
-        }
-        if (!Objects.equals(reference, other.reference)) {
-            return false;
-        }
-        if (!Objects.equals(regex, other.regex)) {
-            return false;
-        }
-        return true;
+        return Objects.equals(description, other.description) && Objects.equals(errorAppTag, other.errorAppTag)
+                && Objects.equals(errorMessage, other.errorMessage) && Objects.equals(reference, other.reference)
+                && Objects.equals(regex, other.regex) && Objects.equals(modifier, other.modifier);
     }
 
     @Override
     public String toString() {
-        StringBuilder builder = new StringBuilder();
-        builder.append("PatternConstraintImpl [regex=");
-        builder.append(regex);
-        builder.append(", description=");
-        builder.append(description);
-        builder.append(", reference=");
-        builder.append(reference);
-        builder.append(", errorAppTag=");
-        builder.append(errorAppTag);
-        builder.append(", errorMessage=");
-        builder.append(errorMessage);
-        builder.append("]");
-        return builder.toString();
+        return MoreObjects.toStringHelper(this).omitNullValues().add("regex", regex).add("description", description)
+                .add("reference", reference).add("errorAppTag", errorAppTag).add("errorMessage", errorMessage)
+                .add("modifier", modifier).toString();
     }
-}
\ No newline at end of file
+}