Rev 32960 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.service.order;import java.util.NavigableMap;import java.util.Random;import java.util.TreeMap;public class RandomCollection<E> {private final NavigableMap<Double, E> map = new TreeMap<>();private final Random random;private double total = 0;public RandomCollection() {this(new Random());}public RandomCollection(Random random) {this.random = random;}public RandomCollection<E> add(double weight, E result) {if (weight <= 0) return this;total += weight;map.put(total, result);return this;}public E next() {double value = random.nextDouble() * total;return map.higherEntry(value).getValue();}public int size() {return map.size();}}