Add position variant of ObjectUtils.addObjectToArray

Add an overloaded version of `ObjectUtils.addObjectToArray` that allows
inserts at a specific position.

Closes gh-28415
This commit is contained in:
Phillip Webb
2022-04-13 16:35:28 -07:00
parent 44bdcef0b9
commit f17372ebea
2 changed files with 25 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -182,6 +182,15 @@ class ObjectUtilsTests {
assertThat(newArray[2]).isEqualTo(newElement);
}
@Test
void addObjectToArraysAtPosition() {
String[] array = new String[] {"foo", "bar", "baz"};
assertThat(ObjectUtils.addObjectToArray(array, "bat", 3)).containsExactly("foo", "bar", "baz", "bat");
assertThat(ObjectUtils.addObjectToArray(array, "bat", 2)).containsExactly("foo", "bar", "bat", "baz");
assertThat(ObjectUtils.addObjectToArray(array, "bat", 1)).containsExactly("foo", "bat", "bar", "baz");
assertThat(ObjectUtils.addObjectToArray(array, "bat", 0)).containsExactly("bat", "foo", "bar", "baz");
}
@Test
void addObjectToArrayWhenEmpty() {
String[] array = new String[0];