Skip to content

Commit

Permalink
Review code of TypeDefinitionBuilder (#3064)
Browse files Browse the repository at this point in the history
* Review code of TypeDefinitionBuilder
1. use init method to init builds' list

* use single list for all builders.
Seems like the builder is thread-safe, we can keep them static and final.

* clean code.
  • Loading branch information
carryxyh authored and cvictory committed Jan 25, 2019
1 parent 26f010d commit ed8bb74
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -35,20 +36,16 @@
*/
public class TypeDefinitionBuilder {

private static final ThreadLocal<ArrayList<TypeBuilder>> builders;
private static final TypeBuilder ARRAY_BUILDER = new ArrayTypeBuilder();
private static final TypeBuilder COLLECTION_BUILDER = new CollectionTypeBuilder();
private static final TypeBuilder MAP_BUILDER = new MapTypeBuilder();
private static final TypeBuilder ENUM_BUILDER = new EnumTypeBuilder();

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 List<TypeBuilder> BUILDERS = Arrays.asList(ARRAY_BUILDER, COLLECTION_BUILDER, MAP_BUILDER, ENUM_BUILDER);

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 @@ -58,22 +55,22 @@ public static TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, Type
}

private static TypeBuilder getGenericTypeBuilder(Type type, Class<?> clazz) {
for (TypeBuilder builder : builders.get()) {
for (TypeBuilder builder : BUILDERS) {
if (builder.accept(type, clazz)) {
return builder;
}
}
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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ public boolean accept(Type type, Class<?> clazz) {
if (clazz == null) {
return false;
}

if (clazz.isArray()) {
return true;
}

return false;
return clazz.isArray();
}

@Override
Expand All @@ -47,8 +42,7 @@ public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinit
TypeDefinitionBuilder.build(componentType, componentType, typeCache);

final String canonicalName = clazz.getCanonicalName();
TypeDefinition td = new TypeDefinition(canonicalName);
return td;
return new TypeDefinition(canonicalName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,14 @@
/**
* 2015/1/27.
*/
public class
CollectionTypeBuilder implements TypeBuilder {
public class CollectionTypeBuilder implements TypeBuilder {

@Override
public boolean accept(Type type, Class<?> clazz) {
if (clazz == null) {
return false;
}

if (Collection.class.isAssignableFrom(clazz)) {
return true;
}

return false;
return Collection.class.isAssignableFrom(clazz);
}

@Override
Expand Down Expand Up @@ -72,8 +66,7 @@ public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinit
}
}

TypeDefinition td = new TypeDefinition(type.toString());
return td;
return new TypeDefinition(type.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ public boolean accept(Type type, Class<?> clazz) {
if (clazz == null) {
return false;
}

if (clazz.isEnum()) {
return true;
}

return false;
return clazz.isEnum();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ public boolean accept(Type type, Class<?> clazz) {
if (clazz == null) {
return false;
}

if (Map.class.isAssignableFrom(clazz)) {
return true;
}

return false;
return Map.class.isAssignableFrom(clazz);
}

@Override
Expand Down

0 comments on commit ed8bb74

Please sign in to comment.