diff --git a/src/main/java/com/android/volley/toolbox/DiskBasedAsyncCache.java b/src/main/java/com/android/volley/toolbox/DiskBasedAsyncCache.java index 52c28458..18112a9d 100644 --- a/src/main/java/com/android/volley/toolbox/DiskBasedAsyncCache.java +++ b/src/main/java/com/android/volley/toolbox/DiskBasedAsyncCache.java @@ -59,16 +59,10 @@ public class DiskBasedAsyncCache extends AsyncCache { /** * Constructs an instance of the DiskBasedAsyncCache at the specified directory. * - * @param rootDirectory The root directory of the cache. + * @param rootDirectorySupplier The root directory supplier of the cache. */ - public DiskBasedAsyncCache(final File rootDirectory, int maxCacheSizeInBytes) { - mRootDirectorySupplier = - new FileSupplier() { - @Override - public File get() { - return rootDirectory; - } - }; + private DiskBasedAsyncCache(FileSupplier rootDirectorySupplier, int maxCacheSizeInBytes) { + mRootDirectorySupplier = rootDirectorySupplier; mMaxCacheSizeInBytes = maxCacheSizeInBytes; } @@ -411,4 +405,58 @@ private void deleteFileAndInvokeCallback( mTotalSize = DiskBasedCacheUtility.removeEntry(key, mTotalSize, mEntries); callback.onGetComplete(null); } + + /** + * Builder is used to build an instance of {@link DiskBasedAsyncCache} from values configured by + * the setters. + */ + public static class Builder { + @Nullable private FileSupplier rootDirectorySupplier = null; + @Nullable private File rootDirectory = null; + private int maxCacheSizeInBytes = DiskBasedCacheUtility.DEFAULT_DISK_USAGE_BYTES; + + /** + * Sets the root directory of the cache. Must be called if {@link + * Builder#setRootDirectorySupplier(FileSupplier)} is not. + */ + public Builder setRootDirectory(File rootDirectory) { + this.rootDirectory = rootDirectory; + return this; + } + + /** + * Sets the root directory supplier of the cache. Must be called if {@link + * Builder#setRootDirectory(File)} is not. + */ + public Builder setRootDirectorySupplier(FileSupplier rootDirectorySupplier) { + this.rootDirectorySupplier = rootDirectorySupplier; + return this; + } + + /** + * Sets the max size of the cache in bytes. Will default to {@link + * DiskBasedCacheUtility#DEFAULT_DISK_USAGE_BYTES} if not called. + */ + public Builder setMaxCacheSizeInBytes(int maxCacheSizeInBytes) { + this.maxCacheSizeInBytes = maxCacheSizeInBytes; + return this; + } + + /** Builds a DiskBasedAsyncCache from the provided parameters. */ + public DiskBasedAsyncCache build() { + if (rootDirectory == null && rootDirectorySupplier == null) { + throw new IllegalArgumentException("Must set either file or supplier"); + } + if (rootDirectorySupplier == null) { + rootDirectorySupplier = + new FileSupplier() { + @Override + public File get() { + return rootDirectory; + } + }; + } + return new DiskBasedAsyncCache(rootDirectorySupplier, maxCacheSizeInBytes); + } + } } diff --git a/src/test/java/com/android/volley/toolbox/DiskBasedAsyncCacheTest.java b/src/test/java/com/android/volley/toolbox/DiskBasedAsyncCacheTest.java index 839f37ca..b6e92c02 100644 --- a/src/test/java/com/android/volley/toolbox/DiskBasedAsyncCacheTest.java +++ b/src/test/java/com/android/volley/toolbox/DiskBasedAsyncCacheTest.java @@ -68,7 +68,11 @@ public void onWriteComplete() { } }; // Initialize empty cache - cache = new DiskBasedAsyncCache(temporaryFolder.getRoot(), MAX_SIZE); + cache = + new DiskBasedAsyncCache.Builder() + .setRootDirectory(temporaryFolder.getRoot()) + .setMaxCacheSizeInBytes(MAX_SIZE) + .build(); cache.initialize(futureCallback); future.get(); } @@ -310,7 +314,11 @@ public void testReinitialize() throws ExecutionException, InterruptedException { Cache.Entry entry = CacheTestUtils.randomData(1023); putEntry("key", entry).get(); - final AsyncCache copy = new DiskBasedAsyncCache(temporaryFolder.getRoot(), MAX_SIZE); + final AsyncCache copy = + new DiskBasedAsyncCache.Builder() + .setRootDirectory(temporaryFolder.getRoot()) + .setMaxCacheSizeInBytes(MAX_SIZE) + .build(); final CompletableFuture getEntry = new CompletableFuture<>(); copy.initialize( new AsyncCache.OnWriteCompleteCallback() {