changeset 164:0dc7ae392a2e

8080620: Config classes hashCode() refactoring Summary: hashCode() calculation is moved to platfrom specific code Reviewed-by: alkonsta
author snazarki
date Mon, 18 May 2015 20:24:53 +0300
parents f8fd28c56300
children 404c859bac91
files src/se/classes/com/oracle/dio/impl/Platform.java src/share/classes/jdk/dio/adc/ADCChannelConfig.java src/share/classes/jdk/dio/atcmd/ATDeviceConfig.java src/share/classes/jdk/dio/counter/PulseCounterConfig.java src/share/classes/jdk/dio/dac/DACChannelConfig.java src/share/classes/jdk/dio/generic/GenericDeviceConfig.java src/share/classes/jdk/dio/gpio/GPIOPinConfig.java src/share/classes/jdk/dio/gpio/GPIOPortConfig.java src/share/classes/jdk/dio/i2cbus/I2CDeviceConfig.java src/share/classes/jdk/dio/pwm/PWMChannelConfig.java src/share/classes/jdk/dio/spibus/SPIDeviceConfig.java src/share/classes/jdk/dio/uart/UARTConfig.java src/share/classes/jdk/dio/watchdog/WatchdogTimerConfig.java
diffstat 13 files changed, 99 insertions(+), 127 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/classes/com/oracle/dio/impl/Platform.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/se/classes/com/oracle/dio/impl/Platform.java	Mon May 18 20:24:53 2015 +0300
@@ -24,6 +24,11 @@
  */
 package com.oracle.dio.impl;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Objects;
@@ -60,9 +65,82 @@
         if (null == obj1 || null == obj2) {
             return false;
         }
-        if (obj1.getClass() != obj2.getClass()) {
-            return false;
+
+        try {
+            serialize(obj1).equals(serialize(obj2));
+        } catch (IOException e) {
+            return Objects.equals(obj1, obj2);
         }
-        return Objects.equals(obj1, obj2);
     }
+
+    /**
+     * Creates a copy of object.
+     *
+     * @param obj object to be cloned.
+     * @return a clone of object.
+     */
+    public static Object clone(Object obj) throws IOException {
+        return deserialize(serialize(obj));
+    }
+
+    /**
+     * Convert the given byte array into object form.
+     *
+     * @note The object must be annotated with @SerializeMe tag
+     * @note So far ByteArrayInputStream is only useful
+     *       InputStream implementation
+     *
+     * @param in the serialized form of the objects to restore
+     *
+     * @return the root object
+     */
+    public static Object deserialize(InputStream in) throws IOException {
+        throw new IOException("Platform.deserialize() is not implemented");
+    }
+
+
+
+    /**
+     *  Converts the given object and everything is refers to
+     *       into serialized form.
+     * @note The object must be annotated with @SerializeMe tag
+     * @param obj root the root object to start serialization from
+     * @param os output stream to store serialized data
+     *
+     * @return a size of serialized data
+     */
+    public static int serialize(Object obj, OutputStream os) throws IOException {
+        throw new IOException("Platform.serialize() is not implemented");
+    }
+
+    /**
+     * Returns the hash code value for given object.
+     *
+     * @param obj       the target for hash calculation
+     * @param initVal hash code initial value
+     * @param factor  hash factor
+     * @return a hash code value for this object.
+     */
+    public static int hash(final Object obj, final int initVal, final int factor) {
+        try {
+            return serialize(obj).hashCode();
+        } catch (IOException e) {
+            return Objects.hashCode(obj);
+        }
+    }
+
+
+    // =========================
+    private static byte[] serialize(Object obj) throws IOException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        serialize(obj, baos);
+        baos.close();
+        return baos.toByteArray();
+    }
+
+    private static Object deserialize(byte[] ba) throws IOException {
+        return deserialize(new ByteArrayInputStream(ba));
+    }
+
+
 }
