]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/framework/src/com/ibm/icu/dev/util/ElapsedTimer.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / framework / src / com / ibm / icu / dev / util / ElapsedTimer.java
1 //
2 //  ElapsedTimer.java
3 //
4 //  Created by Steven R. Loomis on 11/11/2005.
5 //  Copyright 2005-2012 IBM. All rights reserved.
6 //
7
8 package com.ibm.icu.dev.util;
9
10 import java.util.Locale;
11
12 import com.ibm.icu.text.MessageFormat;
13 import com.ibm.icu.text.NumberFormat;
14 import com.ibm.icu.text.RuleBasedNumberFormat;
15
16
17 /**
18  * Simple stopwatch timer.
19  * Usage:   { ElapsedTimer et = new ElapsedTimer(); 
20  *            do_some_stuff;  
21  *            System.out.println("It took " + et + " to do stuff."); }
22  *
23  * Advanced:   { ElapsedTimer et = new ElapsedTimer("Thing2's time: {0}");  // messageformat pattern
24  *            do_thing_2();  
25  *            System.out.println(et.toString()); }
26  *
27  * More advanced:  NumberFormat and/or MessageFormat can be provided in the constructor
28  */
29 public final class ElapsedTimer {
30
31     /**
32      * Convenience method to print the elasped time (in milliseconds) 
33      */
34     public static String elapsedTime(long start, long end) {
35         return diffTime(getFormat(), start, end);
36     }
37
38     public static String elapsedTime(long start) {
39         return diffTime(getFormat(), start, System.currentTimeMillis());
40     }
41     
42     // class
43
44     private long startTime = System.currentTimeMillis();
45     private NumberFormat myDurationFormat = null;
46     private MessageFormat myMsgFormat = null;
47
48     public ElapsedTimer() {            
49     }
50     
51     public ElapsedTimer(MessageFormat aMsgFmt) {
52         myMsgFormat = aMsgFmt;
53     }
54
55     public ElapsedTimer(NumberFormat aNumFmt) {
56         myDurationFormat = aNumFmt;
57     }
58     
59     public ElapsedTimer(MessageFormat aMsgFmt, NumberFormat aNumFmt) {
60         myMsgFormat = aMsgFmt;
61         myDurationFormat = aNumFmt;
62     }
63
64     public ElapsedTimer(String pattern) {
65         myMsgFormat = new MessageFormat(pattern);
66     }
67
68     public ElapsedTimer(String pattern, NumberFormat aNumFmt) {
69         myMsgFormat = new MessageFormat(pattern);
70         myDurationFormat = aNumFmt;
71     }
72
73     /**
74      * @return elapsed time in seconds since object creation
75      */
76     public final String toString() { 
77         long endTime = System.currentTimeMillis();
78         String duration = diffTime(myDurationFormat, startTime, endTime);
79         if(myMsgFormat == null) {
80             return duration;
81         } else {
82             return myMsgFormat.format(new Object[] {duration});
83         }
84     }
85
86     private static NumberFormat gFormat = null;
87     
88     private static NumberFormat getFormat() {
89         if(gFormat == null) {
90             gFormat = new RuleBasedNumberFormat(Locale.US,
91                         RuleBasedNumberFormat.DURATION);
92         }
93         return gFormat; 
94     }
95     
96     private static String diffTime(NumberFormat fmt, long start, long end) {
97         if(fmt==null) {
98             fmt = getFormat();
99         }
100         synchronized(fmt) {
101             long age = end - start;
102             long diff = age/1000; // millis per second. Workaround ticket:7936 by using whole number seconds.
103             return fmt.format(diff);
104         }
105     }    
106 }