Browse Source

Replaced Factory interface with Callable.

pull/71/head
Kalle Stenflo 10 years ago
parent
commit
4de672c705
  1. 6
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/Factory.java
  2. 18
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/GsonMappingProvider.java
  3. 11
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java

6
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/Factory.java

@ -1,6 +0,0 @@
package com.jayway.jsonpath.spi.mapper;
public interface Factory<T> {
T createInstance();
}

18
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/GsonMappingProvider.java

@ -23,22 +23,24 @@ import com.jayway.jsonpath.TypeRef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Callable;
public class GsonMappingProvider implements MappingProvider {
private static final Logger logger = LoggerFactory.getLogger(GsonMappingProvider.class);
private final Factory<Gson> factory;
private final Callable<Gson> factory;
public GsonMappingProvider(final Gson gson) {
this(new Factory<Gson>() {
this(new Callable<Gson>() {
@Override
public Gson createInstance() {
public Gson call() {
return gson;
}
});
}
public GsonMappingProvider(Factory<Gson> factory) {
public GsonMappingProvider(Callable<Gson> factory) {
this.factory = factory;
}
@ -46,9 +48,9 @@ public class GsonMappingProvider implements MappingProvider {
super();
try {
Class.forName("com.google.gson.Gson");
this.factory = new Factory<Gson>() {
this.factory = new Callable<Gson>() {
@Override
public Gson createInstance() {
public Gson call() {
return new Gson();
}
};
@ -61,7 +63,7 @@ public class GsonMappingProvider implements MappingProvider {
@Override
public <T> T map(Object source, Class<T> targetType, Configuration configuration) {
try {
return factory.createInstance().getAdapter(targetType).fromJsonTree((JsonElement) source);
return factory.call().getAdapter(targetType).fromJsonTree((JsonElement) source);
} catch (Exception e){
throw new MappingException(e);
}
@ -70,7 +72,7 @@ public class GsonMappingProvider implements MappingProvider {
@Override
public <T> T map(Object source, TypeRef<T> targetType, Configuration configuration) {
try {
return (T) factory.createInstance().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source);
return (T) factory.call().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source);
} catch (Exception e){
throw new MappingException(e);
}

11
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JsonSmartMappingProvider.java

@ -26,6 +26,7 @@ import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.Callable;
public class JsonSmartMappingProvider implements MappingProvider {
@ -48,18 +49,18 @@ public class JsonSmartMappingProvider implements MappingProvider {
}
private final Factory<JsonReader> factory;
private final Callable<JsonReader> factory;
public JsonSmartMappingProvider(final JsonReader jsonReader) {
this(new Factory<JsonReader>() {
this(new Callable<JsonReader>() {
@Override
public JsonReader createInstance() {
public JsonReader call() {
return jsonReader;
}
});
}
public JsonSmartMappingProvider(Factory<JsonReader> factory) {
public JsonSmartMappingProvider(Callable<JsonReader> factory) {
this.factory = factory;
}
@ -79,7 +80,7 @@ public class JsonSmartMappingProvider implements MappingProvider {
}
try {
if(!configuration.jsonProvider().isMap(source) && !configuration.jsonProvider().isArray(source)){
return factory.createInstance().getMapper(targetType).convert(source);
return factory.call().getMapper(targetType).convert(source);
}
String s = configuration.jsonProvider().toJson(source);
return (T) JSONValue.parse(s, targetType);

Loading…
Cancel
Save