LapisSea

Untitled

Feb 27th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. package com.lapissea.amaze;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class Timer{
  7.    
  8.     public interface Updateable{
  9.        
  10.         void update();
  11.        
  12.         void setDirty(boolean dirty);
  13.        
  14.         default void markDirty(){
  15.             register();
  16.             setDirty(true);
  17.         }
  18.        
  19.         boolean isDirty();
  20.  
  21.         boolean isRegistered();
  22.         void register();
  23.         void unregister();
  24.        
  25.     }
  26.    
  27.     public static class UpdateableImpl implements Updateable{
  28.        
  29.         private final Runnable  hook;
  30.         private boolean         dirty       =true;
  31.         private boolean         registered  =false;
  32.        
  33.         public UpdateableImpl(Runnable hook){
  34.             this.hook=hook;
  35.         }
  36.        
  37.         @Override
  38.         public void update(){
  39.             hook.run();
  40.         }
  41.        
  42.         @Override
  43.         public void setDirty(boolean dirty){
  44.             this.dirty=dirty;
  45.         }
  46.        
  47.         @Override
  48.         public boolean isDirty(){
  49.             return dirty;
  50.         }
  51.        
  52.         @Override
  53.         public String toString(){
  54.             return hashCode()+"";
  55.         }
  56.        
  57.         @Override
  58.         public boolean isRegistered(){
  59.             return registered;
  60.         }
  61.  
  62.         @Override
  63.         public void register(){
  64.             if(registered)return;
  65.             registered=true;
  66.             add(this);
  67.         }
  68.  
  69.         @Override
  70.         public void unregister(){
  71.             if(!registered)return;
  72.             registered=false;
  73.             remove(this);
  74.         }
  75.     }
  76.    
  77.     private static final List<Updateable>   LISTENERS   =new ArrayList<>(),TO_ADD=new ArrayList<>(),TO_REMOVE=new ArrayList<>();
  78.     private static boolean                  RUNNING;
  79.    
  80.     public static void start(){
  81.         new Thread(Timer::run).start();
  82.     }
  83.    
  84.     private static void run(){
  85.        
  86.         int ups=60;
  87.        
  88.         double nextUpdate=0,fequ=1000_000_000D/ups;
  89.        
  90.         while(true){
  91.             long tim=System.nanoTime();
  92.            
  93.             if(tim>=nextUpdate){
  94.                 nextUpdate=tim+fequ;
  95.                 update();
  96.             }
  97.             if(tim<nextUpdate-1) UtilM.sleep(0, 500000);
  98.         }
  99.     }
  100.    
  101.     private static void update(){
  102.        
  103.         if(TO_ADD.size()>0){
  104.             LISTENERS.addAll(TO_ADD);
  105.             TO_ADD.clear();
  106.         }
  107.        
  108.         RUNNING=true;
  109.         LISTENERS.stream().filter(Updateable::isDirty).forEach(u->{
  110.             u.setDirty(false);
  111.             u.update();
  112.         });
  113.         RUNNING=false;
  114.        
  115.         if(TO_REMOVE.size()>0){
  116.             LISTENERS.removeAll(TO_REMOVE);
  117.             TO_REMOVE.clear();
  118.         }
  119.     }
  120.    
  121.     public static UpdateableImpl newImpl(Runnable hook){
  122.         return add(new UpdateableImpl(hook));
  123.     }
  124.    
  125.     private static <T extends Updateable> T add(T u){
  126.         if(!LISTENERS.contains(u)){
  127.             u.markDirty();
  128.             (RUNNING?TO_ADD:LISTENERS).add(u);
  129.         }
  130.         return u;
  131.     }
  132.    
  133.     private static <T extends Updateable> T remove(T u){
  134.         (RUNNING?TO_REMOVE:LISTENERS).remove(u);
  135.         return u;
  136.     }
  137. }
Add Comment
Please, Sign In to add comment