You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pk...@apache.org on 2025/09/01 15:52:12 UTC

(commons-compress) branch master updated: Extract duplicate code from `IntList` (#702)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new fac1d8f86 Extract duplicate code from `IntList`  (#702)
fac1d8f86 is described below

commit fac1d8f86e22a4c447169201f46722145b05b348
Author: Zeyuan Li <14...@users.noreply.github.com>
AuthorDate: Tue Sep 2 01:52:07 2025 +1000

    Extract duplicate code from `IntList`  (#702)
    
    Remove duplicate code in `IntList` by extracting a method.
    
    Co-authored-by: Piotr P. Karwasz <pk...@apache.org>
---
 src/changes/changes.xml                            |  1 +
 .../commons/compress/harmony/pack200/IntList.java  | 31 +++++++---------------
 2 files changed, 11 insertions(+), 21 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a08753ef7..164ef80a5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -119,6 +119,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add TarArchiveInputStream.Builder and deprecate all but one constructor.</action>
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 85 to 87.</action>
+      <action type="update" dev="ppkarwasz" due-to="Raeps">Extract duplicate code in org.apache.commons.compress.harmony.pack200.IntList.</action>
     </release>
     <release version="1.28.0" date="2025-07-26" description="This is a feature and maintenance release. Java 8 or later is required.">
       <!-- FIX -->
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/IntList.java b/src/main/java/org/apache/commons/compress/harmony/pack200/IntList.java
index 755fe2fbe..73d416c8b 100644
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/IntList.java
+++ b/src/main/java/org/apache/commons/compress/harmony/pack200/IntList.java
@@ -20,6 +20,8 @@
 
 import java.util.Arrays;
 
+import org.apache.commons.lang3.math.NumberUtils;
+
 /**
  * IntList is based on {@link java.util.ArrayList}, but is written specifically for ints in order to reduce boxing and unboxing to Integers, reduce the memory
  * required and improve performance of pack200.
@@ -120,6 +122,10 @@ public int get(final int location) {
         throw new IndexOutOfBoundsException("" + location);
     }
 
+    private static int calculateIncrement(final int size, final int required) {
+        return NumberUtils.max(size / 2, required, 12);
+    }
+
     private void growAtEnd(final int required) {
         final int size = lastIndex - firstIndex;
         if (firstIndex >= required - (array.length - lastIndex)) {
@@ -130,13 +136,7 @@ private void growAtEnd(final int required) {
             firstIndex = 0;
             lastIndex = newLast;
         } else {
-            int increment = size / 2;
-            if (required > increment) {
-                increment = required;
-            }
-            if (increment < 12) {
-                increment = 12;
-            }
+            final int increment = calculateIncrement(size, required);
             final int[] newArray = new int[size + increment];
             if (size > 0) {
                 System.arraycopy(array, firstIndex, newArray, 0, size);
@@ -157,13 +157,7 @@ private void growAtFront(final int required) {
             firstIndex = newFirst;
             lastIndex = array.length;
         } else {
-            int increment = size / 2;
-            if (required > increment) {
-                increment = required;
-            }
-            if (increment < 12) {
-                increment = 12;
-            }
+            final int increment = calculateIncrement(size, required);
             final int[] newArray = new int[size + increment];
             if (size > 0) {
                 System.arraycopy(array, firstIndex, newArray, newArray.length - size, size);
@@ -174,15 +168,10 @@ private void growAtFront(final int required) {
         }
     }
 
+
     private void growForInsert(final int location, final int required) {
         final int size = lastIndex - firstIndex;
-        int increment = size / 2;
-        if (required > increment) {
-            increment = required;
-        }
-        if (increment < 12) {
-            increment = 12;
-        }
+        final int increment = calculateIncrement(size, required);
         final int[] newArray = new int[size + increment];
         final int newFirst = increment - required;
         // Copy elements after location to the new array skipping inserted