From caa46d49e5a549aff26a2c9841e1a1b57676435a Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Fri, 14 Sep 2018 09:21:01 -0700 Subject: [PATCH] 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 --- .../exoplayer2/source/ConcatenatingMediaSource.java | 2 +- .../java/com/google/android/exoplayer2/util/Util.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java index d1e111515d..30e764dfe5 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ConcatenatingMediaSource.java @@ -313,7 +313,7 @@ public class ConcatenatingMediaSource extends CompositeMediaSourceDoes nothing if the provided range is valid and {@code fromIndex == toIndex}. + * * @param list The List to remove the range from. * @param fromIndex The first index to be removed (inclusive). * @param toIndex The last index to be removed (exclusive). + * @throws IllegalArgumentException If {@code fromIndex} < 0, {@code toIndex} > {@code + * list.size()}, or {@code fromIndex} > {@code toIndex}. */ public static void removeRange(List list, int fromIndex, int toIndex) { - list.subList(fromIndex, toIndex).clear(); + 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(); + } } /**