Make SegmentDownloadAction constructor keys parameter simpler

Removed option to pass null keys parameter.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=185134822
This commit is contained in:
eguven 2018-02-09 06:36:19 -08:00 committed by Oliver Woodman
parent 4916baabf0
commit 2c2aaf0a4b
4 changed files with 19 additions and 36 deletions

View File

@ -40,17 +40,12 @@ public abstract class SegmentDownloadAction<K> extends DownloadAction {
public DownloadAction readFromStream(int version, DataInputStream input) throws IOException { public DownloadAction readFromStream(int version, DataInputStream input) throws IOException {
Uri manifestUri = Uri.parse(input.readUTF()); Uri manifestUri = Uri.parse(input.readUTF());
String data = input.readUTF(); String data = input.readUTF();
boolean removeAction = input.readBoolean();
int keyCount = input.readInt(); int keyCount = input.readInt();
boolean removeAction = keyCount == -1; K[] keys = createKeyArray(keyCount);
K[] keys;
if (removeAction) {
keys = null;
} else {
keys = createKeyArray(keyCount);
for (int i = 0; i < keyCount; i++) { for (int i = 0; i < keyCount; i++) {
keys[i] = readKey(input); keys[i] = readKey(input);
} }
}
return createDownloadAction(manifestUri, removeAction, data, keys); return createDownloadAction(manifestUri, removeAction, data, keys);
} }
@ -74,12 +69,15 @@ public abstract class SegmentDownloadAction<K> extends DownloadAction {
* @param manifestUri The {@link Uri} of the manifest to be downloaded. * @param manifestUri The {@link Uri} of the manifest to be downloaded.
* @param removeAction Whether the data will be removed. If {@code false} it will be downloaded. * @param removeAction Whether the data will be removed. If {@code false} it will be downloaded.
* @param data Optional custom data for this action. If null, an empty string is used. * @param data Optional custom data for this action. If null, an empty string is used.
* @param keys Keys of representations to be downloaded. If empty or null, all representations are * @param keys Keys of representations to be downloaded. If empty, all representations are
* downloaded. If {@code removeAction} is true, this is ignored. * downloaded. If {@code removeAction} is true, {@code keys} should be an empty array.
*/ */
protected SegmentDownloadAction(Uri manifestUri, boolean removeAction, String data, K[] keys) { protected SegmentDownloadAction(Uri manifestUri, boolean removeAction, String data, K[] keys) {
super(data); super(data);
Assertions.checkArgument(!removeAction || keys == null || keys.length == 0); Assertions.checkNotNull(keys);
if (removeAction) {
Assertions.checkArgument(keys.length == 0);
}
this.manifestUri = manifestUri; this.manifestUri = manifestUri;
this.keys = keys; this.keys = keys;
this.removeAction = removeAction; this.removeAction = removeAction;
@ -94,15 +92,12 @@ public abstract class SegmentDownloadAction<K> extends DownloadAction {
public final void writeToStream(DataOutputStream output) throws IOException { public final void writeToStream(DataOutputStream output) throws IOException {
output.writeUTF(manifestUri.toString()); output.writeUTF(manifestUri.toString());
output.writeUTF(getData()); output.writeUTF(getData());
if (isRemoveAction()) { output.writeBoolean(removeAction);
output.writeInt(-1);
} else {
output.writeInt(keys.length); output.writeInt(keys.length);
for (K key : keys) { for (K key : keys) {
writeKey(output, key); writeKey(output, key);
} }
} }
}
/** Serializes the {@code key} into the {@code output}. */ /** Serializes the {@code key} into the {@code output}. */
protected abstract void writeKey(DataOutputStream output, K key) throws IOException; protected abstract void writeKey(DataOutputStream output, K key) throws IOException;
@ -124,11 +119,9 @@ public abstract class SegmentDownloadAction<K> extends DownloadAction {
} }
SegmentDownloadAction<?> that = (SegmentDownloadAction<?>) o; SegmentDownloadAction<?> that = (SegmentDownloadAction<?>) o;
return manifestUri.equals(that.manifestUri) return manifestUri.equals(that.manifestUri)
&& (keys == null || keys.length == 0 && removeAction == that.removeAction
? (that.keys == null || that.keys.length == 0) && keys.length == that.keys.length
: (that.keys != null && Arrays.asList(keys).containsAll(Arrays.asList(that.keys));
&& that.keys.length == keys.length
&& Arrays.asList(keys).containsAll(Arrays.asList(that.keys))));
} }
@Override @Override

View File

@ -101,10 +101,10 @@ public abstract class SegmentDownloader<M, K> implements Downloader {
/** /**
* Selects multiple representations pointed to by the keys for downloading, checking status. Any * Selects multiple representations pointed to by the keys for downloading, checking status. Any
* previous selection is cleared. If keys are null or empty, all representations are downloaded. * previous selection is cleared. If keys array is empty, all representations are downloaded.
*/ */
public final void selectRepresentations(K[] keys) { public final void selectRepresentations(K[] keys) {
this.keys = (keys != null && keys.length > 0) ? keys.clone() : null; this.keys = keys.length > 0 ? keys.clone() : null;
resetCounters(); resetCounters();
} }

View File

@ -184,11 +184,6 @@ public class DashDownloaderTest extends InstrumentationTestCase {
// select something random // select something random
dashDownloader.selectRepresentations(new RepresentationKey[] {new RepresentationKey(0, 0, 0)}); dashDownloader.selectRepresentations(new RepresentationKey[] {new RepresentationKey(0, 0, 0)});
// clear selection // clear selection
dashDownloader.selectRepresentations(null);
dashDownloader.download(null);
assertCachedData(cache, fakeDataSet);
dashDownloader.remove();
dashDownloader.selectRepresentations(new RepresentationKey[0]); dashDownloader.selectRepresentations(new RepresentationKey[0]);
dashDownloader.download(null); dashDownloader.download(null);
assertCachedData(cache, fakeDataSet); assertCachedData(cache, fakeDataSet);

View File

@ -156,11 +156,6 @@ public class HlsDownloaderTest extends InstrumentationTestCase {
// select something random // select something random
hlsDownloader.selectRepresentations(new String[] {MEDIA_PLAYLIST_1_URI}); hlsDownloader.selectRepresentations(new String[] {MEDIA_PLAYLIST_1_URI});
// clear selection // clear selection
hlsDownloader.selectRepresentations(null);
hlsDownloader.download(null);
assertCachedData(cache, fakeDataSet);
hlsDownloader.remove();
hlsDownloader.selectRepresentations(new String[0]); hlsDownloader.selectRepresentations(new String[0]);
hlsDownloader.download(null); hlsDownloader.download(null);
assertCachedData(cache, fakeDataSet); assertCachedData(cache, fakeDataSet);