Timer.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package util;
  2. import java.util.Stack;
  3. import exceptions.TimerException;
  4. public class Timer {
  5. public StopWatch[] watches;
  6. static Stack<StopWatch> stack;
  7. static {
  8. stack = new Stack<StopWatch>();
  9. }
  10. public Timer() {
  11. watches = new StopWatch[M.size];
  12. for (int j = 0; j < M.size; j++)
  13. watches[j] = new StopWatch(M.names[j]);
  14. }
  15. public Timer(String task) {
  16. watches = new StopWatch[M.size];
  17. for (int j = 0; j < M.size; j++)
  18. watches[j] = new StopWatch(task + "_" + M.names[j]);
  19. }
  20. public Timer(StopWatch[] sws) {
  21. watches = sws;
  22. }
  23. public void start(int m) {
  24. if (!stack.empty()) {
  25. if (stack.peek() == watches[m])
  26. throw new TimerException("Stopwatch already added to stack");
  27. stack.peek().stop();
  28. }
  29. stack.push(watches[m]).start();
  30. }
  31. public void stop(int m) {
  32. if (stack.empty())
  33. throw new TimerException("No stopwatch running");
  34. if (stack.peek() != watches[m])
  35. throw new TimerException("Wrong Stopwatch to stop");
  36. stack.pop().stop();
  37. if (!stack.empty())
  38. stack.peek().start();
  39. }
  40. public void reset() {
  41. if (!stack.empty())
  42. throw new TimerException("Stack not empty");
  43. for (int i = 0; i < watches.length; i++)
  44. watches[i].reset();
  45. }
  46. public void print() {
  47. if (!stack.empty())
  48. throw new TimerException("Stack not empty");
  49. for (int i = 3; i < watches.length; i++)
  50. System.out.println(watches[i].toMS());
  51. for (int i = 0; i < 3; i++)
  52. System.out.println(watches[i].toMS());
  53. }
  54. public void noPrePrint() {
  55. if (!stack.empty())
  56. throw new TimerException("Stack not empty");
  57. for (int i = 3; i < watches.length; i++)
  58. System.out.println(watches[i].noPreToMS());
  59. for (int i = 0; i < 3; i++)
  60. System.out.println(watches[i].noPreToMS());
  61. }
  62. public Timer divideByAndReturn(int n) {
  63. if (!stack.empty())
  64. throw new TimerException("Stack not empty");
  65. StopWatch[] sws = new StopWatch[M.size];
  66. for (int i = 0; i < watches.length; i++)
  67. sws[i] = watches[i].divideByAndReturn(n);
  68. return new Timer(sws);
  69. }
  70. public Timer addAndReturn(Timer t) {
  71. // if (!stack.empty() || !t.stack.empty())
  72. if (!stack.empty())
  73. throw new TimerException("Stack not empty");
  74. StopWatch[] sws = new StopWatch[M.size];
  75. for (int i = 0; i < watches.length; i++)
  76. sws[i] = watches[i].addAndReturn(t.watches[i]);
  77. return new Timer(sws);
  78. }
  79. public void add(Timer t) {
  80. // if (!stack.empty() || !t.stack.empty())
  81. if (!stack.empty())
  82. throw new TimerException("Stack not empty");
  83. for (int i = 0; i < watches.length; i++)
  84. watches[i].add(t.watches[i]);
  85. }
  86. }