File Source: TimedCommand.java
/*
P/P * Method: com.dmdirc.addons.time.TimedCommand__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.time;
24
25 import com.dmdirc.commandparser.parsers.CommandParser;
26 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
27 import com.dmdirc.ui.interfaces.InputWindow;
28
29 import java.util.Timer;
30 import java.util.TimerTask;
31
32 /**
33 * Timed command represents a command that has been scheduled by the user.
34 */
35 public final class TimedCommand extends TimerTask {
36
37 /** The number of repetitions remaining. */
38 private int repetitions;
39
40 /** The command to execute. */
41 private final String command;
42
43 /** The window to use for executing commands. */
44 private final InputWindow origin;
45
46 /** The timer we're using for scheduling this command. */
47 private final Timer timer;
48
49 /**
50 * Creates a new instance of TimedCommand.
51 * @param repetitions The number of times this command will be executed
52 * @param delay The number of seconds between each execution
53 * @param command The command to be executed
54 * @param origin The command window to use for the execution
55 */
56 public TimedCommand(final int repetitions, final int delay,
57 final String command, final InputWindow origin) {
/*
P/P * Method: void com.dmdirc.addons.time.TimedCommand(int, int, String, InputWindow)
*
* Postconditions:
* this.command == command
* init'ed(this.command)
* this.origin == origin
* init'ed(this.origin)
* this.repetitions == repetitions
* init'ed(this.repetitions)
* this.timer == &new Timer(TimedCommand#1)
* new Timer(TimedCommand#1) num objects == 1
*/
58 super();
59
60 this.repetitions = repetitions;
61 this.command = command;
62 this.origin = origin;
63
64 timer = new Timer("Timed Command Timer");
65 timer.schedule(this, delay * 1000L, delay * 1000L);
66 }
67
68 /** {@inheritDoc} */
69 @Override
70 public void run() {
71 CommandParser parser;
/*
P/P * Method: void run()
*
* Preconditions:
* this.repetitions >= -231+1
* (soft) this.timer != null
*
* Presumptions:
* com.dmdirc.commandparser.parsers.GlobalCommandParser:getGlobalCommandParser(...)@73 != null
* com.dmdirc.ui.interfaces.InputWindow:getCommandParser(...)@75 != null
*
* Postconditions:
* this.repetitions == old this.repetitions - 1
* this.repetitions <= 232-2
*
* Test Vectors:
* this.repetitions: {2..232-1}, {-231+1..1}
* this.origin: Inverse{null}, Addr_Set{null}
*/
72 if (origin == null) {
73 parser = GlobalCommandParser.getGlobalCommandParser();
74 } else {
75 parser = origin.getCommandParser();
76 }
77
78 parser.parseCommand(origin, command);
79
80 if (--repetitions <= 0) {
81 timer.cancel();
82 }
83 }
84 }
SofCheck Inspector Build Version : 2.17854
| TimedCommand.java |
2009-Jun-25 01:54:24 |
| TimedCommand.class |
2009-Sep-02 17:04:15 |