Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SimpleTimer |
|
| 1.5714285714285714;1.571 | ||||
SimpleTimer$SimpleTimerEnumeration |
|
| 1.5714285714285714;1.571 |
1 | /* $Id: SimpleTimer.java 17888 2010-01-12 21:17:37Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * penyaskito | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.util.logging; | |
40 | ||
41 | import java.util.ArrayList; | |
42 | import java.util.Enumeration; | |
43 | import java.util.List; | |
44 | ||
45 | /** | |
46 | * This class makes it easy to get the time between two or several | |
47 | * points in the code. | |
48 | * | |
49 | * @author Linus Tolke | |
50 | */ | |
51 | 114300 | public class SimpleTimer { |
52 | 900 | private List<Long> points = new ArrayList<Long>(); |
53 | 900 | private List<String> labels = new ArrayList<String>(); |
54 | ||
55 | /** | |
56 | * The constructor. Creates a simple timer. | |
57 | */ | |
58 | 900 | public SimpleTimer() { |
59 | 900 | } |
60 | ||
61 | /** | |
62 | * Mark (Store) the current time. | |
63 | */ | |
64 | public void mark() { | |
65 | 16200 | points.add(new Long(System.currentTimeMillis())); |
66 | 16200 | labels.add(null); |
67 | 16200 | } |
68 | ||
69 | /** | |
70 | * Mark (Store) the current time. | |
71 | * | |
72 | * @param label the mark will be labeled with this string | |
73 | */ | |
74 | public void mark(String label) { | |
75 | 15300 | mark(); |
76 | 15300 | labels.set(labels.size() - 1, label); |
77 | 15300 | } |
78 | ||
79 | /** | |
80 | * Returns an enumeration of formatted distances. | |
81 | * | |
82 | * @return an enumeration representing the results | |
83 | */ | |
84 | public Enumeration result() { | |
85 | 900 | mark(); |
86 | 900 | return new SimpleTimerEnumeration(); |
87 | } | |
88 | ||
89 | /** | |
90 | * An enumeration to walk through all entries.<p> | |
91 | * | |
92 | * This allows us to get a summary at the end. | |
93 | * | |
94 | * @author Linus Tolke | |
95 | */ | |
96 | 17100 | class SimpleTimerEnumeration implements Enumeration<String> { |
97 | /** | |
98 | * Keep track of where we are in the list. | |
99 | */ | |
100 | 900 | private int count = 1; |
101 | ||
102 | /* | |
103 | * @see java.util.Enumeration#hasMoreElements() | |
104 | */ | |
105 | public boolean hasMoreElements() { | |
106 | 17100 | return count <= points.size(); |
107 | } | |
108 | ||
109 | /* | |
110 | * @see java.util.Enumeration#nextElement() | |
111 | */ | |
112 | public String nextElement() { | |
113 | 16200 | StringBuffer res = new StringBuffer(); |
114 | 16200 | synchronized (points) { |
115 | 16200 | if (count < points.size()) { |
116 | 15300 | if (labels.get(count - 1) == null) { |
117 | 0 | res.append("phase ").append(count); |
118 | } else { | |
119 | 15300 | res.append(labels.get(count - 1)); |
120 | } | |
121 | 15300 | res.append(" "); |
122 | 15300 | res.append(" "); |
123 | 15300 | res.setLength(60); |
124 | 15300 | res.append(points.get(count) - points.get(count - 1)); |
125 | 900 | } else if (count == points.size()) { |
126 | 900 | res.append("Total "); |
127 | 900 | res.setLength(18); |
128 | 900 | res.append(points.get(points.size() - 1) - (points.get(0))); |
129 | } | |
130 | 16200 | } |
131 | 16200 | count++; |
132 | 16200 | return res.toString(); |
133 | } | |
134 | } | |
135 | ||
136 | /* | |
137 | * @see java.lang.Object#toString() | |
138 | */ | |
139 | public String toString() { | |
140 | 0 | StringBuffer sb = new StringBuffer(""); |
141 | ||
142 | 0 | for (Enumeration e = result(); e.hasMoreElements();) { |
143 | 0 | sb.append((String) e.nextElement()); |
144 | 0 | sb.append("\n"); |
145 | } | |
146 | 0 | return sb.toString(); |
147 | } | |
148 | } |