View Javadoc

1   /*
2    * Copyright 2008 ATG Import Service Project
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  
17  package atg.epub;
18  
19  public class ImportTimer
20  {
21    private String mName;
22    private long mStartTime = 0L;
23    private long mFinishTime = 0L;
24    private boolean mRunning = false;
25    
26    public ImportTimer (String pName)
27    {
28      mName = pName;
29    }
30    
31    public void start ()
32    {
33      if (mRunning)
34      {
35        System.out.println ("Timer: Already running. Stop first.");
36      }
37      else
38      {
39        mStartTime = System.currentTimeMillis();
40        mRunning = true;
41      }
42    }
43  
44    public void stop ()
45    {
46      if (mRunning)
47      {
48        mFinishTime = System.currentTimeMillis();
49        mRunning = false;
50      }
51      else
52      {
53        System.out.println ("Timer: Already stopped.");
54      }
55    }
56  
57    public void reset ()
58    {
59      mStartTime = 0L;
60      mFinishTime = 0L;
61      mRunning = false;
62    }
63    
64    public void display ()
65    {
66      display ("");
67    }
68    
69    public void display (String pExtraText)
70    {
71      long runningTime = 0L;
72      String seconds;
73      String minutes;
74      String hours;
75      
76      if (mRunning)
77      {
78        System.out.println ("Timer: Running. Please stop first.");      
79      }
80      else
81      {
82        runningTime = (mFinishTime - mStartTime) / 1000;
83        
84        seconds = Integer.toString((int)(runningTime % 60));
85        minutes = Integer.toString((int)((runningTime % 3600) / 60));
86        hours = Integer.toString((int)(runningTime / 3600));
87      
88        for (int i = 0; i < 2; i++)
89        {
90          if (seconds.length() < 2)
91          {
92            seconds = "0" + seconds;
93          }
94        
95          if (minutes.length() < 2)
96          {
97            minutes = "0" + minutes;
98          }
99        
100         if (hours.length() < 2)
101         {
102           hours = "0" + hours;
103         }
104       }
105 
106       System.out.println (mName + ": " + hours + ":" + minutes + ":" + seconds + " " + pExtraText);
107     }
108   }
109     
110   public long getTime ()
111   {
112     return (mFinishTime - mStartTime);    
113   }
114 }