提交 d427a1dd 编写于 作者: E eguven 提交者: Oliver Woodman

Make Cache.getCachedSpans return empty set rather than null

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=181161289
上级 67d46267
......@@ -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<CacheSpan>());
tracker = new CachedRegionTracker(cache, CACHE_KEY, CHUNK_INDEX);
cacheDir = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest");
index = new CachedContentIndex(cacheDir);
......
......@@ -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.
* <p>
* 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.
*
* <p>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<CacheSpan> 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<CacheSpan> getCachedSpans(String key);
@NonNull
NavigableSet<CacheSpan> getCachedSpans(String key);
/**
* Returns all keys in the cache.
......
......@@ -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<CacheSpan> cachedSpans = cache.getCachedSpans(key);
if (cachedSpans == null) {
return;
}
for (CacheSpan cachedSpan : cachedSpans) {
try {
cache.removeSpan(cachedSpan);
......
......@@ -50,14 +50,12 @@ public final class CachedRegionTracker implements Cache.Listener {
synchronized (this) {
NavigableSet<CacheSpan> 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<CacheSpan> 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<CacheSpan> spanIterator = cacheSpans.descendingIterator();
while (spanIterator.hasNext()) {
CacheSpan span = spanIterator.next();
mergeSpan(span);
}
}
}
......
......@@ -134,7 +134,8 @@ public final class SimpleCache implements Cache {
@Override
public synchronized NavigableSet<CacheSpan> getCachedSpans(String key) {
CachedContent cachedContent = index.get(key);
return cachedContent == null || cachedContent.isEmpty() ? null
return cachedContent == null || cachedContent.isEmpty()
? new TreeSet<CacheSpan>()
: new TreeSet<CacheSpan>(cachedContent.getSpans());
}
......
......@@ -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();
}
}
......
......@@ -77,7 +77,7 @@ public class SimpleCacheTest {
assertThat(simpleCache.getKeys()).isEmpty();
NavigableSet<CacheSpan> 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<CacheSpan> cachedSpans = simpleCache.getCachedSpans(KEY_1);
assertThat(cachedSpans).isNotNull();
assertThat(cachedSpans).isNotEmpty();
assertThat(cachedSpans).hasSize(1);
assertThat(cachedSpans.pollFirst().position).isEqualTo(15);
}
......
......@@ -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());
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册