Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions guava-tests/test/com/google/common/collect/ObjectArraysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ public void testAppendTwoElements() {
assertThat(array).asList().containsExactly("foo", "bar", "baz").inOrder();
}

public void testPrependZeroElements_withIncompatibleElementType() {
String[] array = {};
Object[] result = ObjectArrays.<Object>concat(0, array);
assertThat(result).asList().containsExactly(0);
}

public void testPrependOneElement_withIncompatibleElementType() {
String[] array = {"bar"};
Object[] result = ObjectArrays.<Object>concat(0, array);
assertThat(result).asList().containsExactly(0, "bar").inOrder();
}

public void testAppendZeroElements_withIncompatibleElementType() {
String[] array = {};
Object[] result = ObjectArrays.<Object>concat(array, 0);
assertThat(result).asList().containsExactly(0);
}

public void testAppendOneElement_withIncompatibleElementType() {
String[] array = {"foo"};
Object[] result = ObjectArrays.<Object>concat(array, 0);
assertThat(result).asList().containsExactly("foo", 0).inOrder();
}

public void testEmptyArrayToEmpty() {
doTestNewArrayEquals(new Object[0], 0);
}
Expand Down
25 changes: 21 additions & 4 deletions guava/src/com/google/common/collect/ObjectArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,18 @@ private ObjectArrays() {}
* the first position, and the elements of {@code array} occupying the remaining elements.
*/
public static <T extends @Nullable Object> T[] concat(@ParametricNullness T element, T[] array) {
T[] result = newArray(array, array.length + 1);
Object[] result;
if (element == null || array.getClass().getComponentType().isInstance(element)) {
result = newArray(array, array.length + 1);
} else {
result = new Object[array.length + 1];
}
result[0] = element;
arraycopy(array, 0, result, 1, array.length);
return result;
@SuppressWarnings(
"unchecked") // safe: result's component type accepts `element` and all of `array`
T[] typedResult = (T[]) result;
return typedResult;
}

/**
Expand All @@ -108,9 +116,18 @@ private ObjectArrays() {}
* array}, plus {@code element} occupying the last position.
*/
public static <T extends @Nullable Object> T[] concat(T[] array, @ParametricNullness T element) {
T[] result = Arrays.copyOf(array, array.length + 1);
Object[] result;
if (element == null || array.getClass().getComponentType().isInstance(element)) {
result = Arrays.copyOf(array, array.length + 1);
} else {
result = new Object[array.length + 1];
arraycopy(array, 0, result, 0, array.length);
}
result[array.length] = element;
return result;
@SuppressWarnings(
"unchecked") // safe: result's component type accepts `element` and all of `array`
T[] typedResult = (T[]) result;
return typedResult;
}

/**
Expand Down