changeset 185:60b4d01ae4eb

8087213: GPIOPinConfig.Builder setters don't throw IllegalArgumentException for invalid value Summary: Redesigned values check procedures Reviewed-by: snazarki Contributed-by: alexey.savin@oracle.com
author snazarki
date Mon, 15 Jun 2015 18:02:37 +0300
parents 551d3edd1fcf
children 633ab404a9e5
files src/share/classes/jdk/dio/gpio/GPIOPinConfig.java
diffstat 1 files changed, 69 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/jdk/dio/gpio/GPIOPinConfig.java	Tue Jun 09 16:44:00 2015 +0300
+++ b/src/share/classes/jdk/dio/gpio/GPIOPinConfig.java	Mon Jun 15 18:02:37 2015 +0300
@@ -192,7 +192,7 @@
          * </ul>
          */
         public GPIOPinConfig build() {
-            instance.checkValues();
+            instance.checkConsistency();
             return instance;
         }
 
@@ -219,6 +219,7 @@
          */
         public Builder setPinNumber(int pinNumber) {
             instance.pinNumber = pinNumber;
+            instance.checkPinNumber();
             return this;
         }
 
@@ -234,6 +235,7 @@
          */
         public Builder setControllerNumber(int controllerNumber) {
             instance.controllerNumber = controllerNumber;
+            instance.checkControllerNumber();
             return this;
         }
 
@@ -250,6 +252,7 @@
          */
         public Builder setDirection(int direction) {
             instance.direction = direction;
+            instance.checkDirection();
             return this;
         }
 
@@ -287,6 +290,7 @@
          */
         public Builder setDriveMode(int mode) {
             instance.mode = mode;
+            instance.checkMode();
             return this;
         }
 
@@ -306,6 +310,7 @@
          */
         public Builder setTrigger(int trigger) {
             instance.trigger = trigger;
+            instance.checkTrigger();
             return this;
         }
     }
@@ -368,7 +373,7 @@
         this.trigger = trigger;
         this.mode = mode;
         this.initValue = initValue;
-        checkValues();
+        checkAll();
     }
 
     /**
@@ -426,7 +431,7 @@
         this.trigger = trigger;
         this.mode = mode;
         this.initValue = initValue;
-        checkValues();
+        checkAll();
     }
 
     /**
@@ -556,18 +561,61 @@
         return Platform.equals(this, obj);
     }
 
-    private void checkValues() {
-        if ( GPIOPinConfig.TRIGGER_NONE > trigger || GPIOPinConfig.TRIGGER_BOTH_LEVELS < trigger || 0 == mode ) {
-            throw new IllegalArgumentException(
-                ExceptionMessage.format(ExceptionMessage.GPIO_TRIGGER_OR_MODE)
-            );
+    private void checkControllerNumber() {
+        // we assume UNASSIGNED = -1
+        if (controllerNumber < UNASSIGNED) {
+            throw new IllegalArgumentException();
         }
+    }
 
+    private void checkPinNumber() {
+        // we assume UNASSIGNED = -1
+        if (pinNumber < UNASSIGNED) {
+            throw new IllegalArgumentException();
+        }
+    }
+
+    private void checkDirection() {
+        if (GPIOPinConfig.DIR_INPUT_ONLY != direction
+            && GPIOPinConfig.DIR_OUTPUT_ONLY != direction
+            && GPIOPinConfig.DIR_BOTH_INIT_INPUT != direction
+            && GPIOPinConfig.DIR_BOTH_INIT_OUTPUT != direction) {
+            throw new IllegalArgumentException();
+        }
+    }
+
+    private void checkMode() {
+        if (UNASSIGNED != mode
+            && (0 == mode
+                || 0 != (mode & ~(GPIOPinConfig.MODE_INPUT_PULL_UP | GPIOPinConfig.MODE_INPUT_PULL_DOWN | GPIOPinConfig.MODE_OUTPUT_PUSH_PULL | GPIOPinConfig.MODE_OUTPUT_OPEN_DRAIN))
+                || (GPIOPinConfig.MODE_INPUT_PULL_UP | GPIOPinConfig.MODE_INPUT_PULL_DOWN) == (mode & (GPIOPinConfig.MODE_INPUT_PULL_UP | GPIOPinConfig.MODE_INPUT_PULL_DOWN))
+                || (GPIOPinConfig.MODE_OUTPUT_PUSH_PULL | GPIOPinConfig.MODE_OUTPUT_OPEN_DRAIN) == (mode & (GPIOPinConfig.MODE_OUTPUT_PUSH_PULL | GPIOPinConfig.MODE_OUTPUT_OPEN_DRAIN)))) {
+            throw new IllegalArgumentException();
+        }
+    }
+
+    private void checkTrigger() {
+        switch (trigger) {
+            case GPIOPinConfig.TRIGGER_NONE:
+            case GPIOPinConfig.TRIGGER_FALLING_EDGE:
+            case GPIOPinConfig.TRIGGER_RISING_EDGE:
+            case GPIOPinConfig.TRIGGER_BOTH_EDGES:
+            case GPIOPinConfig.TRIGGER_HIGH_LEVEL:
+            case GPIOPinConfig.TRIGGER_LOW_LEVEL:
+            case GPIOPinConfig.TRIGGER_BOTH_LEVELS:
+                break;
+            default:
+                throw new IllegalArgumentException();
+        }
+    }
+
+    /** check if @see GPIOPinConfig.Builder.build() passed successfully.
+    * It differs from checking for setters such as setPinNumber etc. */
+    private void checkConsistency() {
         switch (direction) {
         case GPIOPinConfig.DIR_INPUT_ONLY:
             if (DeviceConfig.DEFAULT != mode &&
-                (0 !=  (mode & ~(GPIOPinConfig.MODE_INPUT_PULL_DOWN | GPIOPinConfig.MODE_INPUT_PULL_UP)) ||
-                mode > GPIOPinConfig.MODE_INPUT_PULL_DOWN )) {
+                0 != (mode & ~(GPIOPinConfig.MODE_INPUT_PULL_DOWN | GPIOPinConfig.MODE_INPUT_PULL_UP))) {
                 throw new IllegalArgumentException(
                     ExceptionMessage.format(ExceptionMessage.GPIO_MODE_NOT_FOR_DIRINPUTONLY)
                 );
@@ -575,8 +623,7 @@
             break;
         case GPIOPinConfig.DIR_OUTPUT_ONLY:
             if (DeviceConfig.DEFAULT != mode &&
-                (0 != (mode & ~(GPIOPinConfig.MODE_OUTPUT_OPEN_DRAIN | GPIOPinConfig.MODE_OUTPUT_PUSH_PULL )) ||
-                mode > GPIOPinConfig.MODE_OUTPUT_OPEN_DRAIN) ) {
+                0 != (mode & ~(GPIOPinConfig.MODE_OUTPUT_OPEN_DRAIN | GPIOPinConfig.MODE_OUTPUT_PUSH_PULL ))) {
                 throw new IllegalArgumentException(
                     ExceptionMessage.format(ExceptionMessage.GPIO_MODE_NOT_FOR_DIROUTPUTONLY)
                 );
@@ -600,4 +647,14 @@
             );
         }
     }
+
+    /** check everuthing for deprecated @see GPIOPinConfig call */
+    private void checkAll() {
+        checkControllerNumber();
+        checkPinNumber();
+        checkDirection();
+        checkMode();
+        checkTrigger();
+        checkConsistency();
+    }
 }