Refactor duplicate code
diff --git a/src/main/java/org/apache/commons/codec/digest/MurmurHash.java b/src/main/java/org/apache/commons/codec/digest/MurmurHash.java
new file mode 100644
index 0000000..55ddfe1
--- /dev/null
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.codec.digest;
+
+/**
+ * Commons implementation methods for MurmurHash* classes in this package.
+ */
+final class MurmurHash {
+
+ /**
+ * Gets the little-endian int from 4 bytes starting at the specified index.
+ *
+ * @param data The data
+ * @param index The index
+ * @return The little-endian int
+ */
+ static int getLittleEndianInt(final byte[] data, final int index) {
+ // @formatter:off
+ return data[index ] & 0xff |
+ (data[index + 1] & 0xff) << 8 |
+ (data[index + 2] & 0xff) << 16 |
+ (data[index + 3] & 0xff) << 24;
+ // @formatter:on
+ }
+
+ /**
+ * Gets the little-endian long from 8 bytes starting at the specified index.
+ *
+ * @param data The data
+ * @param index The index
+ * @return The little-endian long
+ */
+ static long getLittleEndianLong(final byte[] data, final int index) {
+ // @formatter:off
+ return (long) data[index ] & 0xff |
+ ((long) data[index + 1] & 0xff) << 8 |
+ ((long) data[index + 2] & 0xff) << 16 |
+ ((long) data[index + 3] & 0xff) << 24 |
+ ((long) data[index + 4] & 0xff) << 32 |
+ ((long) data[index + 5] & 0xff) << 40 |
+ ((long) data[index + 6] & 0xff) << 48 |
+ ((long) data[index + 7] & 0xff) << 56;
+ // @formatter:on
+ }
+}
diff --git a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
index 4ae281e..a3d7040 100644
--- a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
@@ -59,38 +59,6 @@
private static final int R64 = 47;
/**
- * Gets the little-endian int from 4 bytes starting at the specified index.
- *
- * @param data The data
- * @param index The index
- * @return The little-endian int
- */
- private static int getLittleEndianInt(final byte[] data, final int index) {
- return data[index ] & 0xff |
- (data[index + 1] & 0xff) << 8 |
- (data[index + 2] & 0xff) << 16 |
- (data[index + 3] & 0xff) << 24;
- }
-
- /**
- * Gets the little-endian long from 8 bytes starting at the specified index.
- *
- * @param data The data
- * @param index The index
- * @return The little-endian long
- */
- private static long getLittleEndianLong(final byte[] data, final int index) {
- return (long) data[index ] & 0xff |
- ((long) data[index + 1] & 0xff) << 8 |
- ((long) data[index + 2] & 0xff) << 16 |
- ((long) data[index + 3] & 0xff) << 24 |
- ((long) data[index + 4] & 0xff) << 32 |
- ((long) data[index + 5] & 0xff) << 40 |
- ((long) data[index + 6] & 0xff) << 48 |
- ((long) data[index + 7] & 0xff) << 56;
- }
-
- /**
* Generates a 32-bit hash from byte array with the given length and a default seed value.
* This is a helper method that will produce the same result as:
*
@@ -124,7 +92,7 @@
// body
for (int i = 0; i < nblocks; i++) {
final int index = i << 2;
- int k = getLittleEndianInt(data, index);
+ int k = MurmurHash.getLittleEndianInt(data, index);
k *= M32;
k ^= k >>> R32;
k *= M32;
@@ -228,7 +196,7 @@
// body
for (int i = 0; i < nblocks; i++) {
final int index = i << 3;
- long k = getLittleEndianLong(data, index);
+ long k = MurmurHash.getLittleEndianLong(data, index);
k *= M64;
k ^= k >>> R64;
diff --git a/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java b/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
index 1732539..3de91cf 100644
--- a/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
@@ -241,7 +241,7 @@
for (int i = 0; i < nblocks; i++) {
final int index = newOffset + (i << 2);
- final int k = getLittleEndianInt(data, index);
+ final int k = MurmurHash.getLittleEndianInt(data, index);
hash = mix32(k, hash);
}
// Save left-over unprocessed bytes
@@ -371,42 +371,6 @@
}
/**
- * Gets the little-endian int from 4 bytes starting at the specified index.
- *
- * @param data The data
- * @param index The index
- * @return The little-endian int
- */
- private static int getLittleEndianInt(final byte[] data, final int index) {
- // @formatter:off
- return data[index ] & 0xff |
- (data[index + 1] & 0xff) << 8 |
- (data[index + 2] & 0xff) << 16 |
- (data[index + 3] & 0xff) << 24;
- // @formatter:on
- }
-
- /**
- * Gets the little-endian long from 8 bytes starting at the specified index.
- *
- * @param data The data
- * @param index The index
- * @return The little-endian long
- */
- private static long getLittleEndianLong(final byte[] data, final int index) {
- // @formatter:off
- return (long) data[index ] & 0xff |
- ((long) data[index + 1] & 0xff) << 8 |
- ((long) data[index + 2] & 0xff) << 16 |
- ((long) data[index + 3] & 0xff) << 24 |
- ((long) data[index + 4] & 0xff) << 32 |
- ((long) data[index + 5] & 0xff) << 40 |
- ((long) data[index + 6] & 0xff) << 48 |
- ((long) data[index + 7] & 0xff) << 56;
- // @formatter:on
- }
-
- /**
* Generates 128-bit hash from the byte array with a default seed.
* This is a helper method that will produce the same result as:
*
@@ -552,8 +516,8 @@
// body
for (int i = 0; i < nblocks; i++) {
final int index = offset + (i << 4);
- long k1 = getLittleEndianLong(data, index);
- long k2 = getLittleEndianLong(data, index + 8);
+ long k1 = MurmurHash.getLittleEndianLong(data, index);
+ long k2 = MurmurHash.getLittleEndianLong(data, index + 8);
// mix functions for k1
k1 *= C1;
@@ -754,7 +718,7 @@
// body
for (int i = 0; i < nblocks; i++) {
final int index = offset + (i << 2);
- final int k = getLittleEndianInt(data, index);
+ final int k = MurmurHash.getLittleEndianInt(data, index);
hash = mix32(k, hash);
}
// tail
@@ -954,7 +918,7 @@
// body
for (int i = 0; i < nblocks; i++) {
final int index = offset + (i << 2);
- final int k = getLittleEndianInt(data, index);
+ final int k = MurmurHash.getLittleEndianInt(data, index);
hash = mix32(k, hash);
}
// tail
@@ -1103,7 +1067,7 @@
// body
for (int i = 0; i < nblocks; i++) {
final int index = offset + (i << 3);
- long k = getLittleEndianLong(data, index);
+ long k = MurmurHash.getLittleEndianLong(data, index);
// mix functions
k *= C1;
k = Long.rotateLeft(k, R1);