--- a/src/share/classes/jdk/dio/adc/ADCChannelConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/adc/ADCChannelConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -457,16 +457,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 7;
-        hash = 97 * hash + Objects.hashCode(this.controllerName);
-        hash = 97 * hash + this.channelNumber;
-        hash = 97 * hash + this.controllerNumber;
-        hash = 97 * hash + this.resolution;
-        hash = 97 * hash + this.samplingInterval;
-        hash = 97 * hash + this.samplingTime;
-        hash = 97 * hash + (int) (Double.doubleToLongBits(this.scaleFactor) ^ (Double.doubleToLongBits(this.scaleFactor) >>> 32));
-        hash = 97 * hash + this.inputBufferSize;
-        return hash;
+        return Platform.hash(this, 7, 97);
     }
 
     /**
--- a/src/share/classes/jdk/dio/atcmd/ATDeviceConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/atcmd/ATDeviceConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -29,6 +29,8 @@
 import java.io.OutputStream;
 import java.util.Objects;
 
+import com.oracle.dio.impl.Platform;
+
 import jdk.dio.DeviceConfig;
 import jdk.dio.DeviceManager;
 import jdk.dio.InvalidDeviceConfigException;
@@ -180,11 +182,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 3;
-        hash = 79 * hash + Objects.hashCode(this.controllerName);
-        hash = 79 * hash + this.controllerNumber;
-        hash = 79 * hash + this.channelNumber;
-        return hash;
+        return Platform.hash(this, 3, 79);
     }
 
     /**
@@ -198,19 +196,6 @@
      */
     @Override
     public boolean equals(Object obj) {
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        final ATDeviceConfig other = (ATDeviceConfig) obj;
-        if (!Objects.equals(this.controllerName, other.controllerName)) {
-            return false;
-        }
-        if (this.controllerNumber != other.controllerNumber) {
-            return false;
-        }
-        return this.channelNumber == other.channelNumber;
+        return Platform.equals(this, obj);
     }
 }
