Commit f19ab4aa by kimvde Committed by Ian Baker

Add possibility to iterate over Commands

PiperOrigin-RevId: 364836973
parent 94a4ed79
......@@ -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) {
......
......@@ -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);
}
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment