From 8361d7dbf4e5a637b4cd7b1e5d26cc3c936af1aa Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 20 Oct 2015 11:46:58 -0400 Subject: [PATCH] Use built-in method for computing hash code of longs This commit replaces instances of manually computing a hash code for primitive longs by XORing the upper bits with the lower bits with a built-in method for doing the same. --- .../elasticsearch/action/get/MultiGetRequest.java | 2 +- .../elasticsearch/cluster/SnapshotsInProgress.java | 2 +- .../elasticsearch/cluster/routing/ShardRouting.java | 2 +- .../cluster/routing/UnassignedInfo.java | 2 +- .../java/org/elasticsearch/common/geo/GeoPoint.java | 4 ++-- .../org/elasticsearch/common/unit/ByteSizeValue.java | 2 +- .../org/elasticsearch/common/unit/SizeValue.java | 2 +- .../org/elasticsearch/common/unit/TimeValue.java | 2 +- .../random/RandomScoreFunctionBuilder.java | 2 +- .../index/search/geo/GeoDistanceRangeQuery.java | 8 ++++---- .../index/shard/ShardStateMetaData.java | 2 +- .../org/elasticsearch/index/translog/Translog.java | 12 ++++++------ .../indices/cache/request/IndicesRequestCache.java | 4 ++-- .../indices/recovery/RecoveryState.java | 4 ++-- .../java/org/elasticsearch/snapshots/Snapshot.java | 2 +- .../gateway/MetaDataStateFormatTests.java | 4 ++-- 16 files changed, 28 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java b/core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java index 030b0ee5503..a6af7050fd7 100644 --- a/core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java +++ b/core/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java @@ -234,7 +234,7 @@ public class MultiGetRequest extends ActionRequest implements I result = 31 * result + id.hashCode(); result = 31 * result + (routing != null ? routing.hashCode() : 0); result = 31 * result + (fields != null ? Arrays.hashCode(fields) : 0); - result = 31 * result + (int) (version ^ (version >>> 32)); + result = 31 * result + Long.hashCode(version); result = 31 * result + versionType.hashCode(); result = 31 * result + (fetchSourceContext != null ? fetchSourceContext.hashCode() : 0); return result; diff --git a/core/src/main/java/org/elasticsearch/cluster/SnapshotsInProgress.java b/core/src/main/java/org/elasticsearch/cluster/SnapshotsInProgress.java index 821ab3ce1ab..6ee8365d378 100644 --- a/core/src/main/java/org/elasticsearch/cluster/SnapshotsInProgress.java +++ b/core/src/main/java/org/elasticsearch/cluster/SnapshotsInProgress.java @@ -152,7 +152,7 @@ public class SnapshotsInProgress extends AbstractDiffable implements Cus result = 31 * result + shards.hashCode(); result = 31 * result + indices.hashCode(); result = 31 * result + waitingIndices.hashCode(); - result = 31 * result + (int) (startTime ^ (startTime >>> 32)); + result = 31 * result + Long.hashCode(startTime); return result; } diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/ShardRouting.java b/core/src/main/java/org/elasticsearch/cluster/routing/ShardRouting.java index 8ee8205ca60..bc90241811a 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/ShardRouting.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/ShardRouting.java @@ -654,7 +654,7 @@ public final class ShardRouting implements Streamable, ToXContent { result = 31 * result + (relocatingNodeId != null ? relocatingNodeId.hashCode() : 0); result = 31 * result + (primary ? 1 : 0); result = 31 * result + (state != null ? state.hashCode() : 0); - result = 31 * result + (int) (version ^ (version >>> 32)); + result = 31 * result + Long.hashCode(version); result = 31 * result + (restoreSource != null ? restoreSource.hashCode() : 0); result = 31 * result + (allocationId != null ? allocationId.hashCode() : 0); result = 31 * result + (unassignedInfo != null ? unassignedInfo.hashCode() : 0); diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java b/core/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java index e3295415389..384a441d0c2 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java @@ -311,7 +311,7 @@ public class UnassignedInfo implements ToXContent, Writeable { @Override public int hashCode() { int result = reason != null ? reason.hashCode() : 0; - result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); + result = 31 * result + Long.hashCode(timestamp); result = 31 * result + (message != null ? message.hashCode() : 0); result = 31 * result + (failure != null ? failure.hashCode() : 0); return result; diff --git a/core/src/main/java/org/elasticsearch/common/geo/GeoPoint.java b/core/src/main/java/org/elasticsearch/common/geo/GeoPoint.java index 91a6d14e152..1a3b50cc339 100644 --- a/core/src/main/java/org/elasticsearch/common/geo/GeoPoint.java +++ b/core/src/main/java/org/elasticsearch/common/geo/GeoPoint.java @@ -138,9 +138,9 @@ public final class GeoPoint { int result; long temp; temp = lat != +0.0d ? Double.doubleToLongBits(lat) : 0L; - result = (int) (temp ^ (temp >>> 32)); + result = Long.hashCode(temp); temp = lon != +0.0d ? Double.doubleToLongBits(lon) : 0L; - result = 31 * result + (int) (temp ^ (temp >>> 32)); + result = 31 * result + Long.hashCode(temp); return result; } diff --git a/core/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java b/core/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java index a269d23c0ce..d4390febaaf 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java +++ b/core/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java @@ -259,7 +259,7 @@ public class ByteSizeValue implements Streamable { @Override public int hashCode() { - int result = (int) (size ^ (size >>> 32)); + int result = Long.hashCode(size); result = 31 * result + (sizeUnit != null ? sizeUnit.hashCode() : 0); return result; } diff --git a/core/src/main/java/org/elasticsearch/common/unit/SizeValue.java b/core/src/main/java/org/elasticsearch/common/unit/SizeValue.java index fcbcff3c3d7..3958b19309e 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/SizeValue.java +++ b/core/src/main/java/org/elasticsearch/common/unit/SizeValue.java @@ -228,7 +228,7 @@ public class SizeValue implements Streamable { @Override public int hashCode() { - int result = (int) (size ^ (size >>> 32)); + int result = Long.hashCode(size); result = 31 * result + (sizeUnit != null ? sizeUnit.hashCode() : 0); return result; } diff --git a/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java b/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java index 5f9eb953a42..6be5cc043c3 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java +++ b/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java @@ -311,7 +311,7 @@ public class TimeValue implements Streamable { @Override public int hashCode() { long normalized = timeUnit.toNanos(duration); - return (int) (normalized ^ (normalized >>> 32)); + return Long.hashCode(normalized); } public static long nsecToMSec(long ns) { diff --git a/core/src/main/java/org/elasticsearch/index/query/functionscore/random/RandomScoreFunctionBuilder.java b/core/src/main/java/org/elasticsearch/index/query/functionscore/random/RandomScoreFunctionBuilder.java index e62aabc1972..4d5713948e2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/functionscore/random/RandomScoreFunctionBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/functionscore/random/RandomScoreFunctionBuilder.java @@ -131,6 +131,6 @@ public class RandomScoreFunctionBuilder extends ScoreFunctionBuilder>> 32)); + return Long.hashCode(value); } } diff --git a/core/src/main/java/org/elasticsearch/index/search/geo/GeoDistanceRangeQuery.java b/core/src/main/java/org/elasticsearch/index/search/geo/GeoDistanceRangeQuery.java index c590ea08301..1f3c59bc0c6 100644 --- a/core/src/main/java/org/elasticsearch/index/search/geo/GeoDistanceRangeQuery.java +++ b/core/src/main/java/org/elasticsearch/index/search/geo/GeoDistanceRangeQuery.java @@ -204,13 +204,13 @@ public class GeoDistanceRangeQuery extends Query { int result = super.hashCode(); long temp; temp = lat != +0.0d ? Double.doubleToLongBits(lat) : 0L; - result = 31 * result + (int) (temp ^ (temp >>> 32)); + result = 31 * result + Long.hashCode(temp); temp = lon != +0.0d ? Double.doubleToLongBits(lon) : 0L; - result = 31 * result + (int) (temp ^ (temp >>> 32)); + result = 31 * result + Long.hashCode(temp); temp = inclusiveLowerPoint != +0.0d ? Double.doubleToLongBits(inclusiveLowerPoint) : 0L; - result = 31 * result + (int) (temp ^ (temp >>> 32)); + result = 31 * result + Long.hashCode(temp); temp = inclusiveUpperPoint != +0.0d ? Double.doubleToLongBits(inclusiveUpperPoint) : 0L; - result = 31 * result + (int) (temp ^ (temp >>> 32)); + result = 31 * result + Long.hashCode(temp); result = 31 * result + (geoDistance != null ? geoDistance.hashCode() : 0); result = 31 * result + indexFieldData.getFieldNames().indexName().hashCode(); return result; diff --git a/core/src/main/java/org/elasticsearch/index/shard/ShardStateMetaData.java b/core/src/main/java/org/elasticsearch/index/shard/ShardStateMetaData.java index 5c61496b987..6d6d3c788c6 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/ShardStateMetaData.java +++ b/core/src/main/java/org/elasticsearch/index/shard/ShardStateMetaData.java @@ -75,7 +75,7 @@ public final class ShardStateMetaData { @Override public int hashCode() { - int result = (int) (version ^ (version >>> 32)); + int result = Long.hashCode(version); result = 31 * result + (indexUUID != null ? indexUUID.hashCode() : 0); result = 31 * result + (primary ? 1 : 0); return result; diff --git a/core/src/main/java/org/elasticsearch/index/translog/Translog.java b/core/src/main/java/org/elasticsearch/index/translog/Translog.java index 4265d611fbf..24f6ac15c42 100644 --- a/core/src/main/java/org/elasticsearch/index/translog/Translog.java +++ b/core/src/main/java/org/elasticsearch/index/translog/Translog.java @@ -746,8 +746,8 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC @Override public int hashCode() { - int result = (int) (generation ^ (generation >>> 32)); - result = 31 * result + (int) (translogLocation ^ (translogLocation >>> 32)); + int result = Long.hashCode(generation); + result = 31 * result + Long.hashCode(translogLocation); result = 31 * result + size; return result; } @@ -1005,13 +1005,13 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC public int hashCode() { int result = id.hashCode(); result = 31 * result + type.hashCode(); - result = 31 * result + (int) (version ^ (version >>> 32)); + result = 31 * result + Long.hashCode(version); result = 31 * result + versionType.hashCode(); result = 31 * result + source.hashCode(); result = 31 * result + (routing != null ? routing.hashCode() : 0); result = 31 * result + (parent != null ? parent.hashCode() : 0); - result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); - result = 31 * result + (int) (ttl ^ (ttl >>> 32)); + result = 31 * result + Long.hashCode(timestamp); + result = 31 * result + Long.hashCode(ttl); return result; } @@ -1119,7 +1119,7 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC @Override public int hashCode() { int result = uid.hashCode(); - result = 31 * result + (int) (version ^ (version >>> 32)); + result = 31 * result + Long.hashCode(version); result = 31 * result + versionType.hashCode(); return result; } diff --git a/core/src/main/java/org/elasticsearch/indices/cache/request/IndicesRequestCache.java b/core/src/main/java/org/elasticsearch/indices/cache/request/IndicesRequestCache.java index 723c36e30ec..6628252d8eb 100644 --- a/core/src/main/java/org/elasticsearch/indices/cache/request/IndicesRequestCache.java +++ b/core/src/main/java/org/elasticsearch/indices/cache/request/IndicesRequestCache.java @@ -344,7 +344,7 @@ public class IndicesRequestCache extends AbstractComponent implements RemovalLis @Override public int hashCode() { int result = shard.hashCode(); - result = 31 * result + (int) (readerVersion ^ (readerVersion >>> 32)); + result = 31 * result + Long.hashCode(readerVersion); result = 31 * result + value.hashCode(); return result; } @@ -379,7 +379,7 @@ public class IndicesRequestCache extends AbstractComponent implements RemovalLis @Override public int hashCode() { int result = indexShard.hashCode(); - result = 31 * result + (int) (readerVersion ^ (readerVersion >>> 32)); + result = 31 * result + Long.hashCode(readerVersion); return result; } } diff --git a/core/src/main/java/org/elasticsearch/indices/recovery/RecoveryState.java b/core/src/main/java/org/elasticsearch/indices/recovery/RecoveryState.java index 022c326bf93..92bfc87218a 100644 --- a/core/src/main/java/org/elasticsearch/indices/recovery/RecoveryState.java +++ b/core/src/main/java/org/elasticsearch/indices/recovery/RecoveryState.java @@ -690,8 +690,8 @@ public class RecoveryState implements ToXContent, Streamable { @Override public int hashCode() { int result = name.hashCode(); - result = 31 * result + (int) (length ^ (length >>> 32)); - result = 31 * result + (int) (recovered ^ (recovered >>> 32)); + result = 31 * result + Long.hashCode(length); + result = 31 * result + Long.hashCode(recovered); result = 31 * result + (reused ? 1 : 0); return result; } diff --git a/core/src/main/java/org/elasticsearch/snapshots/Snapshot.java b/core/src/main/java/org/elasticsearch/snapshots/Snapshot.java index d7e0a064048..d4311cab901 100644 --- a/core/src/main/java/org/elasticsearch/snapshots/Snapshot.java +++ b/core/src/main/java/org/elasticsearch/snapshots/Snapshot.java @@ -227,7 +227,7 @@ public class Snapshot implements Comparable, ToXContent, FromXContentB @Override public int hashCode() { int result = name.hashCode(); - result = 31 * result + (int) (startTime ^ (startTime >>> 32)); + result = 31 * result + Long.hashCode(startTime); return result; } diff --git a/core/src/test/java/org/elasticsearch/gateway/MetaDataStateFormatTests.java b/core/src/test/java/org/elasticsearch/gateway/MetaDataStateFormatTests.java index ba417ac9765..0c7566d538a 100644 --- a/core/src/test/java/org/elasticsearch/gateway/MetaDataStateFormatTests.java +++ b/core/src/test/java/org/elasticsearch/gateway/MetaDataStateFormatTests.java @@ -489,9 +489,9 @@ public class MetaDataStateFormatTests extends ESTestCase { long temp; result = string.hashCode(); result = 31 * result + aInt; - result = 31 * result + (int) (aLong ^ (aLong >>> 32)); + result = 31 * result + Long.hashCode(aLong); temp = Double.doubleToLongBits(aDouble); - result = 31 * result + (int) (temp ^ (temp >>> 32)); + result = 31 * result + Long.hashCode(temp); result = 31 * result + (aBoolean ? 1 : 0); return result; }