/**
 * 
 */
package binpackage;

import exceptions.NoContextException;
import exceptions.ProblemGeneratorException;
import exceptions.ProblemGeneratorNoBinsException;
import exceptions.ProblemGeneratorNoElementsException;


/**
 * Strategy design pattern: This abstract class describes an anonymous problem generator.
 * @author dennis
 */
public abstract class AbstractProblemGenerator extends ProblemWorker {
	
	/**
	 * The constructor initializes the generator instance with all data it needs for operation.
	 * All data is taken from the given context.
	 * 
	 * @param p_cxt Context object with all necessary data
	 */
	public AbstractProblemGenerator(Context p_cxt) {
		super(p_cxt);
	}
	
	/**
	 * This method creates a new problem. It provides the context object with a fresh
	 * list of bins and a fresh list of elements.
	 * 
	 * @throws NoContextException A valid context needs to be provided prior to calling
	 *                            this method.
	 */
	public abstract void generateProblem() throws NoContextException, ProblemGeneratorException;
	
	/**
	 * This method should be called by all problem generators right at the beginning of
	 * the generateProblem() method. Its purpose is to check for invalid start conditions
	 * which apply to all problem generators.
	 */
	protected void checkStartConditions() throws ProblemGeneratorException, NoContextException {
		if (this.cxt == null) {
			throw new NoContextException();
		}
		
		if (this.cxt.getAmountBins() < 1) {
			throw new ProblemGeneratorNoBinsException();
		}
		
		if (this.cxt.getAmountElements() < 1) {
			throw new ProblemGeneratorNoElementsException();
		}
	}
	
}