--- a/src/share/classes/jdk/dio/counter/PulseCounterConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/counter/PulseCounterConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -490,13 +490,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 3;
-        hash = 83 * hash + Objects.hashCode(this.controllerName);
-        hash = 83 * hash + this.controllerNumber;
-        hash = 83 * hash + this.channelNumber;
-        hash = 83 * hash + Objects.hashCode(this.sourceConfig);
-        hash = 83 * hash + this.type;
-        return hash;
+        return Platform.hash(this, 3, 83);
     }
 
     /**
--- a/src/share/classes/jdk/dio/dac/DACChannelConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/dac/DACChannelConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -412,15 +412,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 5;
-        hash = 13 * hash + Objects.hashCode(this.controllerName);
-        hash = 13 * hash + this.channelNumber;
-        hash = 13 * hash + this.controllerNumber;
-        hash = 13 * hash + this.resolution;
-        hash = 13 * hash + this.samplingInterval;
-        hash = 13 * hash + (int) (Double.doubleToLongBits(this.scaleFactor) ^ (Double.doubleToLongBits(this.scaleFactor) >>> 32));
-        hash = 13 * hash + this.outputBufferSize;
-        return hash;
+        return Platform.hash(this, 5, 13);
     }
 
     /**
--- a/src/share/classes/jdk/dio/generic/GenericDeviceConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/generic/GenericDeviceConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -337,11 +337,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 3;
-        hash = 97 * hash + Objects.hashCode(this.controllerName);
-        hash = 97 * hash + this.channelNumber;
-        hash = 97 * hash + this.controllerNumber;
-        return hash;
+        return Platform.hash(this, 3, 97);
     }
 
     /**
--- a/src/share/classes/jdk/dio/gpio/GPIOPinConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/gpio/GPIOPinConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -539,15 +539,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 7;
-        hash = 59 * hash + Objects.hashCode(this.controllerName);
-        hash = 59 * hash + this.direction;
-        hash = 59 * hash + (this.initValue ? 1 : 0);
-        hash = 59 * hash + this.mode;
-        hash = 59 * hash + this.pinNumber;
-        hash = 59 * hash + this.controllerNumber;
-        hash = 59 * hash + this.trigger;
-        return hash;
+        return Platform.hash(this, 7, 59);
     }
 
     /**
--- a/src/share/classes/jdk/dio/gpio/GPIOPortConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/gpio/GPIOPortConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -246,12 +246,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 3;
-        hash = 97 * hash + this.direction;
-        hash = 97 * hash + this.initValue;
-        for (GPIOPinConfig pinConfig : this.pinConfigs) {
-            hash = 97 * hash + Objects.hashCode(pinConfig);       }
-        return hash;
+        return Platform.hash(this, 3, 97);
     }
 
     /**
--- a/src/share/classes/jdk/dio/i2cbus/I2CDeviceConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/i2cbus/I2CDeviceConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -416,13 +416,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 3;
-        hash = 97 * hash + Objects.hashCode(this.controllerName);
-        hash = 97 * hash + this.address;
-        hash = 97 * hash + this.addressSize;
-        hash = 97 * hash + this.controllerNumber;
-        hash = 97 * hash + this.clockFrequency;
-        return hash;
+        return Platform.hash(this, 3, 97);
     }
 
     /**
--- a/src/share/classes/jdk/dio/pwm/PWMChannelConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/pwm/PWMChannelConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -615,17 +615,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 5;
-        hash = 79 * hash + Objects.hashCode(this.controllerName);
-        hash = 79 * hash + this.channelNumber;
-        hash = 79 * hash + this.controllerNumber;
-        hash = 79 * hash + Objects.hashCode(this.outputConfig);
-        hash = 79 * hash + this.idleState;
-        hash = 79 * hash + this.pulsePeriod;
-        hash = 79 * hash + (int) (Double.doubleToLongBits(this.scaleFactor) ^ (Double.doubleToLongBits(this.scaleFactor) >>> 32));
-        hash = 79 * hash + this.pulseAlignment;
-        hash = 79 * hash + this.outputBufferSize;
-        return hash;
+        return Platform.hash(this, 5, 79);
     }
 
     /**
--- a/src/share/classes/jdk/dio/spibus/SPIDeviceConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/spibus/SPIDeviceConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -646,16 +646,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 7;
-        hash = 47 * hash + Objects.hashCode(this.controllerName);
-        hash = 47 * hash + this.address;
-        hash = 47 * hash + this.controllerNumber;
-        hash = 47 * hash + this.bitOrdering;
-        hash = 47 * hash + this.clockFrequency;
-        hash = 47 * hash + this.clockMode;
-        hash = 47 * hash + this.csActive;
-        hash = 47 * hash + this.wordLength;
-        return hash;
+        return Platform.hash(this, 7, 47);
     }
 
     /**
--- a/src/share/classes/jdk/dio/uart/UARTConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/uart/UARTConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -715,18 +715,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 7;
-        hash = 53 * hash + Objects.hashCode(this.controllerName);
-        hash = 53 * hash + this.channelNumber;
-        hash = 53 * hash + this.controllerNumber;
-        hash = 53 * hash + this.baudRate;
-        hash = 53 * hash + this.dataBits;
-        hash = 53 * hash + this.flowcontrol;
-        hash = 53 * hash + this.inputBufferSize;
-        hash = 53 * hash + this.outputBufferSize;
-        hash = 53 * hash + this.parity;
-        hash = 53 * hash + this.stopBits;
-        return hash;
+        return Platform.hash(this, 7, 53);
     }
 
     /**
--- a/src/share/classes/jdk/dio/watchdog/WatchdogTimerConfig.java	Thu May 14 14:43:31 2015 +0300
+++ b/src/share/classes/jdk/dio/watchdog/WatchdogTimerConfig.java	Mon May 18 20:24:53 2015 +0300
@@ -29,6 +29,8 @@
 import java.io.OutputStream;
 import java.util.Objects;
 
+import com.oracle.dio.impl.Platform;
+
 import jdk.dio.DeviceConfig;
 import jdk.dio.DeviceManager;
 import jdk.dio.InvalidDeviceConfigException;
@@ -178,11 +180,7 @@
      */
     @Override
     public int hashCode() {
-        int hash = 3;
-        hash = 79 * hash + Objects.hashCode(this.controllerName);
-        hash = 79 * hash + this.controllerNumber;
-        hash = 79 * hash + this.timerNumber;
-        return hash;
+        return Platform.hash(this, 3, 79);
     }
 
     /**
@@ -197,19 +195,6 @@
      */
     @Override
     public boolean equals(Object obj) {
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        final WatchdogTimerConfig other = (WatchdogTimerConfig) obj;
-        if (!Objects.equals(this.controllerName, other.controllerName)) {
-            return false;
-        }
-        if (this.controllerNumber != other.controllerNumber) {
-            return false;
-        }
-        return this.timerNumber == other.timerNumber;
+        return Platform.equals(this, obj);
     }
 }