1 11 package org.eclipse.ui.internal.cheatsheets; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.Platform; 17 18 public class CheatSheetStopWatch { 19 private static CheatSheetStopWatch stopWatch = null; 20 21 private Map table; 22 23 private CheatSheetStopWatch() { 24 25 } 26 27 public static CheatSheetStopWatch getInstance() { 28 if(stopWatch == null) { 29 stopWatch = new CheatSheetStopWatch(); 30 } 31 32 return stopWatch; 33 } 34 35 public void start(String key) { 36 37 Entry entry = getEntry(key); 38 if (entry == null) { 39 entry = new Entry(); 40 putEntry(key, entry); 41 } else { 42 resetEntry(entry); 43 } 44 45 entry.start = System.currentTimeMillis(); 46 } 47 48 public void stop(String key) { 49 Entry entry = getEntry(key); 50 entry.stop = System.currentTimeMillis(); 51 } 52 53 public long totalElapsedTime(String key) { 54 Entry entry = getEntry(key); 55 return entry.stop - entry.start; 56 } 57 58 public void lapTime(String key) { 59 Entry entry = getEntry(key); 60 if(entry.currentLap == -1) { 61 entry.previousLap = entry.start; 62 } else { 63 entry.previousLap = entry.currentLap; 64 } 65 entry.currentLap = System.currentTimeMillis(); 66 } 67 68 public long elapsedTime(String key) { 69 Entry entry = getEntry(key); 70 return entry.currentLap - entry.previousLap; 71 } 72 73 76 private static class Entry { 77 protected long start = -1; 78 protected long stop = -1; 79 protected long currentLap = -1; 80 protected long previousLap = -1; 81 } 82 83 private Entry getEntry(String key) { 84 return (Entry) getTable().get(key); 85 } 86 87 private void putEntry(String key, Entry entry) { 88 getTable().put(key, entry); 89 } 90 91 private void resetEntry(Entry entry) { 92 entry.start = -1; 93 entry.stop = -1; 94 entry.currentLap = -1; 95 entry.previousLap = -1; 96 } 97 98 private Map getTable() { 99 if (table == null) { 100 table = new HashMap (10); 101 } 102 return table; 103 } 104 105 106 public static boolean isTracing() { 107 if (CheatSheetPlugin.getPlugin().isDebugging()) { 108 String traceTimes = Platform.getDebugOption("org.eclipse.ui.cheatsheets/trace/creation/times"); if (traceTimes != null && traceTimes.equalsIgnoreCase("true")) { return true; 111 } 112 } 113 return false; 114 } 115 116 public static void startStopWatch(String key) { 117 if(isTracing()) { 118 getInstance().start(key); 119 } 120 } 121 122 public static void printTotalTime(String key, String message) { 123 if(isTracing()) { 124 getInstance().stop(key); 125 System.out.print(message); 126 System.out.println(getInstance().totalElapsedTime(key)); 127 } 128 } 129 130 public static void printLapTime(String key, String message) { 131 if(isTracing()) { 132 getInstance().lapTime(key); 133 System.out.print(message); 134 System.out.println(getInstance().elapsedTime(key)); 135 } 136 } 137 } 138 | Popular Tags |