diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedRegionTrackerTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedRegionTrackerTest.java index fc4a9cfed6..2f54ae8972 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedRegionTrackerTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedRegionTrackerTest.java @@ -15,12 +15,17 @@ */ package com.google.android.exoplayer2.upstream.cache; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.when; + import android.test.InstrumentationTestCase; import com.google.android.exoplayer2.extractor.ChunkIndex; import com.google.android.exoplayer2.testutil.MockitoUtil; import com.google.android.exoplayer2.util.Util; import java.io.File; import java.io.IOException; +import java.util.TreeSet; import org.mockito.Mock; /** @@ -48,6 +53,8 @@ public final class CachedRegionTrackerTest extends InstrumentationTestCase { protected void setUp() throws Exception { super.setUp(); MockitoUtil.setUpMockito(this); + when(cache.addListener(anyString(), any(Cache.Listener.class))) + .thenReturn(new TreeSet()); tracker = new CachedRegionTracker(cache, CACHE_KEY, CHUNK_INDEX); cacheDir = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest"); index = new CachedContentIndex(cacheDir); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/Cache.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/Cache.java index 80ad698fa4..76481bbdf7 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/Cache.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/Cache.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.upstream.cache; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import java.io.File; import java.io.IOException; @@ -80,15 +81,16 @@ public interface Cache { /** * Registers a listener to listen for changes to a given key. - *

- * No guarantees are made about the thread or threads on which the listener is called, but it is - * guaranteed that listener methods will be called in a serial fashion (i.e. one at a time) and in - * the same order as events occurred. + * + *

No guarantees are made about the thread or threads on which the listener is called, but it + * is guaranteed that listener methods will be called in a serial fashion (i.e. one at a time) and + * in the same order as events occurred. * * @param key The key to listen to. * @param listener The listener to add. * @return The current spans for the key. */ + @NonNull NavigableSet addListener(String key, Listener listener); /** @@ -103,9 +105,10 @@ public interface Cache { * Returns the cached spans for a given cache key. * * @param key The key for which spans should be returned. - * @return The spans for the key. May be null if there are no such spans. + * @return The spans for the key. */ - @Nullable NavigableSet getCachedSpans(String key); + @NonNull + NavigableSet getCachedSpans(String key); /** * Returns all keys in the cache. diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java index c612ea3739..2bf5cde8e0 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java @@ -253,9 +253,6 @@ public final class CacheUtil { /** Removes all of the data in the {@code cache} pointed by the {@code key}. */ public static void remove(Cache cache, String key) { NavigableSet cachedSpans = cache.getCachedSpans(key); - if (cachedSpans == null) { - return; - } for (CacheSpan cachedSpan : cachedSpans) { try { cache.removeSpan(cachedSpan); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedRegionTracker.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedRegionTracker.java index 9559054f6d..9455aed11b 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedRegionTracker.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedRegionTracker.java @@ -50,14 +50,12 @@ public final class CachedRegionTracker implements Cache.Listener { synchronized (this) { NavigableSet cacheSpans = cache.addListener(cacheKey, this); - if (cacheSpans != null) { - // Merge the spans into regions. mergeSpan is more efficient when merging from high to low, - // which is why a descending iterator is used here. - Iterator spanIterator = cacheSpans.descendingIterator(); - while (spanIterator.hasNext()) { - CacheSpan span = spanIterator.next(); - mergeSpan(span); - } + // Merge the spans into regions. mergeSpan is more efficient when merging from high to low, + // which is why a descending iterator is used here. + Iterator spanIterator = cacheSpans.descendingIterator(); + while (spanIterator.hasNext()) { + CacheSpan span = spanIterator.next(); + mergeSpan(span); } } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java index 599474d6c3..ffac8a35f1 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java @@ -134,7 +134,8 @@ public final class SimpleCache implements Cache { @Override public synchronized NavigableSet getCachedSpans(String key) { CachedContent cachedContent = index.get(key); - return cachedContent == null || cachedContent.isEmpty() ? null + return cachedContent == null || cachedContent.isEmpty() + ? new TreeSet() : new TreeSet(cachedContent.getSpans()); } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheAsserts.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheAsserts.java index aa98ad3179..65850a13e7 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheAsserts.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheAsserts.java @@ -101,7 +101,8 @@ import java.util.ArrayList; public static void assertDataNotCached(Cache cache, String... uriStrings) { for (String uriString : uriStrings) { assertWithMessage("There is cached data for '" + uriString + "'") - .that(cache.getCachedSpans(CacheUtil.generateKey(Uri.parse(uriString)))).isNull(); + .that(cache.getCachedSpans(CacheUtil.generateKey(Uri.parse(uriString)))) + .isEmpty(); } } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java index d5894895b1..75a80185b9 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java @@ -77,7 +77,7 @@ public class SimpleCacheTest { assertThat(simpleCache.getKeys()).isEmpty(); NavigableSet cachedSpans = simpleCache.getCachedSpans(KEY_1); - assertThat(cachedSpans == null || cachedSpans.isEmpty()).isTrue(); + assertThat(cachedSpans.isEmpty()).isTrue(); assertThat(simpleCache.getCacheSpace()).isEqualTo(0); assertThat(cacheDir.listFiles()).hasLength(0); @@ -283,7 +283,7 @@ public class SimpleCacheTest { // Although store() has failed, it should remove the first span and add the new one. NavigableSet cachedSpans = simpleCache.getCachedSpans(KEY_1); - assertThat(cachedSpans).isNotNull(); + assertThat(cachedSpans).isNotEmpty(); assertThat(cachedSpans).hasSize(1); assertThat(cachedSpans.pollFirst().position).isEqualTo(15); } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java index 82fff0d4fe..eb53191dc8 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java @@ -16,6 +16,7 @@ package com.google.android.exoplayer2.testutil; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; import android.net.Uri; import android.test.MoreAsserts; @@ -29,7 +30,6 @@ import com.google.android.exoplayer2.upstream.cache.CacheUtil; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; -import junit.framework.Assert; /** * Assertion methods for {@link Cache}. @@ -105,8 +105,9 @@ public final class CacheAsserts { /** Asserts that there is no cache content for the given {@code uriStrings}. */ public static void assertDataNotCached(Cache cache, String... uriStrings) { for (String uriString : uriStrings) { - Assert.assertNull("There is cached data for '" + uriString + "',", - cache.getCachedSpans(CacheUtil.generateKey(Uri.parse(uriString)))); + assertTrue( + "There is cached data for '" + uriString + "',", + cache.getCachedSpans(CacheUtil.generateKey(Uri.parse(uriString))).isEmpty()); } }