/*
 *   Copyright 2013-2016 Blocks4J Team (www.blocks4j.org)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package org.blocks4j.feature.toggle.proxy;

import java.lang.reflect.Method;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProbabilisticFeature<T> extends Feature<T> {

    private static final Pattern PROBABILISTIC_VALUE_PATTERN = Pattern.compile("^(\\d+)/(\\d+)$");

    private static final String PROBABILISTIC_VALUE_PROPERTY_FORMAT = "%s#probability";

    private Random random;

    public ProbabilisticFeature() {
        this.random = new Random();
    }

    @Override
    protected boolean isOn(Method method, Object[] args) {
        return super.isOn(method, args) && this.probabilisticAssertion();
    }

    private boolean probabilisticAssertion() {
        boolean isOn = false;

        Set<String> probValue = this.getConfig().getEnabledParameters().get(String.format(PROBABILISTIC_VALUE_PROPERTY_FORMAT, this.getFeatureName()));

        if (probValue.size() == 1) {
            Matcher matcher = PROBABILISTIC_VALUE_PATTERN.matcher(probValue.iterator().next());
            if (matcher.find()) {
                int accept = Integer.valueOf(matcher.group(1));
                int total = Integer.valueOf(matcher.group(2));

                isOn = this.random.nextInt(total) < accept;
            }
        }

        return isOn;
    }
}
@@ -712,16 +712,38 @@
 Method;%0A
+import java.util.Map;%0A
 import j
@@ -1429,12 +1429,11 @@
 n = 
-fals
+tru
 e;%0A%0A
@@ -1444,71 +1444,192 @@
     
-Set%3CString%3E probValue = this.getConfig().getEnabledParameters()
+Map%3CString, Set%3CString%3E%3E enabledParameters = this.getConfig().getEnabledParameters();%0A%0A        if (!enabledParameters.isEmpty()) %7B%0A            Set%3CString%3E probValue = enabledParameters
 .get
@@ -1710,24 +1710,28 @@
 );%0A%0A        
+    
 if (probValu
@@ -1759,16 +1759,20 @@
         
+  
+  
 Matcher 
@@ -1855,24 +1855,28 @@
             
+    
 if (matcher.
@@ -1897,24 +1897,28 @@
             
+  
+  
 int accept =
@@ -1949,24 +1949,28 @@
 .group(1));%0A
+    
             
@@ -2037,16 +2037,20 @@
         
+  
+  
 isOn = t
@@ -2085,16 +2085,110 @@
 accept;%0A
+                %7D%0A            %7D else if (probValue.size() %3E 1)%7B%0A                isOn = false;%0A
         

enhance probabilistic feature configuration. When feature#probability variable is not defined or it is empty, the feature will be activated for all requests