Skip to content

Commit

Permalink
Review code of TypeDefinitionBuilder
Browse files Browse the repository at this point in the history
1. use init method to init builds' list
  • Loading branch information
carryxyh committed Dec 28, 2018
1 parent 5ea4fa5 commit c26b361
Showing 1 changed file with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@
*/
public class TypeDefinitionBuilder {

private static final ThreadLocal<ArrayList<TypeBuilder>> builders;

static {
builders = new ThreadLocal<ArrayList<TypeBuilder>>();
builders.set(new ArrayList<TypeBuilder>());
builders.get().add(new ArrayTypeBuilder());
builders.get().add(new CollectionTypeBuilder());
builders.get().add(new MapTypeBuilder());
builders.get().add(new EnumTypeBuilder());
}
private static final ThreadLocal<ArrayList<TypeBuilder>> builders = ThreadLocal.withInitial(() -> {
ArrayList<TypeBuilder> l = new ArrayList<>();
l.add(new ArrayTypeBuilder());
l.add(new CollectionTypeBuilder());
l.add(new MapTypeBuilder());
l.add(new EnumTypeBuilder());
return l;
});

public static TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
TypeBuilder builder = getGenericTypeBuilder(type, clazz);
TypeDefinition td = null;
TypeDefinition td;
if (builder != null) {
td = builder.build(type, clazz, typeCache);
} else {
Expand All @@ -66,14 +64,14 @@ private static TypeBuilder getGenericTypeBuilder(Type type, Class<?> clazz) {
return null;
}

private Map<Class<?>, TypeDefinition> typeCache = new HashMap<Class<?>, TypeDefinition>();
private Map<Class<?>, TypeDefinition> typeCache = new HashMap<>();

public TypeDefinition build(Type type, Class<?> clazz) {
return build(type, clazz, typeCache);
}

public List<TypeDefinition> getTypeDefinitions() {
return new ArrayList<TypeDefinition>(typeCache.values());
return new ArrayList<>(typeCache.values());
}

}

0 comments on commit c26b361

Please sign in to comment.