BUG-4638: create a dedicated exception for range/length validation 59/29759/10
authorRobert Varga <rovarga@cisco.com>
Mon, 16 Nov 2015 15:37:37 +0000 (16:37 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 19 Nov 2015 13:10:04 +0000 (13:10 +0000)
Having only an IllegalArgumentException is not sufficient, create a pair
of new ones.

Change-Id: I90ac46ac55d9745651a8408398330a2a337d3f9a
Signed-off-by: Robert Varga <robert.varga@pantheon.sk>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/InvalidLengthConstraintException.java [new file with mode: 0644]
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/InvalidRangeConstraintException.java [new file with mode: 0644]
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/LengthRestrictedTypeBuilder.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/RangeRestrictedTypeBuilder.java

diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/InvalidLengthConstraintException.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/InvalidLengthConstraintException.java
new file mode 100644 (file)
index 0000000..0e8bc99
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2015 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.util.type;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
+
+@Beta
+public class InvalidLengthConstraintException extends IllegalArgumentException {
+    private static final long serialVersionUID = 1L;
+    private final LengthConstraint offendingConstraint;
+
+    protected InvalidLengthConstraintException(final LengthConstraint offendingConstraint, final String message) {
+        super(message);
+        this.offendingConstraint = Preconditions.checkNotNull(offendingConstraint);
+    }
+
+    public InvalidLengthConstraintException(final LengthConstraint offendingRange, final String format,
+            final Object... args) {
+        this(offendingRange, String.format(format, args));
+    }
+
+    public LengthConstraint getOffendingConstraint() {
+        return offendingConstraint;
+    }
+}
diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/InvalidRangeConstraintException.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/InvalidRangeConstraintException.java
new file mode 100644 (file)
index 0000000..5051f80
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2015 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.util.type;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
+
+@Beta
+public class InvalidRangeConstraintException extends IllegalArgumentException {
+    private static final long serialVersionUID = 1L;
+    private final RangeConstraint offendingRangeConstraint;
+
+    protected InvalidRangeConstraintException(final RangeConstraint offendingConstraint, final String message) {
+        super(message);
+        this.offendingRangeConstraint = Preconditions.checkNotNull(offendingConstraint);
+    }
+
+    public InvalidRangeConstraintException(final RangeConstraint offendingConstraint, final String format,
+            final Object... args) {
+        this(offendingConstraint, String.format(format, args));
+    }
+
+    public RangeConstraint getOffendingConstraint() {
+        return offendingRangeConstraint;
+    }
+}
index 127457c65baad927dfadb0cbde3980cb3b589ca9..fe217561b1c8df84d1a585cf1da01c0338ddab02 100644 (file)
@@ -153,8 +153,10 @@ public abstract class LengthRestrictedTypeBuilder<T extends TypeDefinition<T>> e
 
         // Now verify if new ranges are strict subset of base ranges
         for (LengthConstraint c : typedLengths) {
-            Preconditions.checkArgument(lengthCovered(baseLengths, c),
-                "Range constraint %s is not a subset of parent constraints %s", c, baseLengths);
+            if (!lengthCovered(baseLengths, c)) {
+                throw new InvalidLengthConstraintException(c, "Length constraint %s is not a subset of parent constraints %s",
+                c, baseLengths);
+            }
         }
 
         return buildType(typedLengths);
index 03b7e9bf1820b39d970cbf342f1f6084b3c50157..ebebbe3e42c4a46926fc2fdb091e18b418cee7e5 100644 (file)
@@ -138,8 +138,10 @@ public abstract class RangeRestrictedTypeBuilder<T extends TypeDefinition<T>> ex
 
         // Now verify if new ranges are strict subset of base ranges
         for (RangeConstraint c : typedRanges) {
-            Preconditions.checkArgument(rangeCovered(baseRangeConstraints, c),
-                "Range constraint %s is not a subset of parent constraints %s", c, baseRangeConstraints);
+            if (!rangeCovered(baseRangeConstraints, c)) {
+                throw new InvalidRangeConstraintException(c, "Range constraint %s is not a subset of parent constraints %s",
+                    c, baseRangeConstraints);
+            }
         }
 
         return typedRanges;