/*
* * This file is part of the Hesperides distribution.
* * (https://github.com/voyages-sncf-technologies/hesperides)
* * Copyright (c) 2016 VSCT.
* *
* * Hesperides is free software: you can redistribute it and/or modify
* * it under the terms of the GNU General Public License as
* * published by the Free Software Foundation, version 3.
* *
* * Hesperides is distributed in the hope that it will be useful, but
* * WITHOUT ANY WARRANTY; without even the implied warranty of
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* * General Public License for more details.
* *
* * You should have received a copy of the GNU General Public License
* * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.vsct.dt.hesperides.applications.virtual;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Created by emeric_martineau on 09/02/2017.
*/
public class VirtualFuture<T> implements Future<T> {
private interface VirtualResult<T> {
T get() throws Exception;
}
private VirtualResult<T> vr;
public VirtualFuture(final Callable<T> task) {
vr = () -> task.call();
}
public VirtualFuture(final Runnable task, final T result) {
vr = () -> result;
}
public VirtualFuture(final Runnable task) {
vr = () -> {
task.run();
return null;
};
}
@Override
public boolean cancel(final boolean mayInterruptIfRunning) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return false;
}
@Override
public T get() throws InterruptedException, ExecutionException {
try {
return vr.get();
} catch (InterruptedException | ExecutionException e) {
throw e;
} catch (Exception e) {
throw new ExecutionException(e);
}
}
@Override
public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
try {
return vr.get();
} catch (InterruptedException | ExecutionException e) {
throw e;
} catch (Exception e) {
throw new ExecutionException(e);
}
}
}