99import java .util .IdentityHashMap ;
1010import java .util .List ;
1111import java .util .Map ;
12+ import java .util .function .Supplier ;
1213
1314public class ComponentMapGenerator {
1415
1516 private static final ClassName COMPONENT_INTERFACE = ClassName .get ("com.github.elebras1.flecs" , "Component" );
17+ private static final ClassName COMPONENT_VIEW_INTERFACE = ClassName .get ("com.github.elebras1.flecs" , "ComponentView" );
1618 private static final String MAP_PACKAGE = "com.github.elebras1.flecs" ;
1719 private static final String MAP_COMPONENT_CLASS_NAME = "ComponentMap" ;
1820
1921 public JavaFile generateComponentMap (List <TypeElement > components ) {
2022 TypeSpec .Builder mapClass = TypeSpec .classBuilder (MAP_COMPONENT_CLASS_NAME )
2123 .addModifiers (Modifier .PUBLIC , Modifier .FINAL )
2224 .addField (this .createMapField (components .size ()))
25+ .addField (this .createViewMapField (components .size ()))
2326 .addStaticBlock (this .createStaticInitializer (components ))
2427 .addMethod (this .createConstructor ())
25- .addMethod (this .createGetInstanceMethod ());
28+ .addMethod (this .createGetInstanceMethod ())
29+ .addMethod (this .createGetViewMethod ());
2630
2731 return JavaFile .builder (MAP_PACKAGE , mapClass .build ())
2832 .addFileComment ("Generated by ComponentMapGenerator." )
@@ -42,18 +46,32 @@ private FieldSpec createMapField(int componentsSize) {
4246 .build ();
4347 }
4448
49+ private FieldSpec createViewMapField (int componentsSize ) {
50+ ParameterizedTypeName supplierType = ParameterizedTypeName .get (ClassName .get (Supplier .class ), COMPONENT_VIEW_INTERFACE );
51+ ParameterizedTypeName mapType = ParameterizedTypeName .get (
52+ ClassName .get (Map .class ),
53+ ParameterizedTypeName .get (ClassName .get (Class .class ), WildcardTypeName .subtypeOf (Object .class )),
54+ supplierType
55+ );
56+
57+ return FieldSpec .builder (mapType , "VIEW_MAP" , Modifier .PRIVATE , Modifier .STATIC , Modifier .FINAL )
58+ .initializer ("new $T<>($L)" , IdentityHashMap .class , componentsSize )
59+ .build ();
60+ }
61+
4562 private CodeBlock createStaticInitializer (List <TypeElement > components ) {
4663 CodeBlock .Builder builder = CodeBlock .builder ();
4764
4865 for (TypeElement component : components ) {
4966 String packageName = this .getPackageName (component );
5067 String recordName = component .getSimpleName ().toString ();
51- String componentClassName = recordName + "Component" ;
5268
5369 ClassName recordClass = ClassName .get (packageName , recordName );
54- ClassName componentClass = ClassName .get (packageName , componentClassName );
70+ ClassName componentClass = ClassName .get (packageName , recordName + "Component" );
71+ ClassName viewClass = ClassName .get (packageName , recordName + "ComponentView" );
5572
5673 builder .addStatement ("COMPONENT_MAP.put($T.class, $T.getInstance())" , recordClass , componentClass );
74+ builder .addStatement ("VIEW_MAP.put($T.class, $T::new)" , recordClass , viewClass );
5775 }
5876
5977 return builder .build ();
@@ -81,12 +99,23 @@ private MethodSpec createGetInstanceMethod() {
8199 return method .build ();
82100 }
83101
102+ private MethodSpec createGetViewMethod () {
103+ MethodSpec .Builder method = MethodSpec .methodBuilder ("getView" )
104+ .addModifiers (Modifier .PUBLIC , Modifier .STATIC )
105+ .addTypeVariable (TypeVariableName .get ("T" ))
106+ .addParameter (ParameterizedTypeName .get (ClassName .get (Class .class ), TypeVariableName .get ("T" )), "componentClass" )
107+ .returns (COMPONENT_VIEW_INTERFACE )
108+ .addStatement ("$T supplier = VIEW_MAP.get(componentClass)" , ParameterizedTypeName .get (ClassName .get (Supplier .class ), COMPONENT_VIEW_INTERFACE ))
109+ .addStatement ("return supplier != null ? supplier.get() : null" );
110+
111+ return method .build ();
112+ }
113+
84114 private String getPackageName (TypeElement element ) {
85115 Element current = element .getEnclosingElement ();
86116 while (current != null && !(current instanceof PackageElement )) {
87117 current = current .getEnclosingElement ();
88118 }
89119 return current != null ? ((PackageElement ) current ).getQualifiedName ().toString () : "" ;
90120 }
91- }
92-
121+ }
0 commit comments