package de.fernunihagen.dna.jkn.scalephant.distribution.resource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import de.fernunihagen.dna.jkn.scalephant.distribution.membership.DistributedInstance;

public class RandomResourcePlacementStrategy implements ResourcePlacementStrategy {

	/**
	 * The random generator
	 */
	protected final Random randomGenerator;
	
	public RandomResourcePlacementStrategy() {
		randomGenerator = new Random();
	}
	
	@Override
	public DistributedInstance findSystemToAllocate(final Collection<DistributedInstance> systems) {

		synchronized (systems) {
			final List<DistributedInstance> elements = new ArrayList<DistributedInstance>(systems);
			final int element = Math.abs(randomGenerator.nextInt()) % elements.size();
			return elements.get(element);
		}
		
	}

}

@@ -610,16 +610,143 @@
 ems) %7B%0A%0A
+%09%09if(systems.isEmpty()) %7B%0A%09%09%09throw new IllegalArgumentException(%22Unable to choose a system, list of systems is empty%22);%0A%09%09%7D%0A%09%09%0A
 %09%09synchr
@@ -970,17 +970,15 @@
 t);%0A%09%09%7D%0A
-%09%09
 %0A%09%7D%0A%0A%7D%0A

Throw an exception on empty resource list