Debian Codegraph @

src/main/java/net/sf/antcontrib/perf/StopWatchTask.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.perf;
17
18 import java.util.Hashtable;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Task;
22
23 /**
24 * Assists in timing tasks and/or targets.
25 * <p>Developed for use with Antelope, migrated to ant-contrib Oct 2003.
26 * @author Dale Anson, [email protected]
27 * @version $Revision: 1.5 $
28 */
29 public class StopWatchTask extends Task {
30
31 // storage for stopwatch name
32 private String name = null;
33
34 // storage for action
35 private String action = null;
36
37 // storage for watches
38 private static Hashtable watches = null;
39
40 // action definitions
41 private static final String STOP = "stop";
42 private static final String START = "start";
43 private static final String ELAPSED = "elapsed";
44 private static final String TOTAL = "total";
45
46
47 public void setName( String name ) {
48 this.name = name;
49 }
50
51 public void setAction( String action ) {
52 action = action.toLowerCase();
53 if ( action.equals( STOP ) ||
54 action.equals( START ) ||
55 action.equals( ELAPSED ) ||
56 action.equals( TOTAL ) ) {
57 this.action = action;
58 }
59 else {
60 throw new BuildException( "invalid action: " + action );
61 }
62 }
63
64 public void execute() {
65 if ( name == null )
66 throw new BuildException( "name is null" );
67 if ( action == null )
68 action = START;
69 if ( watches == null )
70 watches = new Hashtable();
71 StopWatch sw = ( StopWatch ) watches.get( name );
72 if ( sw == null && action.equals( START ) ) {
73 sw = new StopWatch( name );
74 watches.put( name, sw );
75 return ;
76 }
77 if ( sw == null )
78 return ;
79 if ( action.equals( START) ) {
80 sw.start();
81 return;
82 }
83 if ( action.equals( STOP ) ) {
84 sw.stop();
85 return ;
86 }
87 if ( action.equals( TOTAL ) ) {
88 String time = sw.format( sw.total() );
89 log( "[" + name + ": " + time + "]" );
90 getProject().setProperty(name, time);
91 return ;
92 }
93 if ( action.equals( ELAPSED ) ) {
94 String time = sw.format( sw.elapsed() );
95 log( "[" + name + ": " + time + "]" );
96 getProject().setProperty(name, time);
97 return ;
98 }
99 }
100 }