11/*
2- * Copyright (c) 2023, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2023, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3535
3636abstract class Function {
3737
38+ interface FunctionFactory {
39+ Function newFunction ();
40+ }
41+
3842 public abstract void add (Object value );
3943
4044 public abstract Object result ();
4145
42- public static Function create (Field field ) {
46+ public static FunctionFactory createFactory (Field field ) {
4347 Aggregator aggregator = field .aggregator ;
4448
4549 if (field .grouper != null || aggregator == Aggregator .MISSING ) {
46- return new FirstNonNull ();
50+ return () -> new FirstNonNull ();
4751 }
4852 if (aggregator == Aggregator .LIST ) {
49- return new Container (new ArrayList <>());
53+ return () -> new Container (new ArrayList <>());
5054 }
5155
5256 if (aggregator == Aggregator .SET ) {
53- return new Container (new LinkedHashSet <>());
57+ return () -> new Container (new LinkedHashSet <>());
5458 }
5559
5660 if (aggregator == Aggregator .DIFFERENCE ) {
5761 if (field .timestamp ) {
58- return new TimeDifference ();
62+ return () -> new TimeDifference ();
5963 } else {
60- return new Difference ();
64+ return () -> new Difference ();
6165 }
6266 }
6367
6468 if (aggregator == Aggregator .STANDARD_DEVIATION ) {
6569 if (field .timespan ) {
66- return new TimespanFunction (new StandardDeviation ());
70+ return () -> new TimespanFunction (new StandardDeviation ());
6771 } else {
68- return new StandardDeviation ();
72+ return () -> new StandardDeviation ();
6973 }
7074 }
7175
7276 if (aggregator == Aggregator .MEDIAN ) {
7377 if (field .timespan ) {
74- return new TimespanFunction (new Median ());
78+ return () -> new TimespanFunction (new Median ());
7579 } else {
76- return new Median ();
80+ return () -> new Median ();
7781 }
7882 }
7983
@@ -83,7 +87,6 @@ public static Function create(Field field) {
8387
8488 if (aggregator == Aggregator .P95 ) {
8589 return createPercentile (field , 0.95 );
86-
8790 }
8891 if (aggregator == Aggregator .P99 ) {
8992 return createPercentile (field , 0.99 );
@@ -92,46 +95,46 @@ public static Function create(Field field) {
9295 return createPercentile (field , 0.999 );
9396 }
9497 if (aggregator == Aggregator .MAXIMUM ) {
95- return new Maximum ();
98+ return () -> new Maximum ();
9699 }
97100 if (aggregator == Aggregator .MINIMUM ) {
98- return new Minimum ();
101+ return () -> new Minimum ();
99102 }
100103 if (aggregator == Aggregator .SUM ) {
101104 if (field .timespan ) {
102- return new SumDuration ();
105+ return () -> new SumDuration ();
103106 }
104107 if (field .fractionalType ) {
105- return new SumDouble ();
108+ return () -> new SumDouble ();
106109 }
107110 if (field .integralType ) {
108- return new SumLong ();
111+ return () -> new SumLong ();
109112 }
110113 }
111114
112115 if (aggregator == Aggregator .FIRST ) {
113- return new First ();
116+ return () -> new First ();
114117 }
115118 if (aggregator == Aggregator .LAST_BATCH ) {
116- return new LastBatch (field );
119+ return () -> new LastBatch (field );
117120 }
118121 if (aggregator == Aggregator .LAST ) {
119- return new Last ();
122+ return () -> new Last ();
120123 }
121124 if (aggregator == Aggregator .AVERAGE ) {
122125 if (field .timespan ) {
123- return new AverageDuration ();
126+ return () -> new AverageDuration ();
124127 } else {
125- return new Average ();
128+ return () -> new Average ();
126129 }
127130 }
128131 if (aggregator == Aggregator .COUNT ) {
129- return new Count ();
132+ return () -> new Count ();
130133 }
131134 if (aggregator == Aggregator .UNIQUE ) {
132- return new Unique ();
135+ return () -> new Unique ();
133136 }
134- return new Null ();
137+ return () -> new Null ();
135138 }
136139
137140 // **** AVERAGE ****
@@ -507,12 +510,11 @@ public Object result() {
507510 }
508511
509512 // **** PERCENTILE ****
510- private static Function createPercentile (Field field , double percentile ) {
511- Percentile p = new Percentile (percentile );
513+ private static FunctionFactory createPercentile (Field field , double percentile ) {
512514 if (field .timespan ) {
513- return new TimespanFunction (p );
515+ return () -> new TimespanFunction (new Percentile ( percentile ) );
514516 } else {
515- return p ;
517+ return () -> new Percentile ( percentile ) ;
516518 }
517519 }
518520
0 commit comments