Add possibility to iterate over Commands

PiperOrigin-RevId: 364836973
This commit is contained in:
kimvde 2021-03-24 17:36:51 +00:00 committed by Ian Baker
parent 94a4ed7981
commit f19ab4aa8b
3 changed files with 25 additions and 6 deletions

View file

@ -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) {

View file

@ -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);
}

View file

@ -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