Merge "Add documentation for CollectionUtils#arrayAsList"
This commit is contained in:
commit
ed575b62da
2 changed files with 44 additions and 2 deletions
|
@ -22,16 +22,27 @@ import java.util.Collection;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility methods for working with collections.
|
||||||
|
*/
|
||||||
public final class CollectionUtils {
|
public final class CollectionUtils {
|
||||||
private CollectionUtils() {
|
private CollectionUtils() {
|
||||||
// This utility class is not publicly instantiable.
|
// This utility class is not publicly instantiable.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a sub-range of the given array to an ArrayList of the appropriate type.
|
||||||
|
* @param array Array to be converted.
|
||||||
|
* @param start First index inclusive to be converted.
|
||||||
|
* @param end Last index exclusive to be converted.
|
||||||
|
* @throws IllegalArgumentException if start or end are out of range or start > end.
|
||||||
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static <E> ArrayList<E> arrayAsList(@Nonnull final E[] array, final int start,
|
public static <E> ArrayList<E> arrayAsList(@Nonnull final E[] array, final int start,
|
||||||
final int end) {
|
final int end) {
|
||||||
if (start < 0 || start > end || end > array.length) {
|
if (start < 0 || start > end || end > array.length) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException("Invalid start: " + start + " end: " + end
|
||||||
|
+ " with array.length: " + array.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
final ArrayList<E> list = new ArrayList<>(end - start);
|
final ArrayList<E> list = new ArrayList<>(end - start);
|
||||||
|
|
|
@ -28,15 +28,46 @@ import java.util.Collections;
|
||||||
*/
|
*/
|
||||||
@SmallTest
|
@SmallTest
|
||||||
public class CollectionUtilsTests extends AndroidTestCase {
|
public class CollectionUtilsTests extends AndroidTestCase {
|
||||||
|
/**
|
||||||
|
* Tests that {@link CollectionUtils#arrayAsList(Object[],int,int)} fails as expected
|
||||||
|
* with some invalid inputs.
|
||||||
|
*/
|
||||||
|
public void testArrayAsListFailure() {
|
||||||
|
final String[] array = { "0", "1" };
|
||||||
|
// Negative start
|
||||||
|
try {
|
||||||
|
CollectionUtils.arrayAsList(array, -1, 1);
|
||||||
|
fail("Failed to catch start < 0");
|
||||||
|
} catch (final IllegalArgumentException e) {
|
||||||
|
assertEquals("Invalid start: -1 end: 1 with array.length: 2", e.getMessage());
|
||||||
|
}
|
||||||
|
// start > end
|
||||||
|
try {
|
||||||
|
CollectionUtils.arrayAsList(array, 1, -1);
|
||||||
|
fail("Failed to catch start > end");
|
||||||
|
} catch (final IllegalArgumentException e) {
|
||||||
|
assertEquals("Invalid start: 1 end: -1 with array.length: 2", e.getMessage());
|
||||||
|
}
|
||||||
|
// end > array.length
|
||||||
|
try {
|
||||||
|
CollectionUtils.arrayAsList(array, 1, 3);
|
||||||
|
fail("Failed to catch end > array.length");
|
||||||
|
} catch (final IllegalArgumentException e) {
|
||||||
|
assertEquals("Invalid start: 1 end: 3 with array.length: 2", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that {@link CollectionUtils#arrayAsList(Object[],int,int)} gives the expected
|
* Tests that {@link CollectionUtils#arrayAsList(Object[],int,int)} gives the expected
|
||||||
* results for a few valid inputs.
|
* results for a few valid inputs.
|
||||||
*/
|
*/
|
||||||
public void testArrayAsList() {
|
public void testArrayAsList() {
|
||||||
final String[] array = { "0", "1", "2", "3", "4" };
|
|
||||||
final ArrayList<String> empty = new ArrayList<>();
|
final ArrayList<String> empty = new ArrayList<>();
|
||||||
|
assertEquals(empty, CollectionUtils.arrayAsList(new String[] { }, 0, 0));
|
||||||
|
final String[] array = { "0", "1", "2", "3", "4" };
|
||||||
assertEquals(empty, CollectionUtils.arrayAsList(array, 0, 0));
|
assertEquals(empty, CollectionUtils.arrayAsList(array, 0, 0));
|
||||||
assertEquals(empty, CollectionUtils.arrayAsList(array, 1, 1));
|
assertEquals(empty, CollectionUtils.arrayAsList(array, 1, 1));
|
||||||
|
assertEquals(empty, CollectionUtils.arrayAsList(array, array.length, array.length));
|
||||||
final ArrayList<String> expected123 = new ArrayList<>(Arrays.asList("1", "2", "3"));
|
final ArrayList<String> expected123 = new ArrayList<>(Arrays.asList("1", "2", "3"));
|
||||||
assertEquals(expected123, CollectionUtils.arrayAsList(array, 1, 4));
|
assertEquals(expected123, CollectionUtils.arrayAsList(array, 1, 4));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue