@@ -37,24 +37,26 @@ class EvaluatorWithHooks implements EvaluatorInterface {
37
37
public EvalResultAndFlag evalAndFlag (String method , String featureKey , LDContext context , LDValue defaultValue , LDValueType requireType , EvaluationOptions options ) {
38
38
// Each hook will have an opportunity to provide series data to carry along to later stages. This list
39
39
// is to track that data.
40
- List <Map > seriesDataList = new ArrayList <>(hooks .size ());
40
+ int size = hooks .size ();
41
+ List <Map > seriesDataList = new ArrayList <>(size );
41
42
42
43
EvaluationSeriesContext seriesContext = new EvaluationSeriesContext (method , featureKey , context , defaultValue );
43
- for (int i = 0 ; i < hooks .size (); i ++) {
44
+ Map <String , Object > emptyMap = Collections .emptyMap ();
45
+ for (int i = 0 ; i < size ; i ++) {
44
46
Hook currentHook = hooks .get (i );
45
47
try {
46
- Map <String , Object > seriesData = currentHook .beforeEvaluation (seriesContext , Collections . emptyMap () );
47
- seriesDataList .add (Collections .unmodifiableMap (seriesData )); // make data immutable
48
+ Map <String , Object > seriesData = currentHook .beforeEvaluation (seriesContext , emptyMap );
49
+ seriesDataList .add (seriesData . isEmpty () ? emptyMap : Collections .unmodifiableMap (seriesData )); // make data immutable
48
50
} catch (Exception e ) {
49
- seriesDataList .add (Collections . emptyMap () ); // since the provided hook failed to execute, we default the series data to an empty map in this case
51
+ seriesDataList .add (emptyMap ); // since the provided hook failed to execute, we default the series data to an empty map in this case
50
52
logger .error ("During evaluation of flag \" {}\" . Stage \" BeforeEvaluation\" of hook \" {}\" reported error: {}" , featureKey , currentHook .getMetadata ().getName (), e .toString ());
51
53
}
52
54
}
53
55
54
56
EvalResultAndFlag result = underlyingEvaluator .evalAndFlag (method , featureKey , context , defaultValue , requireType , options );
55
57
56
58
// Invoke hooks in reverse order and give them back the series data they gave us.
57
- for (int i = hooks . size () - 1 ; i >= 0 ; i --) {
59
+ for (int i = size - 1 ; i >= 0 ; i --) {
58
60
Hook currentHook = hooks .get (i );
59
61
try {
60
62
currentHook .afterEvaluation (seriesContext , seriesDataList .get (i ), result .getResult ().getAnyType ());
0 commit comments