1 package cz.cuni.amis.pogamut.base.communication.command.react; 2 3 import cz.cuni.amis.pogamut.base.communication.command.IAct; 4 import cz.cuni.amis.pogamut.base.communication.messages.CommandMessage; 5 6 /** 7 * This abstract class allows you to easily hook a specific event-handling behavior. It automatically 8 * register a listener for a specified {@link IWorldCommand} for you and calls {@link CommandReactOnce#react(IWorldCommand)} 9 * method automatically. The {@link CommandReactOnce#react(IWorldCommand)} will be called only once (upon first event received). 10 * <p><p> 11 * If you need to react every time, use {@link CommandReact}. 12 * <p><p> 13 * <p><p> 14 * Use {@link CommandReactOnce#enable()} and {@link CommandReactOnce#disable()} to enable react / disable react. The reaction is enabled 15 * as default. 16 * <b>WARNING:</b>Use as anonymous class, but <b>save it as a field</b> of your class! Note, that we're using weak-references to 17 * listeners and if you do not save pointer to the object, it will be gc()ed! 18 * 19 * @author Jimmy 20 * 21 * @param <COMMAND> 22 */ 23 public abstract class CommandReactOnce<COMMAND extends CommandMessage> extends CommandReact<COMMAND> { 24 25 public CommandReactOnce(Class<COMMAND> commandClass, IAct act) { 26 super(commandClass, act); 27 } 28 29 /** 30 * Disables the reaction. 31 */ 32 @Override 33 protected void postReact(COMMAND event) { 34 super.postReact(event); 35 disable(); 36 } 37 38 }