diff --git a/library/common/src/main/java/com/google/android/exoplayer2/Player.java b/library/common/src/main/java/com/google/android/exoplayer2/Player.java index e5882543cd..50540ccdb7 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/Player.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/Player.java @@ -690,6 +690,7 @@ public interface Player { * * @param index The index. Must be between 0 (inclusive) and {@link #size()} (exclusive). * @return The {@link EventFlags event} at the given index. + * @throws IndexOutOfBoundsException If index is outside the allowed range. */ @EventFlags public int get(int index) { @@ -787,6 +788,23 @@ public interface Player { return flags.contains(command); } + /** Returns the number of commands in this set. */ + public int size() { + return flags.size(); + } + + /** + * Returns the {@link Command} at the given index. + * + * @param index The index. Must be between 0 (inclusive) and {@link #size()} (exclusive). + * @return The {@link Command} at the given index. + * @throws IndexOutOfBoundsException If index is outside the allowed range. + */ + @Command + public int get(int index) { + return flags.get(index); + } + @Override public boolean equals(@Nullable Object obj) { if (this == obj) { diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/ExoFlags.java b/library/common/src/main/java/com/google/android/exoplayer2/util/ExoFlags.java index 3829515bc4..46c9e486df 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/ExoFlags.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/ExoFlags.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.util; +import static com.google.android.exoplayer2.util.Assertions.checkIndex; import static com.google.android.exoplayer2.util.Assertions.checkState; import android.util.SparseBooleanArray; @@ -152,10 +153,10 @@ public final class ExoFlags { * * @param index The index. Must be between 0 (inclusive) and {@link #size()} (exclusive). * @return The flag at the given index. - * @throws IllegalArgumentException If index is outside the allowed range. + * @throws IndexOutOfBoundsException If index is outside the allowed range. */ public int get(int index) { - Assertions.checkArgument(index >= 0 && index < size()); + checkIndex(index, /* start= */ 0, /* limit= */ size()); return flags.keyAt(index); } diff --git a/library/common/src/test/java/com/google/android/exoplayer2/util/ExoFlagsTest.java b/library/common/src/test/java/com/google/android/exoplayer2/util/ExoFlagsTest.java index 11a7240934..0cc71a2881 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/util/ExoFlagsTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/util/ExoFlagsTest.java @@ -122,17 +122,17 @@ public final class ExoFlagsTest { } @Test - public void get_withNegativeIndex_throwsIllegalArgumentException() { + public void get_withNegativeIndex_throwsIndexOutOfBoundsException() { ExoFlags flags = new ExoFlags.Builder().build(); - assertThrows(IllegalArgumentException.class, () -> flags.get(/* index= */ -1)); + assertThrows(IndexOutOfBoundsException.class, () -> flags.get(/* index= */ -1)); } @Test - public void get_withIndexExceedingSize_throwsIllegalArgumentException() { + public void get_withIndexExceedingSize_throwsIndexOutOfBoundsException() { ExoFlags flags = new ExoFlags.Builder().add(/* flag= */ 0).add(/* flag= */ 123).build(); - assertThrows(IllegalArgumentException.class, () -> flags.get(/* index= */ 2)); + assertThrows(IndexOutOfBoundsException.class, () -> flags.get(/* index= */ 2)); } @Test