Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
Refactor future utils, extracted AbstractTaskFuture2.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Dec 15, 2016
1 parent a350d37 commit 624ae0f
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ default <R, EXC extends Exception> ArrayList2<R> mapx(FunctionX<? super E, ? ext
return CollectionUtil.mapx(this, evalFunction);
}

default <EXC extends Exception> ArrayList2<E> filterx(
ArrayList2<E> into, FunctionX<E, Boolean, EXC> predicate
) throws EXC {
return CollectionUtil.filter2x(into, this, predicate);
}

/**
* @return the index in the iteration order of the first element that matches given predicate, or -1 otherwise.
* Note that if the iteration order of this collection is not stable, then the index isn't either.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

import melnorme.utilbox.core.fntypes.CallableX;

public abstract class AbstractRunnableFuture2<RET> extends AbstractFuture2<RET>
implements IRunnableFuture2<RET>
public abstract class AbstractTaskFuture2<RET> extends AbstractFuture2<RET>
implements ICancellable
{

public AbstractRunnableFuture2() {
public AbstractTaskFuture2() {
super();
}

Expand All @@ -28,7 +28,7 @@ public AbstractRunnableFuture2() {
private final CancellableTask cancellableTask = new CancellableTask() {
@Override
protected void doRun() {
AbstractRunnableFuture2.this.internalTaskRun();
AbstractTaskFuture2.this.internalTaskRun();
}
};

Expand All @@ -37,11 +37,6 @@ public boolean canExecute() {
return cancellableTask.canExecute();
}

@Override
public void run() {
runFuture();
}

protected void runFuture() {
cancellableTask.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static <RET> IRunnableFuture2<RET> toFuture(Callable2<RET, RuntimeExcepti
}

public static <RET, EXC extends Throwable> IRunnableFuture2<Result<RET, EXC>> toResultFuture(
Callable2<RET, EXC> callable) {
Callable2<RET, EXC> callable
) {
return toFuture(callable::callToResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,28 @@
*******************************************************************************/
package melnorme.utilbox.concurrency;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import melnorme.utilbox.concurrency.ICancelMonitor.CancelMonitor;
import melnorme.utilbox.concurrency.ICancelMonitor.CompositeCancelMonitor;

