/** * Checks if the start runnable contains the provided callable. * * @param startRunnable runnable to start search at * @param compareTo callable to be comparing against * @return {@code true} if the compareTo runnable is contained within the runnable */ public static boolean isContained(Runnable startRunnable, Callable<?> compareTo) { /* we have some awkward if/else logic in case we have a runnable the is both a * CallableContainerInterface and a RunnableContainerInterface */ boolean isContainedAsCCI = false; if (startRunnable instanceof CallableContainer<?>) { CallableContainer<?> cci = (CallableContainer<?>)startRunnable; isContainedAsCCI = isContained(cci, compareTo); } if (isContainedAsCCI) { return true; } else if (startRunnable instanceof RunnableContainer) { RunnableContainer rci = (RunnableContainer)startRunnable; return isContained(rci, compareTo); } else { return false; } }
@Override public boolean remove(Callable<?> task) { Iterator<WeakReference<RecurringWrapper>> it = recurringTasks.iterator(); while (it.hasNext()) { RecurringWrapper rw = it.next().get(); if (rw == null) { it.remove(); } else if (ContainerHelper.isContained(rw, task)) { it.remove(); if (rw.invalidate()) { // try to remove proactively waitingTasks.remove(rw); scheduler.remove(rw.delayRunnable); scheduler.remove(rw); return true; } } } // synchronize on this so that we don't consume tasks while trying to remove synchronized (this) { return ContainerHelper.remove(waitingTasks, task) || scheduler.remove(task); } }
@Test public void getContainedRunnablesTest() { List<TestRunnableContainer> containers = new ArrayList<>(TEST_QTY); for (int i = 0; i < TEST_QTY; i++) { containers.add(new TestRunnableContainer(new TestRunnable())); } List<Runnable> resultList = ContainerHelper.getContainedRunnables(containers); assertEquals(containers.size(), resultList.size()); Iterator<TestRunnableContainer> it = containers.iterator(); while (it.hasNext()) { assertTrue(resultList.contains(it.next().r)); } }
@Test public void removeRunnableFromCollectionTest() { List<TestRunnableContainer> testRunnables = new ArrayList<>(TEST_QTY); for (int i = 0; i < TEST_QTY; i++) { testRunnables.add(new TestRunnableContainer(new TestRunnable())); } Runnable toRemove = testRunnables.get(TEST_QTY / 2).r; assertTrue(ContainerHelper.remove(testRunnables, toRemove)); }
/** * Removes any tasks waiting to be run. Will not interrupt any tasks currently running. But * will avoid additional tasks from being run (unless they are allowed to be added during or * after this call). * <p> * If tasks are added concurrently during this invocation they may or may not be removed. * * @return List of runnables which were waiting in the task queue to be executed (and were now removed) */ public List<Runnable> clearQueue() { List<TaskWrapper> wrapperList = new ArrayList<>(highPriorityQueueSet.queueSize() + lowPriorityQueueSet.queueSize() + starvablePriorityQueueSet.queueSize()); highPriorityQueueSet.drainQueueInto(wrapperList); lowPriorityQueueSet.drainQueueInto(wrapperList); starvablePriorityQueueSet.drainQueueInto(wrapperList); return ContainerHelper.getContainedRunnables(wrapperList); }
@Test public void removeCallableFromCollectionTest() { List<TestRunnableContainer> testRunnables = new ArrayList<>(TEST_QTY); Callable<?> toRemove = null; for (int i = 0; i < TEST_QTY; i++) { TestCallable tc = new TestCallable(); if (TEST_QTY / 2 == i) { toRemove = tc; } testRunnables.add(new TestRunnableContainer(new TestCallableContainer(tc))); } assertTrue(ContainerHelper.remove(testRunnables, toRemove)); }
/** * Checks if the start runnable contains the provided callable. * * @param startRunnable runnable to start search at * @param compareTo callable to be comparing against * @return {@code true} if the compareTo runnable is contained within the runnable */ public static boolean isContained(Runnable startRunnable, Callable<?> compareTo) { /* we have some awkward if/else logic in case we have a runnable the is both a * CallableContainerInterface and a RunnableContainerInterface */ boolean isContainedAsCCI = false; if (startRunnable instanceof CallableContainer<?>) { CallableContainer<?> cci = (CallableContainer<?>)startRunnable; isContainedAsCCI = isContained(cci, compareTo); } if (isContainedAsCCI) { return true; } else if (startRunnable instanceof RunnableContainer) { RunnableContainer rci = (RunnableContainer)startRunnable; return isContained(rci, compareTo); } else { return false; } }
@Override public boolean remove(Runnable task) { Iterator<WeakReference<RecurringWrapper>> it = recurringTasks.iterator(); while (it.hasNext()) { RecurringWrapper rw = it.next().get(); if (rw == null) { it.remove(); } else if (ContainerHelper.isContained(rw, task)) { it.remove(); if (rw.invalidate()) { // try to remove proactively waitingTasks.remove(rw); scheduler.remove(rw.delayRunnable); scheduler.remove(rw); return true; } } } // synchronize on this so that we don't consume tasks while trying to remove synchronized (this) { return ContainerHelper.remove(waitingTasks, task) || scheduler.remove(task); } }
/** * Removes any tasks waiting to be run. Will not interrupt any tasks currently running. But * will avoid additional tasks from being run (unless they are allowed to be added during or * after this call). * <p> * If tasks are added concurrently during this invocation they may or may not be removed. * * @return List of runnables which were waiting in the task queue to be executed (and were now removed) */ public List<Runnable> clearQueue() { List<TaskWrapper> wrapperList = new ArrayList<>(highPriorityQueueSet.queueSize() + lowPriorityQueueSet.queueSize() + starvablePriorityQueueSet.queueSize()); highPriorityQueueSet.drainQueueInto(wrapperList); lowPriorityQueueSet.drainQueueInto(wrapperList); starvablePriorityQueueSet.drainQueueInto(wrapperList); return ContainerHelper.getContainedRunnables(wrapperList); }
/** * Checks if the start runnable equals the compareTo runnable, or if the compareTo runnable is * contained within a wrapper of the startRunnable. * * @param startRunnable runnable to start search at * @param compareTo runnable to be comparing against * @return {@code true} if they are equivalent, or the compareTo runnable is contained within the start */ public static boolean isContained(Runnable startRunnable, Runnable compareTo) { if (startRunnable.equals(compareTo)) { return true; } boolean containedAsRCCI = false; if (startRunnable instanceof RunnableContainer) { RunnableContainer rci = (RunnableContainer)startRunnable; containedAsRCCI = isContained(rci, compareTo); } if (containedAsRCCI) { return true; } else if (startRunnable instanceof CallableContainer<?>) { CallableContainer<?> cci = (CallableContainer<?>)startRunnable; return isContained(cci, compareTo); } else { return false; } }
@Override public boolean remove(Callable<?> task) { Iterator<WeakReference<RecurringWrapper>> it = recurringTasks.iterator(); while (it.hasNext()) { RecurringWrapper rw = it.next().get(); if (rw == null) { it.remove(); } else if (ContainerHelper.isContained(rw, task)) { it.remove(); if (rw.invalidate()) { // try to remove proactively waitingTasks.remove(rw); scheduler.remove(rw.delayRunnable); scheduler.remove(rw); return true; } } } // synchronize on this so that we don't consume tasks while trying to remove synchronized (this) { return ContainerHelper.remove(waitingTasks, task) || scheduler.remove(task); } }
/** * Checks if the start runnable equals the compareTo runnable, or if the compareTo runnable is * contained within a wrapper of the startRunnable. * * @param startRunnable runnable to start search at * @param compareTo runnable to be comparing against * @return {@code true} if they are equivalent, or the compareTo runnable is contained within the start */ public static boolean isContained(Runnable startRunnable, Runnable compareTo) { if (startRunnable.equals(compareTo)) { return true; } boolean containedAsRCCI = false; if (startRunnable instanceof RunnableContainer) { RunnableContainer rci = (RunnableContainer)startRunnable; containedAsRCCI = isContained(rci, compareTo); } if (containedAsRCCI) { return true; } else if (startRunnable instanceof CallableContainer<?>) { CallableContainer<?> cci = (CallableContainer<?>)startRunnable; return isContained(cci, compareTo); } else { return false; } }
@Override public boolean remove(Runnable task) { Iterator<WeakReference<RecurringWrapper>> it = recurringTasks.iterator(); while (it.hasNext()) { RecurringWrapper rw = it.next().get(); if (rw == null) { it.remove(); } else if (ContainerHelper.isContained(rw, task)) { it.remove(); if (rw.invalidate()) { // try to remove proactively waitingTasks.remove(rw); scheduler.remove(rw.delayRunnable); scheduler.remove(rw); return true; } } } // synchronize on this so that we don't consume tasks while trying to remove synchronized (this) { return ContainerHelper.remove(waitingTasks, task) || scheduler.remove(task); } }
private static boolean isContained(RunnableContainer rci, Callable<?> compareTo) { while (true) { Runnable containedTask = rci.getContainedRunnable(); if (containedTask != null) { if (containedTask instanceof CallableContainer<?> && isContained((CallableContainer<?>)containedTask, compareTo)) { return true; } else if (containedTask instanceof RunnableContainer) { // loop again rci = (RunnableContainer)containedTask; } else { return false; } } else { return false; } } }
/** * Attempts to remove the provided callable from the source collection. This uses the * {@link Collection#remove(Object)} function to remove the container if it is found. * * @since 2.0.0 * @param source source collection to search over * @param compareTo Callable to compare against in search * @return {@code true} if collection was modified */ public static boolean remove(Collection<? extends RunnableContainer> source, Callable<?> compareTo) { Iterator<? extends RunnableContainer> it = source.iterator(); while (it.hasNext()) { RunnableContainer rc = it.next(); // we use source.remove() instead of it.remove() for usage with concurrent structures if (ContainerHelper.isContained(rc.getContainedRunnable(), compareTo) && source.remove(rc)) { return true; } } return false; }
private static boolean isContained(RunnableContainer rci, Callable<?> compareTo) { while (true) { Runnable containedTask = rci.getContainedRunnable(); if (containedTask != null) { if (containedTask instanceof CallableContainer<?> && isContained((CallableContainer<?>)containedTask, compareTo)) { return true; } else if (containedTask instanceof RunnableContainer) { // loop again rci = (RunnableContainer)containedTask; } else { return false; } } else { return false; } } }
/** * Attempts to remove the provided runnable from the source collection. This uses the * {@link Collection#remove(Object)} function to remove the container if it is found. * * @since 2.0.0 * @param source source collection to search over * @param compareTo Runnable to compare against in search * @return {@code true} if collection was modified */ public static boolean remove(Collection<? extends RunnableContainer> source, Runnable compareTo) { Iterator<? extends RunnableContainer> it = source.iterator(); while (it.hasNext()) { RunnableContainer rc = it.next(); // we use source.remove() instead of it.remove() for usage with concurrent structures if (ContainerHelper.isContained(rc.getContainedRunnable(), compareTo) && source.remove(rc)) { return true; } } return false; }
/** * Attempts to remove the provided callable from the source collection. This uses the * {@link Collection#remove(Object)} function to remove the container if it is found. * * @since 2.0.0 * @param source source collection to search over * @param compareTo Callable to compare against in search * @return {@code true} if collection was modified */ public static boolean remove(Collection<? extends RunnableContainer> source, Callable<?> compareTo) { Iterator<? extends RunnableContainer> it = source.iterator(); while (it.hasNext()) { RunnableContainer rc = it.next(); // we use source.remove() instead of it.remove() for usage with concurrent structures if (ContainerHelper.isContained(rc.getContainedRunnable(), compareTo) && source.remove(rc)) { return true; } } return false; }
/** * Attempts to remove the provided runnable from the source collection. This uses the * {@link Collection#remove(Object)} function to remove the container if it is found. * * @since 2.0.0 * @param source source collection to search over * @param compareTo Runnable to compare against in search * @return {@code true} if collection was modified */ public static boolean remove(Collection<? extends RunnableContainer> source, Runnable compareTo) { Iterator<? extends RunnableContainer> it = source.iterator(); while (it.hasNext()) { RunnableContainer rc = it.next(); // we use source.remove() instead of it.remove() for usage with concurrent structures if (ContainerHelper.isContained(rc.getContainedRunnable(), compareTo) && source.remove(rc)) { return true; } } return false; }
/** * Checks if the compareTo runnable is contained by the provided * {@link CallableContainerInterface}. If it's not we check to see if we can continue our * search by looking for another {@link RunnableContainer}, or a {@link CallableContainer}. * * @param cci Container to check contents of * @param compareTo Runnable to compare against * @return {@code true} if the runnable is contained at some point within the container */ private static boolean isContained(CallableContainer<?> cci, Runnable compareTo) { while (true) { Callable<?> containedTask = cci.getContainedCallable(); if (containedTask != null) { if (containedTask instanceof RunnableContainer && isContained((RunnableContainer)containedTask, compareTo)) { return true; } else if (containedTask instanceof CallableContainer<?>) { // loop again cci = (CallableContainer<?>)containedTask; } else { return false; } } else { return false; } } }