commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r812236 - in /commons/proper/lang/trunk/src/java/org/apache/commons/lang/math: DoubleRange.java FloatRange.java IntRange.java LongRange.java NumberRange.java
Date Mon, 07 Sep 2009 17:56:31 GMT
Author: bayard
Date: Mon Sep  7 17:56:30 2009
New Revision: 812236

URL: http://svn.apache.org/viewvc?rev=812236&view=rev
Log:
Applying Boris' patch in LANG-481 to improve thread safety in the Range classes. The previous
code's reuse of the hashCode variable for local operations means it is utterly non-thread-safe.
Now uses a local variable so that the worst case should be the hashCode being calculated multiple
times, but not ending up with different values. 

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/DoubleRange.java
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/FloatRange.java
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/IntRange.java
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/LongRange.java
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberRange.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/DoubleRange.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/DoubleRange.java?rev=812236&r1=812235&r2=812236&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/DoubleRange.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/DoubleRange.java Mon Sep
 7 17:56:30 2009
@@ -398,13 +398,15 @@
      */
     @Override
     public int hashCode() {
-        if (hashCode == 0) {
-            hashCode = 17;
-            hashCode = 37 * hashCode + getClass().hashCode();
+        int temp = hashCode;
+        if (temp == 0) {
+            temp = 17;
+            temp = 37 * temp + getClass().hashCode();
             long lng = Double.doubleToLongBits(min);
-            hashCode = 37 * hashCode + ((int) (lng ^ (lng >> 32)));
+            temp = 37 * temp + ((int) (lng ^ (lng >> 32)));
             lng = Double.doubleToLongBits(max);
-            hashCode = 37 * hashCode + ((int) (lng ^ (lng >> 32)));
+            temp = 37 * temp + ((int) (lng ^ (lng >> 32)));
+            hashCode = temp;
         }
         return hashCode;
     }

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/FloatRange.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/FloatRange.java?rev=812236&r1=812235&r2=812236&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/FloatRange.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/FloatRange.java Mon Sep
 7 17:56:30 2009
@@ -394,11 +394,13 @@
      */
     @Override
     public int hashCode() {
-        if (hashCode == 0) {
-            hashCode = 17;
-            hashCode = 37 * hashCode + getClass().hashCode();
-            hashCode = 37 * hashCode + Float.floatToIntBits(min);
-            hashCode = 37 * hashCode + Float.floatToIntBits(max);
+        int temp = hashCode;
+        if (temp == 0) {
+            temp = 17;
+            temp = 37 * temp + getClass().hashCode();
+            temp = 37 * temp + Float.floatToIntBits(min);
+            temp = 37 * temp + Float.floatToIntBits(max);
+            hashCode = temp;
         }
         return hashCode;
     }

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/IntRange.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/IntRange.java?rev=812236&r1=812235&r2=812236&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/IntRange.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/IntRange.java Mon Sep
 7 17:56:30 2009
@@ -368,11 +368,13 @@
      */
     @Override
     public int hashCode() {
-        if (hashCode == 0) {
-            hashCode = 17;
-            hashCode = 37 * hashCode + getClass().hashCode();
-            hashCode = 37 * hashCode + min;
-            hashCode = 37 * hashCode + max;
+        int temp = hashCode;
+        if (temp == 0) {
+            temp = 17;
+            temp = 37 * temp + getClass().hashCode();
+            temp = 37 * temp + min;
+            temp = 37 * temp + max;
+            hashCode = temp;
         }
         return hashCode;
     }

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/LongRange.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/LongRange.java?rev=812236&r1=812235&r2=812236&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/LongRange.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/LongRange.java Mon Sep
 7 17:56:30 2009
@@ -381,11 +381,13 @@
      */
     @Override
     public int hashCode() {
-        if (hashCode == 0) {
-            hashCode = 17;
-            hashCode = 37 * hashCode + getClass().hashCode();
-            hashCode = 37 * hashCode + ((int) (min ^ (min >> 32)));
-            hashCode = 37 * hashCode + ((int) (max ^ (max >> 32)));
+        int temp = hashCode;
+        if (temp == 0) {
+            temp = 17;
+            temp = 37 * temp + getClass().hashCode();
+            temp = 37 * temp + ((int) (min ^ (min >> 32)));
+            temp = 37 * temp + ((int) (max ^ (max >> 32)));
+            hashCode = temp;
         }
         return hashCode;
     }

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberRange.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberRange.java?rev=812236&r1=812235&r2=812236&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberRange.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberRange.java Mon Sep
 7 17:56:30 2009
@@ -215,11 +215,13 @@
      */
     @Override
     public int hashCode() {
-        if (hashCode == 0) {
-            hashCode = 17;
-            hashCode = 37 * hashCode + getClass().hashCode();
-            hashCode = 37 * hashCode + min.hashCode();
-            hashCode = 37 * hashCode + max.hashCode();
+        int temp = hashCode;
+        if (temp == 0) {
+            temp = 17;
+            temp = 37 * temp + getClass().hashCode();
+            temp = 37 * temp + min.hashCode();
+            temp = 37 * temp + max.hashCode();
+            hashCode = temp;
         }
         return hashCode;
     }



Mime
View raw message