/**
* A {@link IRunnableFuture2} with a cancellation monitor.
*/
public abstract class MonitorRunnableFuture<RET>
extends AbstractRunnableFuture2<RET>
extends MonitorTaskFuture<RET>
implements IRunnableFuture2<RET>
{

/** The cancel monitor for this Future */
private final CancelMonitor cancelMonitor;

public MonitorRunnableFuture() {
this(new CancelMonitor());
}

public MonitorRunnableFuture(ICancelMonitor cancelMonitor) {
this(new CompositeCancelMonitor(cancelMonitor));
super();
}

public MonitorRunnableFuture(CancelMonitor cancelMonitor) {
this.cancelMonitor = assertNotNull(cancelMonitor);
}

public CancelMonitor getCancelMonitor() {
updateCancellationFromMonitor();
return cancelMonitor;
super(cancelMonitor);
}

protected void updateCancellationFromMonitor() {
boolean monitorCancelled = cancelMonitor.isCancelled();
if(monitorCancelled && super.isCancelled() == false) {
tryCancel();
}
}

@Override
public boolean isTerminated() {
updateCancellationFromMonitor();
return super.isTerminated();
}

@Override
public boolean isCancelled() {
updateCancellationFromMonitor();
return super.isCancelled();
public MonitorRunnableFuture(ICancelMonitor cancelMonitor) {
super(cancelMonitor);
}

@Override
protected void handleCancellation() {
cancelMonitor.cancel();
super.handleCancellation();
public void run() {
runFuture();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2016 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.utilbox.concurrency;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import melnorme.utilbox.concurrency.ICancelMonitor.CancelMonitor;
import melnorme.utilbox.concurrency.ICancelMonitor.CompositeCancelMonitor;

/**
* A {@link AbstractTaskFuture2} with a cancellation monitor.
*/
public abstract class MonitorTaskFuture<RET>
extends AbstractTaskFuture2<RET>
{

/** The cancel monitor for this Future */
private final CancelMonitor cancelMonitor;

public MonitorTaskFuture() {
this(new CancelMonitor());
}

public MonitorTaskFuture(ICancelMonitor cancelMonitor) {
this(new CompositeCancelMonitor(cancelMonitor));
}

public MonitorTaskFuture(CancelMonitor cancelMonitor) {
this.cancelMonitor = assertNotNull(cancelMonitor);
}

public CancelMonitor getCancelMonitor() {
updateCancellationFromMonitor();
return cancelMonitor;
}

protected void updateCancellationFromMonitor() {
boolean monitorCancelled = cancelMonitor.isCancelled();
if(monitorCancelled && super.isCancelled() == false) {
tryCancel();
}
}

@Override
public boolean isTerminated() {
updateCancellationFromMonitor();
return super.isTerminated();
}

@Override
public boolean isCancelled() {
updateCancellationFromMonitor();
return super.isCancelled();
}

@Override
protected void handleCancellation() {
cancelMonitor.cancel();
super.handleCancellation();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@

/**
* A simple future, completable by means of executing the {@link #run()} method.
* Cannot be cancelled.
*
*/
public class RunnableFuture2<RET> extends AbstractRunnableFuture2<RET> {
public class RunnableFuture2<RET> extends AbstractTaskFuture2<RET>
implements IRunnableFuture2<RET>
{

protected final Callable2<RET, RuntimeException> callable;

public RunnableFuture2(Callable2<RET, RuntimeException> callable) {
this.callable = assertNotNull(callable);
}

@Override
public void run() {
runFuture();
}

@Override
protected RET internalInvoke() {
return callable.invoke();
Expand Down
24 changes: 23 additions & 1 deletion melnorme_util/src/melnorme/utilbox/misc/CollectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public static <ELEM, COLL extends Collection<? super ELEM>, SourceELEM> COLL add
}

/** Remove from given list all elements that match given predicate. */
public static <E, L extends List<E>> L filter(L list, Predicate<E> predicate) {
public static <E, L extends Iterable<E>> L filter(L list, Predicate<E> predicate) {
for (Iterator<? extends E> iter = list.iterator(); iter.hasNext(); ) {
E obj = iter.next();
if(predicate.test(obj)) {
Expand All @@ -239,6 +239,28 @@ public static <E, L extends List<E>> L filter(L list, Predicate<E> predicate) {
return list;
}

public static <ELEM, INTO extends Collection<ELEM>> INTO filter2(
INTO into, Iterable<? extends ELEM> from, Predicate<ELEM> predicate
) {
for (ELEM elem : from) {
if(predicate.test(elem)) {
into.add(elem);
}
}
return into;
}

public static <ELEM, INTO extends Collection<ELEM>, EXC extends Exception> INTO filter2x(
INTO into, Iterable<? extends ELEM> from, FunctionX<ELEM, Boolean, EXC> predicate
) throws EXC {
for (ELEM elem : from) {
if(predicate.apply(elem) == Boolean.TRUE) {
into.add(elem);
}
}
return into;
}

/** Removes from given list the first element that matches given predicate.
* @return true if an element was removed, false otherwise. */
public static <E> boolean removeElement(List<? extends E> list, Predicate<E> predicate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import melnorme.utilbox.concurrency.AbstractRunnableFuture2;
import melnorme.utilbox.concurrency.ICancelMonitor;
import melnorme.utilbox.concurrency.ICancelMonitor.NullCancelMonitor;
import melnorme.utilbox.concurrency.IRunnableFuture2;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.fntypes.Result;
import melnorme.utilbox.misc.StreamUtil;
Expand All @@ -34,8 +34,8 @@
* Subclasses must specify Runnable's for the worker threads reading the process stdout and stderr streams.
*/
public abstract class ExternalProcessHandler<
STDOUT_TASK extends AbstractRunnableFuture2<? extends Result<?, IOException>>,
STDERR_TASK extends AbstractRunnableFuture2<? extends Result<?, IOException>>
STDOUT_TASK extends IRunnableFuture2<? extends Result<?, IOException>>,
STDERR_TASK extends IRunnableFuture2<? extends Result<?, IOException>>
> implements IExternalProcessHandler {

protected final Process process;
Expand Down

0 comments on commit 624ae0f

Please sign in to comment.