Skip to content

Commit 68c2deb

Browse files
committed
F ArrayUtils.combine for single argument that is not an array yet plus array of same type
1 parent d2612f8 commit 68c2deb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

approvaltests-util-tests/src/test/java/com/spun/util/ArrayUtilsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public void testCombine()
3838
assertEquals("[1, 2, 3, 5, 6, 7]", ArrayUtils.combine(list1, list2).toString());
3939
}
4040
@Test
41+
public void testCombineArrays()
42+
{
43+
Integer[] ints = new Integer[]{5, 6, 7};
44+
assertEquals("[1, 5, 6, 7]", Arrays.toString(ArrayUtils.combine(1, ints)));
45+
assertEquals("[1]", Arrays.toString(ArrayUtils.combine(1)));
46+
}
47+
@Test
4148
void testToArray()
4249
{
4350
// begin-snippet: toArray

approvaltests-util/src/main/java/com/spun/util/ArrayUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ else if (isEmpty(b))
211211
return newArray;
212212
}
213213
}
214+
public static <T> T[] combine(T a, T... b)
215+
{
216+
final T[] toArray = (T[]) Array.newInstance(a.getClass(), 1);
217+
toArray[0] = a;
218+
return combine(toArray, b);
219+
}
214220
public static <T> boolean contains(T[] values, T value)
215221
{
216222
for (int i = 0; i < values.length; i++)
@@ -278,6 +284,7 @@ public static <T> T[] toArray(List<T> list, Class<T> type)
278284
{
279285
return Queryable.as(list, type).asArray();
280286
}
287+
281288
public static <KEY, VALUES, SPECIFIC_VALUE extends VALUES> SPECIFIC_VALUE getOrElse(Map<KEY, VALUES> fields,
282289
KEY key, Function0<SPECIFIC_VALUE> defaultIfNotFound)
283290
{
@@ -290,6 +297,7 @@ public static <KEY, VALUES, SPECIFIC_VALUE extends VALUES> SPECIFIC_VALUE getOrE
290297
return defaultIfNotFound.call();
291298
}
292299
}
300+
293301
public static class IterableWrapper<T> implements Iterable<T>
294302
{
295303
private final Iterator<T> iterator;

0 commit comments

Comments
 (0)