changeset 168:633ab404a9e5

8087224: Counter: PulseCounterConfig.Builder.setSourceConfig() IllegalArgumentException should be thrown if direction is set to DIR_BOTH_INIT_OUTPUT Summary: Redesigned values check procedures Reviewed-by: snazarki Contributed-by: olga.milovskaya@oracle.com
author snazarki
date Mon, 15 Jun 2015 18:07:44 +0300
parents 60b4d01ae4eb
children 35bd126ae904
files src/share/classes/jdk/dio/counter/PulseCounterConfig.java
diffstat 1 files changed, 41 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/jdk/dio/counter/PulseCounterConfig.java	Mon Jun 15 18:02:37 2015 +0300
+++ b/src/share/classes/jdk/dio/counter/PulseCounterConfig.java	Mon Jun 15 18:07:44 2015 +0300
@@ -146,7 +146,9 @@
          * </ul>
          */
         public PulseCounterConfig build() {
-            instance.checkValues();
+            if (instance.type == UNASSIGNED) {
+                throw new IllegalStateException();
+            }
             return instance;
         }
 
@@ -174,6 +176,7 @@
          */
         public Builder setChannelNumber(int channelNumber) {
             instance.channelNumber = channelNumber;
+            instance.checkChannelNumber();
             return this;
         }
 
@@ -189,6 +192,7 @@
          */
         public Builder setControllerNumber(int controllerNumber) {
             instance.controllerNumber = controllerNumber;
+            instance.checkControllerNumber();
             return this;
         }
 
@@ -204,6 +208,7 @@
          */
         public Builder setSourceConfig(GPIOPinConfig source) {
             instance.sourceConfig = source;
+            instance.checkSourceConfig();
             return this;
         }
 
@@ -219,6 +224,7 @@
          */
         public Builder setType(int type) {
             instance.type = type;
+            instance.checkType();
             return this;
         }
     }
@@ -507,33 +513,51 @@
         return Platform.equals(this, obj);
     }
 
-    private void checkValues() throws IllegalArgumentException {
-        if (controllerNumber < DEFAULT) {
+    private void checkChannelNumber() {
+        if (channelNumber < UNASSIGNED ) {
+            throw new IllegalArgumentException(
+                ExceptionMessage.format(ExceptionMessage.COUNTER_INVALID_CHANNEL_NUMBER)
+            );
+        }
+    }
+
+    private void checkControllerNumber() {
+        if (controllerNumber < UNASSIGNED) {
             throw new IllegalArgumentException(
                 ExceptionMessage.format(ExceptionMessage.COUNTER_INVALID_CONTROLLER_NUMBER)
             );
         }
-        if(channelNumber < DEFAULT) {
-            throw new IllegalArgumentException(
-                ExceptionMessage.format(ExceptionMessage.COUNTER_INVALID_CHANNEL_NUMBER)
-            );
-        }
-        if (TYPE_NEGATIVE_PULSE < type || TYPE_FALLING_EDGE_ONLY > type) {
+    }
+
+    private void checkType() {
+        if (TYPE_NEGATIVE_PULSE < type || TYPE_FALLING_EDGE_ONLY > type ) {
             throw new IllegalArgumentException(
                 ExceptionMessage.format(ExceptionMessage.COUNTER_INVALID_TYPE)
             );
         }
-        GPIOPinConfig cfg =  sourceConfig;
+    }
 
-        if (null != cfg) {
-            if ((cfg.getDirection() != GPIOPinConfig.DIR_INPUT_ONLY &&
-                cfg.getDirection() != GPIOPinConfig.DIR_BOTH_INIT_OUTPUT &&
-                cfg.getDirection() != GPIOPinConfig.DIR_BOTH_INIT_INPUT) ||
-                // rising and falling edges
-                ((cfg.getTrigger() >> 1) != type &&
+    private void checkSourceConfig() {
+        if  (sourceConfig != null &&
+                    (sourceConfig.getDirection() !=  GPIOPinConfig.DIR_INPUT_ONLY && sourceConfig.getDirection() !=  GPIOPinConfig.DIR_BOTH_INIT_INPUT)) {
+            throw new IllegalArgumentException(
+                    ExceptionMessage.format(ExceptionMessage.COUNTER_CONFIG_CANNOT_BE_USED)
+                );
+        }
+    }
+
+    private void checkValues() throws IllegalArgumentException {
+        checkChannelNumber();
+        checkControllerNumber();
+        checkType();
+        checkSourceConfig();
+        // rising and falling edges
+        if (null != sourceConfig) {
+            int trigger = sourceConfig.getTrigger();
+            if (((trigger >> 1) != type &&
                 //When the pulse source is a GPIO pin  then this pulse edge type can only be used if the pin is configured with the
                 // GPIOPinConfig#TRIGGER_BOTH_EDGES trigger mode
-                (type > TYPE_RISING_EDGE_ONLY && cfg.getTrigger() != GPIOPinConfig.TRIGGER_BOTH_EDGES))
+                (type > TYPE_RISING_EDGE_ONLY && trigger != GPIOPinConfig.TRIGGER_BOTH_EDGES))
                 ) {
                 throw new IllegalArgumentException(
                     ExceptionMessage.format(ExceptionMessage.COUNTER_CONFIG_CANNOT_BE_USED)