3636import java .util .Arrays ;
3737import java .util .List ;
3838import java .util .Map ;
39- import java .util .stream . Collectors ;
39+ import java .util .HashMap ;
4040import java .util .stream .Stream ;
4141
4242class CodeBreakdownJsonObject extends JsonObject {
4343
44- private final Feature .AfterCompilationAccess access ;
45-
46- private static final String INFO_NAME = "name" ;
47- private static final List <String > NAMES = Arrays .asList (INFO_NAME , "size" );
44+ private boolean built = false ;
45+ private Feature .AfterCompilationAccess access ;
46+ private final Map <String , Integer > data = new HashMap <>();
4847
4948 CodeBreakdownJsonObject (Feature .AfterCompilationAccess access ) {
5049 this .access = access ;
5150 }
5251
5352 public Map <String , Integer > getData () {
54- return ((FeatureImpl .AfterCompilationAccessImpl ) access ).getCompilationTasks ().stream ()
55- .collect (Collectors .toMap (t -> t .method .format ("%H.%n(%p) %r" ), t -> t .result .getTargetCodeSize ()));
53+ return data ;
5654 }
5755
5856 @ Override
@@ -62,15 +60,35 @@ Stream<String> getNames() {
6260
6361 @ Override
6462 JsonValue getValue (String name ) {
65- return JsonArray .get (((FeatureImpl .AfterCompilationAccessImpl ) access ).getCompilationTasks ().stream ().map (MethodJsonObject ::new ));
63+ return JsonArray .get (data .entrySet ().stream ().map (MethodJsonObject ::new ));
64+ }
65+
66+ @ Override
67+ protected void build () {
68+ if (built ) {
69+ return ;
70+ }
71+ for (CompileQueue .CompileTask task : ((FeatureImpl .AfterCompilationAccessImpl ) access ).getCompilationTasks ()) {
72+ data .merge (task .method .format ("%H.%n(%p) %r" ), task .result .getTargetCodeSize (), Integer ::sum );
73+ }
74+ access = null ;
75+ built = true ;
6676 }
6777
6878 private static class MethodJsonObject extends JsonObject {
79+ private static final String INFO_NAME = "name" ;
80+ private static final List <String > NAMES = Arrays .asList (INFO_NAME , "size" );
6981
70- private final CompileQueue .CompileTask task ;
82+ private final String methodName ;
83+ private final int methodSize ;
84+
85+ MethodJsonObject (Map .Entry <String , Integer > entry ) {
86+ this (entry .getKey (), entry .getValue ());
87+ }
7188
72- MethodJsonObject (CompileQueue .CompileTask task ) {
73- this .task = task ;
89+ MethodJsonObject (String name , int size ) {
90+ this .methodName = name ;
91+ this .methodSize = size ;
7492 }
7593
7694 @ Override
@@ -80,9 +98,9 @@ Stream<String> getNames() {
8098
8199 @ Override
82100 JsonValue getValue (String name ) {
83- return name == INFO_NAME
84- ? JsonString .get (task . method . format ( "%H.%n(%p) %r" ) )
85- : JsonNumber .get (task . result . getTargetCodeSize () );
101+ return INFO_NAME . equals ( name )
102+ ? JsonString .get (this . methodName )
103+ : JsonNumber .get (this . methodSize );
86104 }
87105 }
88106}
0 commit comments