All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nytimes.android.external.fs.FileSystemPersister Maven / Gradle / Ivy

package com.nytimes.android.external.fs;

import com.nytimes.android.external.fs.filesystem.FileSystem;
import com.nytimes.android.external.store.base.Persister;

import javax.annotation.Nonnull;

import okio.BufferedSource;
import rx.Observable;
/**
 * FileSystemPersister is used when persisting to/from file system
 * PathResolver will be used in creating file system paths based on cache keys.
 * Make sure to have keys containing same data resolve to same "path"
 * @param  key type
 */
public final class FileSystemPersister implements Persister {
    private final FSReader fileReader;
    private final FSWriter fileWriter;

    private FileSystemPersister(FileSystem fileSystem, PathResolver pathResolver) {
        fileReader = new FSReader<>(fileSystem, pathResolver);
        fileWriter = new FSWriter<>(fileSystem, pathResolver);
    }

    @Nonnull
    public static  Persister create(FileSystem fileSystem,
                                                          PathResolver pathResolver) {
        if (fileSystem == null) {
            throw new IllegalArgumentException("root file cannot be null.");
        }
        return new FileSystemPersister<>(fileSystem, pathResolver);
    }

    @Nonnull
    @Override
    public Observable read(@Nonnull final T key) {
        return fileReader.read(key);
    }

    @Nonnull
    @Override
    public Observable write(@Nonnull final T key, @Nonnull final BufferedSource data) {
        return fileWriter.write(key, data);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy