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.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@812236 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-07 17:56:30 +00:00
parent 02f510c204
commit ab66a1d87c
5 changed files with 35 additions and 25 deletions

View File

@ -398,13 +398,15 @@ public final class DoubleRange extends Range implements Serializable {
*/
@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;
}

View File

@ -394,11 +394,13 @@ public final class FloatRange extends Range implements Serializable {
*/
@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;
}

View File

@ -368,11 +368,13 @@ public final class IntRange extends Range implements Serializable {
*/
@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;
}

View File

@ -381,11 +381,13 @@ public final class LongRange extends Range implements Serializable {
*/
@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;
}

View File

@ -215,11 +215,13 @@ public final class NumberRange extends Range implements Serializable {
*/
@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;
}