@@ -465,17 +465,13 @@ public Boolean getFeatureVariableBoolean(@Nonnull String featureKey,
465
465
return null ;
466
466
}
467
467
468
- String variableValue = getFeatureVariableValueForType (
469
- featureKey ,
470
- variableKey ,
471
- userId ,
472
- attributes ,
473
- FeatureVariable .VariableType .BOOLEAN
474
- );
475
- if (variableValue != null ) {
476
- return Boolean .parseBoolean (variableValue );
477
- }
478
- return null ;
468
+ return getFeatureVariableValueForType (
469
+ featureKey ,
470
+ variableKey ,
471
+ userId ,
472
+ attributes ,
473
+ FeatureVariable .VariableType .BOOLEAN
474
+ );
479
475
}
480
476
481
477
/**
@@ -514,20 +510,19 @@ public Double getFeatureVariableDouble(@Nonnull String featureKey,
514
510
return null ;
515
511
}
516
512
517
- String variableValue = getFeatureVariableValueForType (
518
- featureKey ,
519
- variableKey ,
520
- userId ,
521
- attributes ,
522
- FeatureVariable .VariableType .DOUBLE
523
- );
524
- if (variableValue != null ) {
525
- try {
526
- return Double .parseDouble (variableValue );
527
- } catch (NumberFormatException exception ) {
528
- logger .error ("NumberFormatException while trying to parse \" " + variableValue +
529
- "\" as Double. " + exception );
530
- }
513
+ Double variableValue = null ;
514
+ try {
515
+ variableValue = getFeatureVariableValueForType (
516
+ featureKey ,
517
+ variableKey ,
518
+ userId ,
519
+ attributes ,
520
+ FeatureVariable .VariableType .DOUBLE
521
+ );
522
+ return variableValue ;
523
+ } catch (Exception exception ) {
524
+ logger .error ("NumberFormatException while trying to parse \" " + variableValue +
525
+ "\" as Double. " + exception );
531
526
}
532
527
return null ;
533
528
}
@@ -567,21 +562,19 @@ public Integer getFeatureVariableInteger(@Nonnull String featureKey,
567
562
logger .error ("Optimizely instance is not valid, failing getFeatureVariableInteger call." );
568
563
return null ;
569
564
}
570
-
571
- String variableValue = getFeatureVariableValueForType (
572
- featureKey ,
573
- variableKey ,
574
- userId ,
575
- attributes ,
576
- FeatureVariable .VariableType .INTEGER
577
- );
578
- if (variableValue != null ) {
579
- try {
580
- return Integer .parseInt (variableValue );
581
- } catch (NumberFormatException exception ) {
582
- logger .error ("NumberFormatException while trying to parse \" " + variableValue +
583
- "\" as Integer. " + exception .toString ());
565
+ try {
566
+ Integer variableValue = getFeatureVariableValueForType (
567
+ featureKey ,
568
+ variableKey ,
569
+ userId ,
570
+ attributes ,
571
+ FeatureVariable .VariableType .INTEGER
572
+ );
573
+ if (variableValue != null ) {
574
+ return variableValue ;
584
575
}
576
+ } catch (Exception exception ) {
577
+ logger .error ("NumberFormatException while trying to parse value as Integer. " + exception .toString ());
585
578
}
586
579
return null ;
587
580
}
@@ -631,11 +624,11 @@ public String getFeatureVariableString(@Nonnull String featureKey,
631
624
}
632
625
633
626
@ VisibleForTesting
634
- String getFeatureVariableValueForType (@ Nonnull String featureKey ,
635
- @ Nonnull String variableKey ,
636
- @ Nonnull String userId ,
637
- @ Nonnull Map <String , ?> attributes ,
638
- @ Nonnull FeatureVariable .VariableType variableType ) {
627
+ < T extends Object > T getFeatureVariableValueForType (@ Nonnull String featureKey ,
628
+ @ Nonnull String variableKey ,
629
+ @ Nonnull String userId ,
630
+ @ Nonnull Map <String , ?> attributes ,
631
+ @ Nonnull FeatureVariable .VariableType variableType ) {
639
632
if (featureKey == null ) {
640
633
logger .warn ("The featureKey parameter must be nonnull." );
641
634
return null ;
@@ -668,6 +661,7 @@ String getFeatureVariableValueForType(@Nonnull String featureKey,
668
661
String variableValue = variable .getDefaultValue ();
669
662
Map <String , ?> copiedAttributes = copyAttributes (attributes );
670
663
FeatureDecision featureDecision = decisionService .getVariationForFeature (featureFlag , userId , copiedAttributes );
664
+ Boolean featureEnabled = false ;
671
665
if (featureDecision .variation != null ) {
672
666
if (featureDecision .variation .getFeatureEnabled ()) {
673
667
FeatureVariableUsageInstance featureVariableUsageInstance =
@@ -683,14 +677,61 @@ String getFeatureVariableValueForType(@Nonnull String featureKey,
683
677
featureKey , featureDecision .variation .getKey (), variableValue , variableKey
684
678
);
685
679
}
680
+ featureEnabled = featureDecision .variation .getFeatureEnabled ();
686
681
} else {
687
682
logger .info ("User \" {}\" was not bucketed into any variation for feature flag \" {}\" . " +
688
683
"The default value \" {}\" for \" {}\" is being returned." ,
689
684
userId , featureKey , variableValue , variableKey
690
685
);
691
686
}
692
687
693
- return variableValue ;
688
+ Object convertedValue = convertStringToType (variableValue , variableType );
689
+
690
+ DecisionNotification decisionNotification = DecisionNotification .newFeatureVariableBuilder ()
691
+ .withUserId (userId )
692
+ .withAttributes (copiedAttributes )
693
+ .withFeatureKey (featureKey )
694
+ .withFeatureEnabled (featureEnabled )
695
+ .withVariableKey (variableKey )
696
+ .withVariableType (variableType )
697
+ .withVariableValue (convertedValue )
698
+ .withFeatureDecision (featureDecision )
699
+ .build ();
700
+
701
+
702
+ notificationCenter .sendNotifications (decisionNotification );
703
+
704
+ return (T ) convertedValue ;
705
+ }
706
+
707
+ // Helper method which takes type and variable value and convert it to object to use in Listener DecisionInfo object variable value
708
+ @ VisibleForTesting
709
+ Object convertStringToType (String variableValue , FeatureVariable .VariableType type ) {
710
+ if (variableValue != null ) {
711
+ switch (type ) {
712
+ case DOUBLE :
713
+ try {
714
+ return Double .parseDouble (variableValue );
715
+ } catch (NumberFormatException exception ) {
716
+ logger .error ("NumberFormatException while trying to parse \" " + variableValue +
717
+ "\" as Double. " + exception );
718
+ }
719
+ break ;
720
+ case STRING :
721
+ return variableValue ;
722
+ case BOOLEAN :
723
+ return Boolean .parseBoolean (variableValue );
724
+ case INTEGER :
725
+ try {
726
+ return Integer .parseInt (variableValue );
727
+ } catch (NumberFormatException exception ) {
728
+ logger .error ("NumberFormatException while trying to parse \" " + variableValue +
729
+ "\" as Integer. " + exception .toString ());
730
+ }
731
+ break ;
732
+ }
733
+ }
734
+ return null ;
694
735
}
695
736
696
737
/**
0 commit comments