Source Code Cross Referenced for TimeStopper.java in  » Parser » runcc » fri » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » runcc » fri.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package fri.util;
002:
003:        /**
004:         Stopwatch, for testing purposes. Provides suspend and resume methods.
005:         */
006:
007:        public class TimeStopper {
008:            private long time1 = 0L, time2 = 0L;
009:            private long timeSum = 0L;
010:            private long lastIntervalStop = 0L;
011:
012:            /** Catch current time. Timer is running. This is a call to resume(). */
013:            public TimeStopper() {
014:                this (true);
015:            }
016:
017:            /** If doStart is false, start() or resume() must be called explicitly. */
018:            public TimeStopper(boolean doStart) {
019:                if (doStart)
020:                    resume();
021:            }
022:
023:            /** Returns true if the timer is running, else it has been stopped or suspended. */
024:            public boolean isRunning() {
025:                return time1 > 0L;
026:            }
027:
028:            /**
029:            	Interrupt timer. Time gets saved. This method works if the timer
030:            	was started or resumed before, else it does nothing.
031:             */
032:            public void suspend() {
033:                if (isRunning()) {
034:                    time2 = System.currentTimeMillis();
035:                    timeSum += time2 - time1;
036:                    time1 = time2 = 0L;
037:                }
038:            }
039:
040:            /**
041:            	Start again after interruption. Pause time will not be added.
042:            	This method works if the timer was suspended before, else it does nothing.
043:             */
044:            public void resume() {
045:                if (isRunning() == false)
046:                    lastIntervalStop = time1 = time2 = System
047:                            .currentTimeMillis();
048:            }
049:
050:            /** Stop the timer and return current time sum. */
051:            public String stop() {
052:                return getTime(true);
053:            }
054:
055:            /** Start the timer after a stop(). The sum of time is forgotten. */
056:            public void start() {
057:                resume();
058:                timeSum = 0L;
059:            }
060:
061:            /**
062:            	Returns current time representation "HH:MM:SS".
063:            	The timer will be stopped for calculation of time and then be resumed.
064:             */
065:            public String getTime() {
066:                return getTime(false);
067:            }
068:
069:            private String getTime(boolean doStop) {
070:                suspend();
071:                long elapsed = timeSum / 1000L;
072:                long sec = elapsed % 60L;
073:                long min = elapsed / 60L;
074:                if (min > 60L)
075:                    min = min % 60L;
076:                long hours = elapsed / 3600L;
077:                if (doStop == false)
078:                    resume();
079:                return hours + ":" + min + ":" + sec;
080:            }
081:
082:            /**
083:            	Returns current time representation in millisconds.
084:            	The timer will be stopped for calculation and then be resumed.
085:             */
086:            public String getTimeMillis() {
087:                suspend();
088:                resume();
089:                return new Long(timeSum).toString();
090:            }
091:
092:            /**
093:            	Stop the timer and return current time sum.
094:             */
095:            public String stopMillis() {
096:                suspend();
097:                return new Long(timeSum).toString();
098:            }
099:
100:            /**
101:            	Returns the time interval since start or the last getInterval() call.
102:             */
103:            public String getInterval() {
104:                long time = System.currentTimeMillis();
105:                long interval = time - lastIntervalStop;
106:                lastIntervalStop = time;
107:                return new Long(interval).toString();
108:            }
109:
110:            // test main
111:
112:            public static final void main(String[] args) {
113:                TimeStopper timer = new TimeStopper();
114:                try {
115:                    Thread.sleep(2000);
116:                } catch (Exception e) {
117:                }
118:                System.err.println(timer.stop());
119:            }
120:
121:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.