Skip to content

Instantly share code, notes, and snippets.

@mgp
Created July 8, 2015 02:05
Show Gist options
  • Save mgp/fe945c0f15df65eb142a to your computer and use it in GitHub Desktop.
Save mgp/fe945c0f15df65eb142a to your computer and use it in GitHub Desktop.
package org.khanacademy.core.net.downloadmanager;
import org.khanacademy.core.net.downloadmanager.persistence.database.VideoDownloadManagerDatabase;
import org.khanacademy.core.net.downloadmanager.persistence.models.VideoDownloadEntity;
import org.khanacademy.core.topictree.identifiers.ContentItemIdentifier;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import java.util.Map;
/**
* TODO(mgp)
*/
public class CachingVideoDownloadManagerDatabase {
private final VideoDownloadManagerDatabase mDatabase;
private final Map<ContentItemIdentifier, VideoDownloadEntity> mEntities;
public Foo(final VideoDownloadManagerDatabase database) {
mDatabase = Preconditions.checkNotNull(database);
mEntities = Maps.newHashMap();
}
public VideoDownloadEntity createVideoItemDownload(
final ContentItemIdentifier contentItemIdentifier) {
final VideoDownloadEntity videoDownloadEntity =
mDatabase.createVideoItemDownload(contentItemIdentifier);
}
/**
* @see VideoDownloadManagerDatabase#fetchVideoDownloadEntity(ContentItemIdentifier)
*/
public Optional<VideoDownloadEntity> fetchVideoDownloadEntity(
final ContentItemIdentifier contentItemIdentifier) {
final VideoDownloadEntity cachedVideoDownloadEntity = mEntities.get(contentItemIdentifier);
if (cachedVideoDownloadEntity != null) {
return Optional.of(cachedVideoDownloadEntity);
}
final Optional<VideoDownloadEntity> downloadEntityOptional =
mDatabase.fetchVideoDownloadEntity(contentItemIdentifier);
if (downloadEntityOptional.isPresent()) {
VideoDownloadEntity videoDownloadEntity = downloadEntityOptional.get();
mEntities.put(contentItemIdentifier, videoDownloadEntity);
}
return downloadEntityOptional;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment