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

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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy