Preconditions for middleware module (#56)
* Add preconditions for parsers * Remove redundant checks * Refactored factory * Add factory tests * Change codestyle * Fix PMD checks
This commit is contained in:
parent
91722691c9
commit
6ecb6da5a7
5 changed files with 104 additions and 50 deletions
|
@ -17,18 +17,21 @@ public final class GsonParserFactory {
|
||||||
private GsonParserFactory() {
|
private GsonParserFactory() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new Parser which parses from {@link Reader} to the specified type, using
|
||||||
|
* a new default configured {@link Gson} instance.
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
public static <T> Parser<Reader, T> createReaderParser(@NonNull Type type) {
|
||||||
|
return createReaderParser(new Gson(), type);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new Parser which parses from {@link Reader} to the specified type, using
|
* Returns a new Parser which parses from {@link Reader} to the specified type, using
|
||||||
* the provided {@link Gson} instance.
|
* the provided {@link Gson} instance.
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public static <T> Parser<Reader, T> createReaderParser(@NonNull Gson gson, @NonNull Type type) {
|
public static <T> Parser<Reader, T> createReaderParser(@NonNull Gson gson, @NonNull Type type) {
|
||||||
if (gson == null) {
|
|
||||||
throw new IllegalArgumentException("gson cannot be null.");
|
|
||||||
}
|
|
||||||
if (type == null) {
|
|
||||||
throw new IllegalArgumentException("type cannot be null.");
|
|
||||||
}
|
|
||||||
return new GsonReaderParser<>(gson, type);
|
return new GsonReaderParser<>(gson, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,11 +40,8 @@ public final class GsonParserFactory {
|
||||||
* a new default configured {@link Gson} instance.
|
* a new default configured {@link Gson} instance.
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public static <T> Parser<Reader, T> createReaderParser(@NonNull Type type) {
|
public static <T> Parser<BufferedSource, T> createSourceParser(@NonNull Type type) {
|
||||||
if (type == null) {
|
return createSourceParser(new Gson(), type);
|
||||||
throw new IllegalArgumentException("type cannot be null.");
|
|
||||||
}
|
|
||||||
return new GsonReaderParser<>(new Gson(), type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,52 +50,25 @@ public final class GsonParserFactory {
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public static <T> Parser<BufferedSource, T> createSourceParser(@NonNull Gson gson, @NonNull Type type) {
|
public static <T> Parser<BufferedSource, T> createSourceParser(@NonNull Gson gson, @NonNull Type type) {
|
||||||
if (gson == null) {
|
|
||||||
throw new IllegalArgumentException("gson cannot be null.");
|
|
||||||
}
|
|
||||||
if (type == null) {
|
|
||||||
throw new IllegalArgumentException("type cannot be null.");
|
|
||||||
}
|
|
||||||
return new GsonSourceParser<>(gson, type);
|
return new GsonSourceParser<>(gson, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a new Parser which parses from {@link Reader} to the specified type, using
|
|
||||||
* a new default configured {@link Gson} instance.
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
public static <T> Parser<BufferedSource, T> createSourceParser(@NonNull Type type) {
|
|
||||||
if (type == null) {
|
|
||||||
throw new IllegalArgumentException("type cannot be null.");
|
|
||||||
}
|
|
||||||
return new GsonSourceParser<>(new Gson(), type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a new Parser which parses from a String to the specified type, using
|
|
||||||
* the provided {@link Gson} instance.
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
public static <T> Parser<String, T> createStringParser(@NonNull Gson gson, @NonNull Type type) {
|
|
||||||
if (gson == null) {
|
|
||||||
throw new IllegalArgumentException("gson cannot be null.");
|
|
||||||
}
|
|
||||||
if (type == null) {
|
|
||||||
throw new IllegalArgumentException("type cannot be null.");
|
|
||||||
}
|
|
||||||
return new GsonStringParser<>(gson, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new Parser which parses from a String to the specified type, using
|
* Returns a new Parser which parses from a String to the specified type, using
|
||||||
* a new default {@link Gson} instance.
|
* a new default {@link Gson} instance.
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public static <T> Parser<String, T> createStringParser(@NonNull Class<T> type) {
|
public static <T> Parser<String, T> createStringParser(@NonNull Class<T> type) {
|
||||||
if (type == null) {
|
return createStringParser(new Gson(), type);
|
||||||
throw new IllegalArgumentException("type cannot be null.");
|
}
|
||||||
}
|
|
||||||
return new GsonStringParser<>(new Gson(), type);
|
/**
|
||||||
|
* Returns a new Parser which parses from a String to the specified type, using
|
||||||
|
* the provided {@link Gson} instance.
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
public static <T> Parser<String, T> createStringParser(@NonNull Gson gson, @NonNull Type type) {
|
||||||
|
return new GsonStringParser<>(gson, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@ import java.lang.reflect.Type;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static com.nytimes.android.external.cache.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public class GsonReaderParser<Parsed> implements Parser<Reader, Parsed> {
|
public class GsonReaderParser<Parsed> implements Parser<Reader, Parsed> {
|
||||||
|
|
||||||
private final Gson gson;
|
private final Gson gson;
|
||||||
|
@ -17,6 +19,8 @@ public class GsonReaderParser<Parsed> implements Parser<Reader, Parsed> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public GsonReaderParser(Gson gson, Type type) {
|
public GsonReaderParser(Gson gson, Type type) {
|
||||||
|
checkNotNull(gson, "Gson can't be null");
|
||||||
|
checkNotNull(type, "Type can't be null");
|
||||||
this.gson = gson;
|
this.gson = gson;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,8 @@ import javax.inject.Inject;
|
||||||
|
|
||||||
import okio.BufferedSource;
|
import okio.BufferedSource;
|
||||||
|
|
||||||
|
import static com.nytimes.android.external.cache.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parser to be used when going from a BufferedSource to any Parsed Type
|
* Parser to be used when going from a BufferedSource to any Parsed Type
|
||||||
* example usage:
|
* example usage:
|
||||||
|
@ -31,6 +33,8 @@ public class GsonSourceParser<Parsed> implements Parser<BufferedSource, Parsed>
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public GsonSourceParser(Gson gson, Type type) {
|
public GsonSourceParser(Gson gson, Type type) {
|
||||||
|
checkNotNull(gson, "Gson can't be null");
|
||||||
|
checkNotNull(type, "Type can't be null");
|
||||||
this.gson = gson;
|
this.gson = gson;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,11 @@ package com.nytimes.android.external.store.middleware;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.nytimes.android.external.store.base.Parser;
|
import com.nytimes.android.external.store.base.Parser;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static com.nytimes.android.external.cache.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public class GsonStringParser<Parsed> implements Parser<String, Parsed> {
|
public class GsonStringParser<Parsed> implements Parser<String, Parsed> {
|
||||||
|
|
||||||
private final Gson gson;
|
private final Gson gson;
|
||||||
|
@ -14,6 +14,8 @@ public class GsonStringParser<Parsed> implements Parser<String, Parsed> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public GsonStringParser(Gson gson, Type parsedClass) {
|
public GsonStringParser(Gson gson, Type parsedClass) {
|
||||||
|
checkNotNull(gson, "Gson can't be null");
|
||||||
|
checkNotNull(parsedClass, "Type can't be null");
|
||||||
this.gson = gson;
|
this.gson = gson;
|
||||||
this.type = parsedClass;
|
this.type = parsedClass;
|
||||||
}
|
}
|
||||||
|
|
71
middleware/src/test/java/com/nytimes/android/external/store/GsonParserFactoryTest.java
vendored
Normal file
71
middleware/src/test/java/com/nytimes/android/external/store/GsonParserFactoryTest.java
vendored
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
package com.nytimes.android.external.store;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.nytimes.android.external.store.middleware.GsonParserFactory;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
public class GsonParserFactoryTest {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException expectedException = ExpectedException.none();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
Type type;
|
||||||
|
private final Gson gson = new Gson();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateParsersProperly() {
|
||||||
|
GsonParserFactory.createReaderParser(gson, type);
|
||||||
|
GsonParserFactory.createSourceParser(gson, type);
|
||||||
|
GsonParserFactory.createStringParser(gson, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldThrowExceptionWhenCreatingReaderWithNullType() {
|
||||||
|
expectedException.expect(NullPointerException.class);
|
||||||
|
GsonParserFactory.createReaderParser(gson, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldThrowExceptionWhenCreatingReaderWithNullGson() {
|
||||||
|
expectedException.expect(NullPointerException.class);
|
||||||
|
GsonParserFactory.createReaderParser(null, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldThrowExceptionWhenCreatingSourceWithNullType() {
|
||||||
|
expectedException.expect(NullPointerException.class);
|
||||||
|
GsonParserFactory.createSourceParser(gson, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldThrowExceptionWhenCreatingSourceWithNullGson() {
|
||||||
|
expectedException.expect(NullPointerException.class);
|
||||||
|
GsonParserFactory.createSourceParser(null, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldThrowExceptionWhenCreatingStringWithNullType() {
|
||||||
|
expectedException.expect(NullPointerException.class);
|
||||||
|
GsonParserFactory.createStringParser(gson, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldThrowExceptionWhenCreatingStringWithNullGson() {
|
||||||
|
expectedException.expect(NullPointerException.class);
|
||||||
|
GsonParserFactory.createStringParser(null, type);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue