Change DataSource.Type to a boolean.

There are only two types at the moment and we can therefore use a boolean.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=203937357
This commit is contained in:
tonihei 2018-07-10 06:37:49 -07:00 committed by Oliver Woodman
parent 18d208ab93
commit 2b1434dfcb
15 changed files with 27 additions and 40 deletions

View file

@ -241,7 +241,7 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource {
Clock clock,
RequestProperties defaultRequestProperties,
boolean handleSetCookieRequests) {
super(DataSource.TYPE_REMOTE);
super(/* isNetwork= */ true);
this.urlRequestCallback = new UrlRequestCallback();
this.cronetEngine = Assertions.checkNotNull(cronetEngine);
this.executor = Assertions.checkNotNull(executor);

View file

@ -120,7 +120,7 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource {
@Nullable TransferListener<? super DataSource> listener,
@Nullable CacheControl cacheControl,
@Nullable RequestProperties defaultRequestProperties) {
super(DataSource.TYPE_REMOTE);
super(/* isNetwork= */ true);
this.callFactory = Assertions.checkNotNull(callFactory);
this.userAgent = userAgent;
this.contentTypePredicate = contentTypePredicate;

View file

@ -43,7 +43,7 @@ public final class RtmpDataSource extends BaseDataSource {
/** @param listener An optional listener. */
public RtmpDataSource(@Nullable TransferListener<? super DataSource> listener) {
super(DataSource.TYPE_REMOTE);
super(/* isNetwork= */ true);
if (listener != null) {
addTransferListener(listener);
}

View file

@ -57,7 +57,7 @@ public final class AssetDataSource extends BaseDataSource {
* @param listener An optional listener.
*/
public AssetDataSource(Context context, @Nullable TransferListener<? super DataSource> listener) {
super(DataSource.TYPE_LOCAL);
super(/* isNetwork= */ false);
this.assetManager = context.getAssets();
if (listener != null) {
addTransferListener(listener);

View file

@ -26,16 +26,16 @@ import java.util.ArrayList;
*/
public abstract class BaseDataSource implements DataSource {
private final @DataSource.Type int type;
private final boolean isNetwork;
private final ArrayList<TransferListener<? super DataSource>> listeners;
/**
* Creates base data source for a data source of the specified type.
* Creates base data source.
*
* @param type The {@link DataSource.Type} of the data source.
* @param isNetwork Whether the data source loads data through a network.
*/
protected BaseDataSource(@DataSource.Type int type) {
this.type = type;
protected BaseDataSource(boolean isNetwork) {
this.isNetwork = isNetwork;
this.listeners = new ArrayList<>(/* initialCapacity= */ 1);
}

View file

@ -35,7 +35,7 @@ public final class ByteArrayDataSource extends BaseDataSource {
* @param data The data to be read.
*/
public ByteArrayDataSource(byte[] data) {
super(DataSource.TYPE_LOCAL);
super(/* isNetwork= */ false);
Assertions.checkNotNull(data);
Assertions.checkArgument(data.length > 0);
this.data = data;

View file

@ -62,7 +62,7 @@ public final class ContentDataSource extends BaseDataSource {
*/
public ContentDataSource(
Context context, @Nullable TransferListener<? super DataSource> listener) {
super(DataSource.TYPE_LOCAL);
super(/* isNetwork= */ false);
this.resolver = context.getContentResolver();
if (listener != null) {
addTransferListener(listener);

View file

@ -34,7 +34,7 @@ public final class DataSchemeDataSource extends BaseDataSource {
private @Nullable byte[] data;
public DataSchemeDataSource() {
super(DataSource.TYPE_LOCAL);
super(/* isNetwork= */ false);
}
@Override

View file

@ -16,12 +16,9 @@
package com.google.android.exoplayer2.upstream;
import android.net.Uri;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -31,15 +28,6 @@ import java.util.Map;
*/
public interface DataSource {
/** Type of a data source. */
@Retention(RetentionPolicy.SOURCE)
@IntDef({TYPE_REMOTE, TYPE_LOCAL})
public @interface Type {}
/** Data source which loads data from a remote (network) source. */
int TYPE_REMOTE = 0;
/** Data source which loads data from a local (on-device) source. */
int TYPE_LOCAL = 1;
/**
* A factory for {@link DataSource} instances.
*/

View file

@ -155,7 +155,7 @@ public class DefaultHttpDataSource extends BaseDataSource implements HttpDataSou
int readTimeoutMillis,
boolean allowCrossProtocolRedirects,
@Nullable RequestProperties defaultRequestProperties) {
super(DataSource.TYPE_REMOTE);
super(/* isNetwork= */ true);
this.userAgent = Assertions.checkNotEmpty(userAgent);
this.contentTypePredicate = contentTypePredicate;
this.requestProperties = new RequestProperties();

View file

@ -47,7 +47,7 @@ public final class FileDataSource extends BaseDataSource {
/** @param listener An optional listener. */
public FileDataSource(@Nullable TransferListener<? super DataSource> listener) {
super(DataSource.TYPE_LOCAL);
super(/* isNetwork= */ false);
if (listener != null) {
addTransferListener(listener);
}

View file

@ -83,7 +83,7 @@ public final class RawResourceDataSource extends BaseDataSource {
*/
public RawResourceDataSource(
Context context, @Nullable TransferListener<? super DataSource> listener) {
super(DataSource.TYPE_LOCAL);
super(/* isNetwork= */ false);
this.resources = context.getResources();
if (listener != null) {
addTransferListener(listener);

View file

@ -86,7 +86,7 @@ public final class UdpDataSource extends BaseDataSource {
@Nullable TransferListener<? super DataSource> listener,
int maxPacketSize,
int socketTimeoutMillis) {
super(DataSource.TYPE_REMOTE);
super(/* isNetwork= */ true);
this.socketTimeoutMillis = socketTimeoutMillis;
packetBuffer = new byte[maxPacketSize];
packet = new DatagramPacket(packetBuffer, 0, maxPacketSize);

View file

@ -31,7 +31,7 @@ public class BaseDataSourceTest {
@Test
public void dataTransfer_withLocalSource_isReported() throws IOException {
TestSource testSource = new TestSource(DataSource.TYPE_LOCAL);
TestSource testSource = new TestSource(/* isNetwork= */ false);
TestTransferListener transferListener = new TestTransferListener();
testSource.addTransferListener(transferListener);
@ -49,7 +49,7 @@ public class BaseDataSourceTest {
@Test
public void dataTransfer_withRemoteSource_isReported() throws IOException {
TestSource testSource = new TestSource(DataSource.TYPE_REMOTE);
TestSource testSource = new TestSource(/* isNetwork= */ true);
TestTransferListener transferListener = new TestTransferListener();
testSource.addTransferListener(transferListener);
@ -67,8 +67,8 @@ public class BaseDataSourceTest {
private static final class TestSource extends BaseDataSource {
public TestSource(@DataSource.Type int type) {
super(type);
public TestSource(boolean isNetwork) {
super(isNetwork);
}
@Override

View file

@ -42,11 +42,10 @@ public class FakeDataSource extends BaseDataSource {
protected final TransferListener<? super DataSource> transferListener;
protected FakeDataSet fakeDataSet;
protected @DataSource.Type int dataSourceType;
protected boolean isNetwork;
public Factory(@Nullable TransferListener<? super DataSource> transferListener) {
this.transferListener = transferListener;
this.dataSourceType = DataSource.TYPE_LOCAL;
}
public final Factory setFakeDataSet(FakeDataSet fakeDataSet) {
@ -54,14 +53,14 @@ public class FakeDataSource extends BaseDataSource {
return this;
}
public final Factory setDataSourceType(@DataSource.Type int dataSourceType) {
this.dataSourceType = dataSourceType;
public final Factory setIsNetwork(boolean isNetwork) {
this.isNetwork = isNetwork;
return this;
}
@Override
public DataSource createDataSource() {
return new FakeDataSource(fakeDataSet, transferListener, dataSourceType);
return new FakeDataSource(fakeDataSet, transferListener, isNetwork);
}
}
@ -80,14 +79,14 @@ public class FakeDataSource extends BaseDataSource {
}
public FakeDataSource(FakeDataSet fakeDataSet) {
this(fakeDataSet, null, DataSource.TYPE_LOCAL);
this(fakeDataSet, null, /* isNetwork= */ false);
}
public FakeDataSource(
FakeDataSet fakeDataSet,
@Nullable TransferListener<? super DataSource> transferListener,
@DataSource.Type int dataSourceType) {
super(dataSourceType);
boolean isNetwork) {
super(isNetwork);
Assertions.checkNotNull(fakeDataSet);
this.fakeDataSet = fakeDataSet;
this.openedDataSpecs = new ArrayList<>();