/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* A Future for a pending I/O operation. A PendingFuture allows for the
* attachment of an additional arbitrary context object and a timer task.
*/
new CancellationException();
private final A attachment;
// true if result (or exception) is available
private volatile boolean haveResult;
private volatile V result;
// latch for waiting (created lazily if needed)
// optional timer task that is cancelled when result becomes available
// optional context object
CompletionHandler<V,? super A> handler,
A attachment,
{
this.attachment = attachment;
}
CompletionHandler<V,? super A> handler,
A attachment)
{
this.attachment = attachment;
}
}
}
return channel;
}
return handler;
}
A attachment() {
return attachment;
}
}
return context;
}
synchronized (this) {
if (haveResult) {
} else {
this.timeoutTask = task;
}
}
}
// creates latch if required; return true if caller needs to wait
private boolean prepareForWait() {
synchronized (this) {
if (haveResult) {
return false;
} else {
return true;
}
}
}
/**
* Sets the result, or a no-op if the result or exception is already set.
*/
synchronized (this) {
if (haveResult)
return;
haveResult = true;
if (timeoutTask != null)
timeoutTask.cancel(false);
}
}
/**
* Sets the result, or a no-op if the result or exception is already set.
*/
if (!(x instanceof IOException) && !(x instanceof SecurityException))
x = new IOException(x);
synchronized (this) {
if (haveResult)
return;
exc = x;
haveResult = true;
if (timeoutTask != null)
timeoutTask.cancel(false);
}
}
/**
* Sets the result
*/
if (x == null) {
} else {
setFailure(x);
}
}
if (!haveResult) {
boolean needToWait = prepareForWait();
if (needToWait)
}
throw new CancellationException();
throw new ExecutionException(exc);
}
return result;
}
{
if (!haveResult) {
boolean needToWait = prepareForWait();
if (needToWait)
}
throw new CancellationException();
throw new ExecutionException(exc);
}
return result;
}
}
V value() {
return result;
}
public boolean isCancelled() {
}
public boolean isDone() {
return haveResult;
}
synchronized (this) {
if (haveResult)
return false; // already completed
// notify channel
if (channel() instanceof Cancellable)
// set result and cancel timer
haveResult = true;
if (timeoutTask != null)
timeoutTask.cancel(false);
}
// close channel if forceful cancel
if (mayInterruptIfRunning) {
try {
} catch (IOException ignore) { }
}
// release waiters
return true;
}
}