view src/samer/tools/SubrateTask.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
/*
 *	Copyright (c) 2001, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.tools;

/**
  *		This is a task that runs a single child task
  *		every N iterations, that is, at some integer
  *      sub-multiple of the main loop rate
  */

public class SubrateTask implements Task, java.io.Serializable
{
	private Task	task;
	private int		factor;	// subrate factor
	private int		count;

	protected SubrateTask() {}
	public SubrateTask(int f) { factor=f; }
	public SubrateTask(int f, Task t) { factor=f; task=t; count=factor; }

	public int  getFactor() { return factor; }
	public void setFactor(int f) {
		factor=(f==0? 1 : f);	// at least 1
		if (count>factor) count=factor; // cut short current batch
	}

	public void setTask(Task t) { task=t; }
	public Task getTask() { return task; }

	public final void starting() { task.starting(); count=factor; }
	public final void stopping() { task.stopping(); }
	public final void run() throws Exception {
		if ((--count)<=0) { task.run(); count=factor; }
	}

	public void dispose() { task.dispose(); }
	public String toString() { return "Subrate("+factor+"):"+task; }
}