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 { ...@@ -690,6 +690,7 @@ public interface Player {
* *
* @param index The index. Must be between 0 (inclusive) and {@link #size()} (exclusive). * @param index The index. Must be between 0 (inclusive) and {@link #size()} (exclusive).
* @return The {@link EventFlags event} at the given index. * @return The {@link EventFlags event} at the given index.
* @throws IndexOutOfBoundsException If index is outside the allowed range.
*/ */
@EventFlags @EventFlags
public int get(int index) { public int get(int index) {
...@@ -787,6 +788,23 @@ public interface Player { ...@@ -787,6 +788,23 @@ public interface Player {
return flags.contains(command); 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 @Override
public boolean equals(@Nullable Object obj) { public boolean equals(@Nullable Object obj) {
if (this == obj) { if (this == obj) {
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2.util; 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 static com.google.android.exoplayer2.util.Assertions.checkState;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
...@@ -152,10 +153,10 @@ public final class ExoFlags { ...@@ -152,10 +153,10 @@ public final class ExoFlags {
* *
* @param index The index. Must be between 0 (inclusive) and {@link #size()} (exclusive). * @param index The index. Must be between 0 (inclusive) and {@link #size()} (exclusive).
* @return The flag at the given index. * @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) { public int get(int index) {
Assertions.checkArgument(index >= 0 && index < size()); checkIndex(index, /* start= */ 0, /* limit= */ size());
return flags.keyAt(index); return flags.keyAt(index);
} }
......
...@@ -122,17 +122,17 @@ public final class ExoFlagsTest { ...@@ -122,17 +122,17 @@ public final class ExoFlagsTest {
} }
@Test @Test
public void get_withNegativeIndex_throwsIllegalArgumentException() { public void get_withNegativeIndex_throwsIndexOutOfBoundsException() {
ExoFlags flags = new ExoFlags.Builder().build(); ExoFlags flags = new ExoFlags.Builder().build();
assertThrows(IllegalArgumentException.class, () -> flags.get(/* index= */ -1)); assertThrows(IndexOutOfBoundsException.class, () -> flags.get(/* index= */ -1));
} }
@Test @Test
public void get_withIndexExceedingSize_throwsIllegalArgumentException() { public void get_withIndexExceedingSize_throwsIndexOutOfBoundsException() {
ExoFlags flags = new ExoFlags.Builder().add(/* flag= */ 0).add(/* flag= */ 123).build(); 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 @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