Merge pull request #35 from scottmeschke/scottmeschke/source_persister_and_gson_parser_factories
Added factories for SourcePersisters and the various GsonParser imple…
This commit is contained in:
commit
f8a4a03e81
4 changed files with 113 additions and 15 deletions
12
README.md
12
README.md
|
@ -23,8 +23,8 @@ Let's start by looking at what a fully configured store looks like, we will then
|
|||
```java
|
||||
Store<Foo> Store = ParsingStoreBuilder.<BufferedSource, String>builder()
|
||||
.fetcher(this::ResponseAsSource) //OkHttp responseBody.source()
|
||||
.persister(new SourcePersister(new FileSystemImpl(context.getFilesDir())))
|
||||
.parser(new GsonSourceParser<>(gson, Foo.class))
|
||||
.persister(SourcePersisterFactory.create(context.getFilesDir())
|
||||
.parser(GsonParserFactory.createSourceParser(gson, Foo.class))
|
||||
.open();
|
||||
|
||||
```
|
||||
|
@ -138,7 +138,7 @@ Our example can now be rewritten as:
|
|||
```java
|
||||
Store<Article> Store = ParsingStoreBuilder.<BufferedSource, Article>builder()
|
||||
.nonObservableFetcher(this::getResponse)
|
||||
.parser(new GsonSourceParser<>(gson, Article.class))
|
||||
.parser(GsonParserFactory.createSourceParser(gson, Article.class))
|
||||
.open();
|
||||
```
|
||||
|
||||
|
@ -173,7 +173,7 @@ Now our data flow looks like:
|
|||
return Observable.just(true);
|
||||
}
|
||||
})
|
||||
.parser(new GsonSourceParser<>(gson, String.class))
|
||||
.parser(GsonParserFactory.createSourceParser(gson, String.class))
|
||||
.open();
|
||||
```
|
||||
|
||||
|
@ -192,8 +192,8 @@ We've found the fastest form of persistence is streaming network responses direc
|
|||
```java
|
||||
Store<String> Store = ParsingStoreBuilder.<BufferedSource, String>builder()
|
||||
.nonObservableFetcher(this::ResponseAsSource) //OkHttp responseBody.source()
|
||||
.persister(new SourcePersister(new FileSystemImpl(context.getFilesDir())))
|
||||
.parser(new GsonSourceParser<>(gson, String.class))
|
||||
.persister(SourcePersisterFactory.create(context.getFilesDir()))
|
||||
.parser(GsonParserFactory.createSourceParser(gson, String.class))
|
||||
.open();
|
||||
```
|
||||
|
||||
|
|
|
@ -8,12 +8,12 @@ import android.support.v7.widget.Toolbar;
|
|||
import android.widget.Toast;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.nytimes.android.external.fs.SourcePersister;
|
||||
import com.nytimes.android.external.fs.filesystem.FileSystemFactory;
|
||||
import com.nytimes.android.external.fs.SourcePersisterFactory;
|
||||
import com.nytimes.android.external.store.base.Persister;
|
||||
import com.nytimes.android.external.store.base.Store;
|
||||
import com.nytimes.android.external.store.base.impl.BarCode;
|
||||
import com.nytimes.android.external.store.base.impl.ParsingStoreBuilder;
|
||||
import com.nytimes.android.external.store.middleware.GsonSourceParser;
|
||||
import com.nytimes.android.external.store.middleware.GsonParserFactory;
|
||||
import com.nytimes.android.sample.BuildConfig;
|
||||
import com.nytimes.android.sample.R;
|
||||
import com.nytimes.android.sample.data.model.Children;
|
||||
|
@ -37,7 +37,7 @@ import static android.widget.Toast.makeText;
|
|||
|
||||
public class PersistingStoreActivity extends AppCompatActivity {
|
||||
|
||||
private SourcePersister sourcePersister;
|
||||
private Persister<BufferedSource> persister;
|
||||
private RecyclerView recyclerView;
|
||||
private PostAdapter postAdapter;
|
||||
|
||||
|
@ -49,7 +49,7 @@ public class PersistingStoreActivity extends AppCompatActivity {
|
|||
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
|
||||
|
||||
try {
|
||||
sourcePersister = persister();
|
||||
persister = newPersister();
|
||||
} catch (IOException exception) {
|
||||
throw new RuntimeException(exception);
|
||||
}
|
||||
|
@ -91,13 +91,13 @@ public class PersistingStoreActivity extends AppCompatActivity {
|
|||
private Store<RedditData> provideRedditStore() {
|
||||
return ParsingStoreBuilder.<BufferedSource,RedditData>builder()
|
||||
.fetcher(this::fetcher)
|
||||
.persister(sourcePersister)
|
||||
.parser(new GsonSourceParser<>(provideGson(),RedditData.class))
|
||||
.persister(persister)
|
||||
.parser(GsonParserFactory.createSourceParser(provideGson(),RedditData.class))
|
||||
.open();
|
||||
}
|
||||
|
||||
private SourcePersister persister() throws IOException {
|
||||
return new SourcePersister(FileSystemFactory.create(getApplicationContext().getCacheDir()));
|
||||
private Persister<BufferedSource> newPersister() throws IOException {
|
||||
return SourcePersisterFactory.create(getApplicationContext().getCacheDir());
|
||||
}
|
||||
|
||||
private Observable<BufferedSource> fetcher(BarCode barCode) {
|
||||
|
|
26
filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersisterFactory.java
vendored
Normal file
26
filesystem/src/main/java/com/nytimes/android/external/fs/SourcePersisterFactory.java
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
package com.nytimes.android.external.fs;
|
||||
|
||||
import com.nytimes.android.external.fs.filesystem.FileSystemFactory;
|
||||
import com.nytimes.android.external.store.base.Persister;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import okio.BufferedSource;
|
||||
|
||||
/**
|
||||
* Factory for {@link SourcePersister}
|
||||
*/
|
||||
|
||||
public class SourcePersisterFactory {
|
||||
|
||||
/**
|
||||
* Returns a new {@link BufferedSource} persister with the provided file as the root of the
|
||||
* persistence {@link com.nytimes.android.external.fs.filesystem.FileSystem}.
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Persister<BufferedSource> create(File root) throws IOException {
|
||||
if (root == null) throw new IllegalArgumentException("root file cannot be null.");
|
||||
return new SourcePersister(FileSystemFactory.create(root));
|
||||
}
|
||||
}
|
72
middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonParserFactory.java
vendored
Normal file
72
middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonParserFactory.java
vendored
Normal file
|
@ -0,0 +1,72 @@
|
|||
package com.nytimes.android.external.store.middleware;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.nytimes.android.external.store.base.Parser;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import okio.BufferedSource;
|
||||
|
||||
/**
|
||||
* Factory which returns various Gson {@link Parser} implementations.
|
||||
*/
|
||||
public class GsonParserFactory {
|
||||
|
||||
/**
|
||||
* Returns a new Parser which parses from {@link Reader} to the specified type, using
|
||||
* the provided {@link Gson} instance.
|
||||
*/
|
||||
public static <T> Parser<Reader, T> createReaderParser(Gson gson, Class<T> parsedClass) {
|
||||
if (gson == null) throw new IllegalArgumentException("gson cannot be null.");
|
||||
if (parsedClass == null) throw new IllegalArgumentException("parsedClass cannot be null.");
|
||||
return new GsonReaderParser<>(gson, parsedClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Parser which parses from {@link Reader} to the specified type, using
|
||||
* a new default configured {@link Gson} instance.
|
||||
*/
|
||||
public static <T> Parser<Reader, T> createReaderParser(Class<T> parsedClass) {
|
||||
if (parsedClass == null) throw new IllegalArgumentException("parsedClass cannot be null.");
|
||||
return new GsonReaderParser<>(new Gson(), parsedClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Parser which parses from {@link BufferedSource} to the specified type, using
|
||||
* the provided {@link Gson} instance.
|
||||
*/
|
||||
public static <T> Parser<BufferedSource, T> createSourceParser(Gson gson, Class<T> parsedClass) {
|
||||
if (gson == null) throw new IllegalArgumentException("gson cannot be null.");
|
||||
if (parsedClass == null) throw new IllegalArgumentException("parsedClass cannot be null.");
|
||||
return new GsonSourceParser<>(gson, parsedClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Parser which parses from {@link Reader} to the specified type, using
|
||||
* a new default configured {@link Gson} instance.
|
||||
*/
|
||||
public static <T> Parser<BufferedSource, T> createSourceParser(Class<T> parsedClass) {
|
||||
if (parsedClass == null) throw new IllegalArgumentException("parsedClass cannot be null.");
|
||||
return new GsonSourceParser<>(new Gson(), parsedClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Parser which parses from a String to the specified type, using
|
||||
* the provided {@link Gson} instance.
|
||||
*/
|
||||
public static <T> Parser<String, T> createStringParser(Gson gson, Class<T> parsedClass) {
|
||||
if (gson == null) throw new IllegalArgumentException("gson cannot be null.");
|
||||
if (parsedClass == null) throw new IllegalArgumentException("parsedClass cannot be null.");
|
||||
return new GsonStringParser<>(gson, parsedClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Parser which parses from a String to the specified type, using
|
||||
* a new default {@link Gson} instance.
|
||||
*/
|
||||
public static <T> Parser<String, T> createStringParser(Class<T> parsedClass) {
|
||||
if (parsedClass == null) throw new IllegalArgumentException("parsedClass cannot be null.");
|
||||
return new GsonStringParser<>(new Gson(), parsedClass);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue