es.ucm.fdi.gaia.jcolibri.util.ProgressController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jCOLIBRI Show documentation
Show all versions of jCOLIBRI Show documentation
jCOLIBRI is a java framework for the development of Case-Based Reasoning systems.
/**
* ProgressController.java
* jCOLIBRI2 framework.
* @author Juan A. Recio-Garc�a.
* GAIA - Group for Artificial Intelligence Applications
* http://gaia.fdi.ucm.es
* 11/01/2007
*/
package es.ucm.fdi.gaia.jcolibri.util;
import java.util.*;
/**
* This class allows methods to indicate their progress in a long task.
* This is a modification of the Observer pattern where the notifier of the tasks keeps a list of observers and notifies them its progress.
* Here we centralize the managing of the observers in this class simplifing the code of the notifiers.
*
* If a method wants to notify its progress it only has to call to:
*
* - init(myclass, some info, estimated steps)
*
- step(myclass)
*
- ...
*
- step(myclass)
*
- finish(myclass)
*
* On the other side observers must implement the ProgressListener interface and register to recieve the events from a concrete class or all the classes.
* @author Juan A. Recio-Garcia
* @version 1.0
* @see ProgressController
*/
public class ProgressController {
/** Defines unknown number of steps for a progress */
public static final int UNKNOWN_STEPS = -1;
static Hashtable, Collection> listeners = new Hashtable, Collection>();
static Collection listenersEverything = new ArrayList();
/**
* Removes all listeners
*/
public static void clear()
{
listeners.clear();
listenersEverything.clear();
}
/**
* Registers a listener to recieve the progress of a concrete class.
*/
public static void register(ProgressListener pl, Class> c)
{
Collection l = listeners.get(c);
if(l==null)
{
l = new ArrayList();
listeners.put(c, l);
}
l.add(pl);
}
/**
* Registers a listener to recieve the progress of all classes.
*/
public static void register(ProgressListener pl)
{
listenersEverything.add(pl);
}
/**
* Deregisters a listener to recieve the progress of a concrete class.
*/
public static void deregister(ProgressListener pl, Class> c)
{
Collection l = listeners.get(c);
if(l!=null)
l.remove(pl);
}
/**
* Deregisters a listener to recieve the progress of all classes.
*/
public static void deregister(ProgressListener pl)
{
listenersEverything.remove(pl);
}
/**
* Notifies to all the listeners of a class that the progress is begining.
* @param c Class that notifies the progress.
* @param info Some info about the progress.
* @param numberOfSteps Number of steps of the task. If unknown it must be -1.
*/
public static void init(Class> c, String info, int numberOfSteps)
{
Collection ls = listeners.get(c);
if(ls!=null)
for(ProgressListener pl: ls)
pl.init(info, numberOfSteps);
for(ProgressListener pl: listenersEverything)
pl.init(info, numberOfSteps);
}
/**
* Notifies a new step in the task for a concrete class.
*/
public static void step(Class> c)
{
Collection ls = listeners.get(c);
if(ls!=null)
for(ProgressListener pl:ls)
pl.step();
for(ProgressListener pl: listenersEverything)
pl.step();
}
/**
* Finishes the progress of a task.
*/
public static void finish(Class> c)
{
Collection ls = listeners.get(c);
if(ls!=null)
for(ProgressListener pl:ls)
pl.finish();
for(ProgressListener pl: listenersEverything)
pl.finish();
}
}