Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes the wrap type adapter factory single class #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/io/gsonfire/GsonFireBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ public GsonBuilder createGsonBuilder(){
}

builder.registerTypeAdapterFactory(new SimpleIterableTypeAdapterFactory());
builder.registerTypeAdapterFactory(new WrapTypeAdapterFactory(wrappedClasses));

for(Map.Entry<Class, Mapper> wrappedEntries: wrappedClasses.entrySet()) {
builder.registerTypeAdapterFactory(new WrapTypeAdapterFactory(wrappedEntries.getKey(), wrappedEntries.getValue()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern here is that if you register two wrappers, one for Animal and another one for Cat, you should be aware that it will wrap with the first registered. Apart from that, looks good to me.

}

return builder;
}
Expand Down
31 changes: 9 additions & 22 deletions src/main/java/io/gsonfire/gson/WrapTypeAdapterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,30 @@
import io.gsonfire.util.Mapper;

import java.io.IOException;
import java.util.Map;

/**
* Created by asanchez on 19/02/16.
*/
public class WrapTypeAdapterFactory<T> implements TypeAdapterFactory {

private final Map<Class<T>, Mapper<T, String>> wrappedClasses;
private final Class<T> clazz;
private final Mapper<T, String> mapper;

public WrapTypeAdapterFactory(Map<Class<T>, Mapper<T, String>> wrappedClasses) {
this.wrappedClasses = wrappedClasses;
public WrapTypeAdapterFactory(Class<T> clazz, Mapper<T, String> mapper) {
this.clazz = clazz;
this.mapper = mapper;
}

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> originalTypeAdapter = gson.getDelegateAdapter(this, type);
final Mapper<T, String> mapper = (Mapper<T, String>) getMostSpecificMapper(type.getRawType());

if (mapper == null) {
return originalTypeAdapter;
} else {
if(clazz.isAssignableFrom(type.getRawType())){
final TypeAdapter<T> originalTypeAdapter = gson.getDelegateAdapter(this, type);
return new NullableTypeAdapter<T>(new WrapperTypeAdapter(mapper, gson, originalTypeAdapter));
} else {
return null;
}
}

private Mapper<T, String> getMostSpecificMapper(Class clazz) {
Mapper<T, String> mostSpecificMapper = null;
Class mostSpecificClass = clazz;
while (mostSpecificClass != null) {
mostSpecificMapper = wrappedClasses.get(mostSpecificClass);
if (mostSpecificMapper != null) {
return mostSpecificMapper;
}
mostSpecificClass = mostSpecificClass.getSuperclass();
}
return null;
}

private class WrapperTypeAdapter<T> extends TypeAdapter<T> {

Expand Down