This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git


The following commit(s) were added to refs/heads/master by this push:
     new d6d4b824 Refactor duplicate code
d6d4b824 is described below

commit d6d4b824539b0d4794200d05286fd3de1ff2f236
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Jul 19 13:09:12 2025 -0400

    Refactor duplicate code
---
 .../apache/commons/codec/digest/MurmurHash.java    | 60 ++++++++++++++++++++++
 .../apache/commons/codec/digest/MurmurHash2.java   | 36 +------------
 .../apache/commons/codec/digest/MurmurHash3.java   | 48 +++--------------
 3 files changed, 68 insertions(+), 76 deletions(-)

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 00000000..55ddfe10
--- /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 4ae281ec..a3d7040b 100644
--- a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
@@ -58,38 +58,6 @@ public final class MurmurHash2 {
     private static final long M64 = 0xc6a4a7935bd1e995L;
     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 @@ public final class MurmurHash2 {
         // 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 @@ public final class MurmurHash2 {
         // 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 17325397..3de91cf8 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 @@ public final class MurmurHash3 {
 
             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
@@ -370,42 +370,6 @@ public final class MurmurHash3 {
         return hash;
     }
 
-    /**
-     * 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 @@ public final class MurmurHash3 {
         // 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 @@ public final class MurmurHash3 {
         // 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 @@ public final class MurmurHash3 {
         // 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 @@ public final class MurmurHash3 {
         // 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);

Reply via email to