Skip to content

Conversation

@ivamly
Copy link

@ivamly ivamly commented Dec 8, 2025

Implementation of the LANG-1707 suggestion: added the concat methods to ArrayUtils for convenient concatenation of multiple arrays in a single call.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

@ivamly
Copy link
Author

ivamly commented Dec 8, 2025

Hi! This is my first contribution to Apache Commons.
I've started by implementing the concat methods for primitive arrays and their corresponding tests.
I would appreciate verification that I'm on the right track. I'm happy to address any feedback and will gladly add the generic T[] method and its tests once the approach is confirmed.

@ivamly
Copy link
Author

ivamly commented Dec 14, 2025

I'm not entirely sure I understand the question correctly. My implementation was based on the Jira ticket and its comments, where adding these methods was discussed and approved (as I understood it).

Could you please clarify what I might be missing? I'd be happy to adjust the approach if needed.

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @ivamly

I see this could be useful now...

  • Add Javadoc tags @since 3.21.0 on new public and protected methods
  • Remove extra blank lines
  • Short-circuit the totalLength computations to throw an IllegalArgumentException once the value exceeds SAFE_MAX_ARRAY_LENGTH; you'll likely need to use a long as the running total or use Math.addExact().
  • Javadoc: Sentence should end in a period (@param).

@garydgregory
Copy link
Member

Hello @ivamly

Are you still working on this?

@ivamly
Copy link
Author

ivamly commented Jan 21, 2026

Hello @garydgregory! I'll push the changes today.

@ivamly ivamly force-pushed the feature/LANG-1707 branch 3 times, most recently from a33357d to e11de7c Compare January 21, 2026 18:43
@ivamly
Copy link
Author

ivamly commented Jan 21, 2026

@garydgregory done.

@ivamly ivamly force-pushed the feature/LANG-1707 branch from e11de7c to 5b8a06b Compare January 21, 2026 19:55
@ivamly
Copy link
Author

ivamly commented Jan 21, 2026

@garydgregory fix build.

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @ivamly
Are you sure about the length computation? It looks to me like it allows for values > SAFE_MAX_ARRAY_LENGTH but just less than Integer.MAX_VALUE.

@ivamly
Copy link
Author

ivamly commented Jan 22, 2026

Really, you right. Work on it.

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @ivamly

  • You are missing tests that check the arithmetic exception path. You can use Mockito or EasyMock to mock Math.addExact() for that.
  • The exception try-catch doesn't need to be setup and torn down for every array element, instead of you can do that outside the for loop.
  • You can "move" the array element null test "inside" like this: totalLength = Math.addExact(totalLength, getLength(array));
  • Then you can refactor all of that copy-pasta, like this:
    public static byte[] concat(final byte[]... arrays) {
        int totalLength = 0;
        for (final byte[] array : arrays) {
            totalLength = addExact(totalLength, array);
        }
        final byte[] result = new byte[totalLength];
        int currentPos = 0;
        for (final byte[] array : arrays) {
            if (array != null && array.length > 0) {
                System.arraycopy(array, 0, result, currentPos, array.length);
                currentPos += array.length;
            }
        }
        return result;
    }

    private static int addExact(final int totalLength, final Object array) {
        try {
            return Math.addExact(totalLength, getLength(array));
        } catch (final ArithmeticException exception) {
            throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
        }
    }

...
  • Finally, when you add new method, make the new methods appear in AB order, like they existing ones already are.

Thank you!

@ivamly ivamly force-pushed the feature/LANG-1707 branch from 5b8a06b to bea4ecd Compare January 22, 2026 14:39
@ivamly
Copy link
Author

ivamly commented Jan 22, 2026

@garydgregory, thank you for the detailed feedback!

I've implemented the changes: added the helper method and sorted the methods. However, I've run into challenges testing the arithmetic exception path.

The issue: The project uses EasyMock, which doesn't support mocking static methods. Attempting to test this directly by creating an array near Integer.MAX_VALUE consistently causes an OutOfMemoryError, and such a test likely won't run reliably in CI or for other developers

My question: What's the established pattern for testing such scenarios in project?

I'd appreciate your guidance on the best path forward.

@garydgregory
Copy link
Member

garydgregory commented Jan 22, 2026

You can also try Mockito, it's version and BOM POM is inherited from the parent POM.

@ivamly
Copy link
Author

ivamly commented Jan 22, 2026

Mockito can't mock Math.addExact() due to known limitations with java.lang.* classes (see issue).

@garydgregory
Copy link
Member

garydgregory commented Jan 22, 2026

How about creating an addExcat() bridge method then mock that.

@ivamly ivamly force-pushed the feature/LANG-1707 branch from bea4ecd to 885499c Compare January 22, 2026 17:52
@ivamly
Copy link
Author

ivamly commented Jan 22, 2026

Mockito's mockStatic() proxies the entire class, and we can't call real methods for testing. Created a inner MathBridge class that encapsulates calls to Math and allows mocking only specific operations.

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @ivamly
Thank you for your update.
I tested the patch locally and it's green but it's still the case that totalLength computation can be > SAFE_MAX_ARRAY_LENGTH but < Integer.MAX_VALUE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants