Advertisement
bulbul_akabulbul

the-gay

Jan 27th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package my.pack;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.PriorityQueue;
  6.  
  7. public class Controller {
  8.  
  9.     static interface Command {
  10.         int getID();
  11.         void execute();
  12.     }
  13.    
  14.     Map<Integer, Long> runtimes = new HashMap<>();
  15.     PriorityQueue<Command> queue;
  16.    
  17.     private class TimeMeasuringCommand implements Command {
  18.  
  19.         private Command c;
  20.  
  21.         public TimeMeasuringCommand(Command c) {
  22.             this.c = c;
  23.         }
  24.        
  25.         @Override
  26.         public int getID() {
  27.             return c.getID();
  28.         }
  29.  
  30.         @Override
  31.         public void execute() {
  32.             long start = System.currentTimeMillis();
  33.             c.execute();
  34.             long end = System.currentTimeMillis();
  35.             runtimes.put(c.getID(), end - start);
  36.         }
  37.        
  38.     }
  39.    
  40.     Controller() {
  41.         queue = new PriorityQueue<Command>(0,
  42.                 (x, y) -> Long.compare(runtimes.get(x.getID()),
  43.                         runtimes.get(y.getID())));
  44.     }
  45.    
  46.     void insertCommand(Command c) {
  47.         if (!runtimes.containsKey(c.getID())) {
  48.             runtimes.put(c.getID(), 0L);
  49.             queue.add(new TimeMeasuringCommand(c));
  50.         }
  51.         else {
  52.             queue.add(c);
  53.         }
  54.        
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement