From: Tuukka Lehtonen Date: Thu, 27 Apr 2017 12:56:07 +0000 (+0300) Subject: Added maxQueueLength parameter constructor for DisposingPolicy. X-Git-Tag: v1.28.0~18 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=ec37e4545752e6aaa194235cf63acd9c1845a1d0;p=simantics%2Fplatform.git Added maxQueueLength parameter constructor for DisposingPolicy. Previously DisposingPolicy was hardcoded to 8 item queues. refs #7172 Change-Id: I2b7df0b71483396490ab559271f7531ae80d0822 (cherry picked from commit 8c344e6911c8156dc3ee25e5048f8202f80e52c1) --- diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DisposingPolicy.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DisposingPolicy.java index a3cd08b06..9e9b7418b 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DisposingPolicy.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DisposingPolicy.java @@ -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 disposerQueue = new ArrayDeque(MAX_QUEUE_LENGTH); private TObjectLongHashMap disposeTime = new TObjectLongHashMap(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(); } } - + }