All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netflix.ocelli.algorithm.LinearWeightingStrategy Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
package netflix.ocelli.algorithm;

import java.util.ArrayList;
import java.util.List;

import netflix.ocelli.WeightingStrategy;
import netflix.ocelli.selectors.ClientsAndWeights;
import rx.functions.Func1;

public class LinearWeightingStrategy implements WeightingStrategy {
    
    private Func1 func;

    public LinearWeightingStrategy(Func1 func) {
        this.func = func;
    }
    
    @Override
    public ClientsAndWeights call(List source) {
        List  clients = new ArrayList(source.size());
        List weights = new ArrayList(source.size());
        
        if (source.size() > 0) {
            for (C context : source) {
                clients.add(context);
                weights.add(func.call(context));
            }
    
            int sum = 0;
            for (int i = 0; i < weights.size(); i++) {
                sum += weights.get(i);
                weights.set(i, sum);
            }
        }
        return new ClientsAndWeights(clients, weights);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy