View Javadoc

1   /*
2   Karma core - Core of the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.karma.core.cmd.event;
20  
21  import nl.toolforge.karma.core.cmd.Command;
22  
23  /***
24   * Generated when a command was finished (without an exception).
25   *
26   * @author D.A. Smedes
27   * @version $Id: CommandFinishedEvent.java,v 1.2 2004/10/26 22:47:58 hippe Exp $
28   */
29  public final class CommandFinishedEvent extends CommandResponseEvent {
30  
31    private long start = 0L;
32    private long duration = 0L;
33  
34    /***
35     * Creates the event for <code>command</code>.
36     *
37     * @param command The command that has started.
38     */
39    public CommandFinishedEvent(Command command, long startTime) {
40  
41      super(command);
42  
43      start = startTime;
44      duration = System.currentTimeMillis() - start;
45    }
46  
47    /***
48     * Returns the time this event was generated, effectively, the time the command was finished.
49     *
50     * @return Finish time of the command.
51     */
52    public long getTime() {
53      return duration;
54    }
55  
56    /***
57     * Returns a SimpleMessage formatted as <code>[ &lt;command-name&gt; ] Started.</code>.
58     */
59    public Message getEventMessage() {
60  
61      if (duration < 1000) {
62        return new SimpleMessage(MessageHelper.format(getCommand().getName(), "Finished in " + duration + " milliseconds."));
63      } else {
64        if (duration/1000 <= 1) {
65          return new SimpleMessage(MessageHelper.format(getCommand().getName(), "Finished in " + duration/1000 + " second."));
66        } else {
67          return new SimpleMessage(MessageHelper.format(getCommand().getName(), "Finished in " + duration/1000 + " seconds."));
68        }
69      }
70    }
71  }