@@ -465,17 +465,13 @@ public Boolean getFeatureVariableBoolean(@Nonnull String featureKey,
465465 return null ;
466466 }
467467
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+ );
479475 }
480476
481477 /**
@@ -514,20 +510,19 @@ public Double getFeatureVariableDouble(@Nonnull String featureKey,
514510 return null ;
515511 }
516512
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 );
531526 }
532527 return null ;
533528 }
@@ -567,21 +562,19 @@ public Integer getFeatureVariableInteger(@Nonnull String featureKey,
567562 logger .error ("Optimizely instance is not valid, failing getFeatureVariableInteger call." );
568563 return null ;
569564 }
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 ;
584575 }
576+ } catch (Exception exception ) {
577+ logger .error ("NumberFormatException while trying to parse value as Integer. " + exception .toString ());
585578 }
586579 return null ;
587580 }
@@ -631,11 +624,11 @@ public String getFeatureVariableString(@Nonnull String featureKey,
631624 }
632625
633626 @ 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 ) {
639632 if (featureKey == null ) {
640633 logger .warn ("The featureKey parameter must be nonnull." );
641634 return null ;
@@ -668,6 +661,7 @@ String getFeatureVariableValueForType(@Nonnull String featureKey,
668661 String variableValue = variable .getDefaultValue ();
669662 Map <String , ?> copiedAttributes = copyAttributes (attributes );
670663 FeatureDecision featureDecision = decisionService .getVariationForFeature (featureFlag , userId , copiedAttributes );
664+ Boolean featureEnabled = false ;
671665 if (featureDecision .variation != null ) {
672666 if (featureDecision .variation .getFeatureEnabled ()) {
673667 FeatureVariableUsageInstance featureVariableUsageInstance =
@@ -683,14 +677,61 @@ String getFeatureVariableValueForType(@Nonnull String featureKey,
683677 featureKey , featureDecision .variation .getKey (), variableValue , variableKey
684678 );
685679 }
680+ featureEnabled = featureDecision .variation .getFeatureEnabled ();
686681 } else {
687682 logger .info ("User \" {}\" was not bucketed into any variation for feature flag \" {}\" . " +
688683 "The default value \" {}\" for \" {}\" is being returned." ,
689684 userId , featureKey , variableValue , variableKey
690685 );
691686 }
692687
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 ;
694735 }
695736
696737 /**
0 commit comments