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

Review code of TypeDefinitionBuilder #3064

Merged
merged 3 commits into from
Jan 25, 2019
Merged
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
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);
cvictory marked this conversation as resolved.
Show resolved Hide resolved

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