|
@@ -2,7 +2,7 @@ package util;
|
|
|
|
|
|
import java.util.Stack;
|
|
import java.util.Stack;
|
|
|
|
|
|
-import exceptions.TimingException;
|
|
+import exceptions.TimerException;
|
|
|
|
|
|
public class Timer {
|
|
public class Timer {
|
|
StopWatch[][] watches;
|
|
StopWatch[][] watches;
|
|
@@ -16,10 +16,15 @@ public class Timer {
|
|
stack = new Stack<StopWatch>();
|
|
stack = new Stack<StopWatch>();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public Timer(StopWatch[][] sws) {
|
|
|
|
+ watches = sws;
|
|
|
|
+ stack = new Stack<StopWatch>();
|
|
|
|
+ }
|
|
|
|
+
|
|
public void start(int p, int m) {
|
|
public void start(int p, int m) {
|
|
if (!stack.empty()) {
|
|
if (!stack.empty()) {
|
|
if (stack.peek() == watches[p][m])
|
|
if (stack.peek() == watches[p][m])
|
|
- throw new TimingException("Stopwatch already added to stack");
|
|
+ throw new TimerException("Stopwatch already added to stack");
|
|
stack.peek().stop();
|
|
stack.peek().stop();
|
|
}
|
|
}
|
|
stack.push(watches[p][m]).start();
|
|
stack.push(watches[p][m]).start();
|
|
@@ -27,17 +32,35 @@ public class Timer {
|
|
|
|
|
|
public void stop(int p, int m) {
|
|
public void stop(int p, int m) {
|
|
if (stack.empty())
|
|
if (stack.empty())
|
|
- throw new TimingException("No stopwatch found");
|
|
+ throw new TimerException("No stopwatch found");
|
|
stack.pop().stop();
|
|
stack.pop().stop();
|
|
if (!stack.empty())
|
|
if (!stack.empty())
|
|
stack.peek().start();
|
|
stack.peek().start();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void reset() {
|
|
|
|
+ if (!stack.empty())
|
|
|
|
+ throw new TimerException("Stack not empty");
|
|
|
|
+ for (int i = 0; i < watches.length; i++)
|
|
|
|
+ for (int j = 0; j < watches[i].length; j++)
|
|
|
|
+ watches[i][j].reset();
|
|
|
|
+ }
|
|
|
|
+
|
|
public void print() {
|
|
public void print() {
|
|
if (!stack.empty())
|
|
if (!stack.empty())
|
|
- throw new TimingException("Stack not empty");
|
|
+ throw new TimerException("Stack not empty");
|
|
for (int i = 0; i < watches.length; i++)
|
|
for (int i = 0; i < watches.length; i++)
|
|
for (int j = 0; j < watches[i].length; j++)
|
|
for (int j = 0; j < watches[i].length; j++)
|
|
System.out.println(watches[i][j].toMS());
|
|
System.out.println(watches[i][j].toMS());
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public Timer divideBy(int n) {
|
|
|
|
+ if (!stack.empty())
|
|
|
|
+ throw new TimerException("Stack not empty");
|
|
|
|
+ StopWatch[][] sws = new StopWatch[P.size][M.size];
|
|
|
|
+ for (int i = 0; i < watches.length; i++)
|
|
|
|
+ for (int j = 0; j < watches[i].length; j++)
|
|
|
|
+ sws[i][j] = watches[i][j].divideBy(n);
|
|
|
|
+ return new Timer(sws);
|
|
|
|
+ }
|
|
}
|
|
}
|