Explicitly document empty range removal in Util.removeRange.

Also prevent an unnecessary allocation when the removed range is empty.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=212993427
This commit is contained in:
aquilescanta 2018-09-14 09:21:01 -07:00 committed by Oliver Woodman
parent 1284f9ea4d
commit caa46d49e5
2 changed files with 11 additions and 2 deletions

View File

@ -313,7 +313,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSource<MediaSourceHo
* untouched. This index must be in the range of 0 &lt;= index &lt;= {@link #getSize()}. * untouched. This index must be in the range of 0 &lt;= index &lt;= {@link #getSize()}.
* @param actionOnCompletion A {@link Runnable} which is executed immediately after the media * @param actionOnCompletion A {@link Runnable} which is executed immediately after the media
* source range has been removed from the playlist. * source range has been removed from the playlist.
* @throws IndexOutOfBoundsException When the range is malformed, i.e. {@code fromIndex} &lt; 0, * @throws IllegalArgumentException When the range is malformed, i.e. {@code fromIndex} &lt; 0,
* {@code toIndex} &gt; {@link #getSize()}, {@code fromIndex} &gt; {@code toIndex} * {@code toIndex} &gt; {@link #getSize()}, {@code fromIndex} &gt; {@code toIndex}
*/ */
public final synchronized void removeMediaSourceRange( public final synchronized void removeMediaSourceRange(

View File

@ -253,13 +253,22 @@ public final class Util {
/** /**
* Removes an indexed range from a List. * Removes an indexed range from a List.
* *
* <p>Does nothing if the provided range is valid and {@code fromIndex == toIndex}.
*
* @param list The List to remove the range from. * @param list The List to remove the range from.
* @param fromIndex The first index to be removed (inclusive). * @param fromIndex The first index to be removed (inclusive).
* @param toIndex The last index to be removed (exclusive). * @param toIndex The last index to be removed (exclusive).
* @throws IllegalArgumentException If {@code fromIndex} &lt; 0, {@code toIndex} &gt; {@code
* list.size()}, or {@code fromIndex} &gt; {@code toIndex}.
*/ */
public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) { public static <T> void removeRange(List<T> list, int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) {
throw new IllegalArgumentException();
} else if (fromIndex != toIndex) {
// Checking index inequality prevents an unnecessary allocation.
list.subList(fromIndex, toIndex).clear(); list.subList(fromIndex, toIndex).clear();
} }
}
/** /**
* Casts a nullable variable to a non-null variable without runtime null check. * Casts a nullable variable to a non-null variable without runtime null check.