org.javasimon.examples.testapp.WeightController Maven / Gradle / Ivy
The newest version!
package org.javasimon.examples.testapp;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import org.javasimon.examples.testapp.test.Action;
import org.javasimon.examples.testapp.test.Controller;
/**
* Class WeightController.
*
* @author Radovan Sninsky
* @since 2.0
*/
public class WeightController implements Controller {
private final Random random = new Random();
private Map actions = new TreeMap();
public WeightController() {
}
public void addAction(Action action, int weight) {
int last = 0;
if (actions.size() > 0) {
for (int x : actions.keySet()) {
last = x;
}
}
actions.put(last + weight, action);
}
public Action next() {
int x = random.nextInt(100);
for (int i : actions.keySet()) {
if (x < i) {
return actions.get(i);
}
}
return null;
}
}