//# 0 errors, 48 messages
//#
/*
    //#TimedCommand.java:1:1: class: com.dmdirc.addons.time.TimedCommand
    //#TimedCommand.java:1:1: method: com.dmdirc.addons.time.TimedCommand.com.dmdirc.addons.time.TimedCommand__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.time;

import com.dmdirc.commandparser.parsers.CommandParser;
import com.dmdirc.commandparser.parsers.GlobalCommandParser;
import com.dmdirc.ui.interfaces.InputWindow;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Timed command represents a command that has been scheduled by the user.
 */
public final class TimedCommand extends TimerTask {
    
    /** The number of repetitions remaining. */
    private int repetitions;
    
    /** The command to execute. */
    private final String command;
    
    /** The window to use for executing commands. */
    private final InputWindow origin;
    
    /** The timer we're using for scheduling this command. */
    private final Timer timer;
    
    /**
     * Creates a new instance of TimedCommand.
     * @param repetitions The number of times this command will be executed
     * @param delay The number of seconds between each execution
     * @param command The command to be executed
     * @param origin The command window to use for the execution
     */
    public TimedCommand(final int repetitions, final int delay,
            final String command, final InputWindow origin) {
        super();
    //#TimedCommand.java:58: method: void com.dmdirc.addons.time.TimedCommand.com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)
    //#input(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): command
    //#input(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): delay
    //#input(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): origin
    //#input(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): repetitions
    //#input(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this
    //#output(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): new Timer(TimedCommand#1) num objects
    //#output(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this.command
    //#output(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this.origin
    //#output(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this.repetitions
    //#output(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this.timer
    //#new obj(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): new Timer(TimedCommand#1)
    //#post(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this.command == command
    //#post(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): init'ed(this.command)
    //#post(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this.origin == origin
    //#post(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): init'ed(this.origin)
    //#post(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this.repetitions == repetitions
    //#post(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): init'ed(this.repetitions)
    //#post(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): this.timer == &new Timer(TimedCommand#1)
    //#post(void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)): new Timer(TimedCommand#1) num objects == 1
        
        this.repetitions = repetitions;
        this.command = command;
        this.origin = origin;
        
        timer = new Timer("Timed Command Timer");
        timer.schedule(this, delay * 1000L, delay * 1000L);
    }
    //#TimedCommand.java:66: end of method: void com.dmdirc.addons.time.TimedCommand.com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)

    /** {@inheritDoc} */
    @Override
    public void run() {
        CommandParser parser;
        if (origin == null) {
    //#TimedCommand.java:72: method: void com.dmdirc.addons.time.TimedCommand.run()
    //#input(void run()): this
    //#input(void run()): this.command
    //#input(void run()): this.origin
    //#input(void run()): this.repetitions
    //#input(void run()): this.timer
    //#output(void run()): this.repetitions
    //#pre[1] (void run()): this.repetitions >= -2_147_483_647
    //#pre[5] (void run()): (soft) this.timer != null
    //#presumption(void run()): com.dmdirc.commandparser.parsers.GlobalCommandParser:getGlobalCommandParser(...)@73 != null
    //#presumption(void run()): com.dmdirc.ui.interfaces.InputWindow:getCommandParser(...)@75 != null
    //#post(void run()): this.repetitions == old this.repetitions - 1
    //#post(void run()): this.repetitions <= 4_294_967_294
    //#test_vector(void run()): this.repetitions: {2..4_294_967_295}, {-2_147_483_647..1}
    //#test_vector(void run()): this.origin: Inverse{null}, Addr_Set{null}
            parser = GlobalCommandParser.getGlobalCommandParser();
    //#TimedCommand.java:73: Warning: method not available - call not analyzed
    //#    call on GlobalCommandParser com.dmdirc.commandparser.parsers.GlobalCommandParser:getGlobalCommandParser()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.time.TimedCommand
    //#    method: void run()
    //#    unanalyzed callee: GlobalCommandParser com.dmdirc.commandparser.parsers.GlobalCommandParser:getGlobalCommandParser()
        } else {
            parser = origin.getCommandParser();
    //#TimedCommand.java:75: Warning: method not available - call not analyzed
    //#    call on CommandParser com.dmdirc.ui.interfaces.InputWindow:getCommandParser()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.time.TimedCommand
    //#    method: void run()
    //#    unanalyzed callee: CommandParser com.dmdirc.ui.interfaces.InputWindow:getCommandParser()
        }
        
        parser.parseCommand(origin, command);
    //#TimedCommand.java:78: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.commandparser.parsers.CommandParser:parseCommand(InputWindow, String)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.time.TimedCommand
    //#    method: void run()
    //#    unanalyzed callee: void com.dmdirc.commandparser.parsers.CommandParser:parseCommand(InputWindow, String)
                
        if (--repetitions <= 0) {
            timer.cancel();
        }
    }
    //#TimedCommand.java:83: end of method: void com.dmdirc.addons.time.TimedCommand.run()
}
    //#output(com.dmdirc.addons.time.TimedCommand__static_init): __Descendant_Table[com/dmdirc/addons/time/TimedCommand]
    //#output(com.dmdirc.addons.time.TimedCommand__static_init): __Dispatch_Table.run()V
    //#post(com.dmdirc.addons.time.TimedCommand__static_init): __Descendant_Table[com/dmdirc/addons/time/TimedCommand] == &__Dispatch_Table
    //#post(com.dmdirc.addons.time.TimedCommand__static_init): __Dispatch_Table.run()V == &run
    //#TimedCommand.java:: end of method: com.dmdirc.addons.time.TimedCommand.com.dmdirc.addons.time.TimedCommand__static_init
    //#TimedCommand.java:: end of class: com.dmdirc.addons.time.TimedCommand
