Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package my.pack;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.PriorityQueue;
- public class Controller {
- static interface Command {
- int getID();
- void execute();
- }
- Map<Integer, Long> runtimes = new HashMap<>();
- PriorityQueue<Command> queue;
- private class TimeMeasuringCommand implements Command {
- private Command c;
- public TimeMeasuringCommand(Command c) {
- this.c = c;
- }
- @Override
- public int getID() {
- return c.getID();
- }
- @Override
- public void execute() {
- long start = System.currentTimeMillis();
- c.execute();
- long end = System.currentTimeMillis();
- runtimes.put(c.getID(), end - start);
- }
- }
- Controller() {
- queue = new PriorityQueue<Command>(0,
- (x, y) -> Long.compare(runtimes.get(x.getID()),
- runtimes.get(y.getID())));
- }
- void insertCommand(Command c) {
- if (!runtimes.containsKey(c.getID())) {
- runtimes.put(c.getID(), 0L);
- queue.add(new TimeMeasuringCommand(c));
- }
- else {
- queue.add(c);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement