]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added maxQueueLength parameter constructor for DisposingPolicy. 65/465/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 27 Apr 2017 12:56:07 +0000 (15:56 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 27 Apr 2017 12:56:07 +0000 (15:56 +0300)
Previously DisposingPolicy was hardcoded to 8 item queues.

refs #7172

Change-Id: I2b7df0b71483396490ab559271f7531ae80d0822

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DisposingPolicy.java

index a3cd08b068745f43bc0599fb04968b738b4ef9dc..9e9b7418bce5cbf069e6d512fa2c9c0a1402e481 100644 (file)
@@ -8,34 +8,40 @@ import org.eclipse.swt.widgets.Display;
 
 
 public class DisposingPolicy {
-    
+
     public static final boolean DEBUG = false;
-    
+
     public static final int MAX_QUEUE_LENGTH = 8;
     public static final long DISPOSE_TIME = 30000L; // ms
     public static final long MIN_DELAY = 200L; // ms
-    
+
+    private final int maxQueueLength;
     private ArrayDeque<Runnable> disposerQueue = new ArrayDeque<Runnable>(MAX_QUEUE_LENGTH);
     private TObjectLongHashMap<Runnable> disposeTime =
             new TObjectLongHashMap<Runnable>(MAX_QUEUE_LENGTH);
     private Runnable currentlyScheduled = null;
-    
-    private Runnable disposeOne = new Runnable() {
-        @Override
-        public void run() {
-            if(!disposerQueue.isEmpty()) {
-                Runnable runnable = disposerQueue.removeFirst();
-                disposeTime.remove(runnable);
-                currentlyScheduled = null;
-                runnable.run();
-                if(DEBUG)
-                    System.out.println("Executed disposer " + runnable);
-                if(!disposerQueue.isEmpty())
-                    scheduleDispose();
-            }
+
+    private Runnable disposeOne = () -> {
+        if(!disposerQueue.isEmpty()) {
+            Runnable runnable = disposerQueue.removeFirst();
+            disposeTime.remove(runnable);
+            currentlyScheduled = null;
+            runnable.run();
+            if(DEBUG)
+                System.out.println("Executed disposer " + runnable);
+            if(!disposerQueue.isEmpty())
+                scheduleDispose();
         }
     };
-    
+
+    public DisposingPolicy() {
+       this(MAX_QUEUE_LENGTH);
+    }
+
+    public DisposingPolicy(int maxQueueLength) {
+        this.maxQueueLength = maxQueueLength;
+    }
+
     private void scheduleDispose() {
         currentlyScheduled = disposerQueue.peekFirst();
         long delay = Math.max(
@@ -45,10 +51,10 @@ public class DisposingPolicy {
         if(DEBUG)
             System.out.println("Scheduled disposer " + currentlyScheduled + " in " + delay + " ms");
     }
-    
+
     /**
      * Runs the disposer either after DISPOSE_TIME or when there are
-     * more than MAX_QUEUE_LENGTH disposers active and this is first
+     * more than {@link #maxQueueLength} disposers active and this is first
      * of them (by activation order). This method must be called from
      * UI thread.
      */
@@ -57,14 +63,14 @@ public class DisposingPolicy {
             System.out.println("Added disposer " + disposer);
         if(disposeTime.contains(disposer))
             return;
-        if(disposerQueue.size() >= MAX_QUEUE_LENGTH)
+        if(disposerQueue.size() >= maxQueueLength)
             disposeOne.run();
         disposerQueue.addLast(disposer);
         disposeTime.put(disposer, System.currentTimeMillis()+DISPOSE_TIME);
         if(disposerQueue.size() == 1)
             scheduleDispose();
     }
-    
+
     /**
      * Cancels a disposer added before. This method must be called from
      * UI thread.
@@ -80,5 +86,5 @@ public class DisposingPolicy {
                 scheduleDispose();
         }
     }
-    
+
 }