
org.glassfish.grizzly.utils.CompletionHandlerAdapter Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.grizzly.utils;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.impl.FutureImpl;
/**
*
* @author Alexey Stashok
*/
public class CompletionHandlerAdapter implements CompletionHandler {
private final static GenericAdapter DIRECT_ADAPTER = new GenericAdapter() {
@Override
public Object adapt(final Object result) {
return result;
}
};
private final GenericAdapter adapter;
private final FutureImpl future;
private final CompletionHandler completionHandler;
public CompletionHandlerAdapter(FutureImpl future) {
this(future, null);
}
public CompletionHandlerAdapter(FutureImpl future, CompletionHandler completionHandler) {
this(future, completionHandler, null);
}
public CompletionHandlerAdapter(FutureImpl future, CompletionHandler completionHandler, GenericAdapter adapter) {
this.future = future;
this.completionHandler = completionHandler;
if (adapter != null) {
this.adapter = adapter;
} else {
this.adapter = getDirectAdapter();
}
}
@Override
public void cancelled() {
if (completionHandler != null) {
completionHandler.cancelled();
}
if (future != null) {
future.cancel(false);
}
}
@Override
public void failed(Throwable throwable) {
if (completionHandler != null) {
completionHandler.failed(throwable);
}
if (future != null) {
future.failure(throwable);
}
}
@Override
public void completed(B result) {
final A adaptedResult = adapt(result);
if (completionHandler != null) {
completionHandler.completed(adaptedResult);
}
if (future != null) {
future.result(adaptedResult);
}
}
@Override
public void updated(B result) {
final A adaptedResult = adapt(result);
if (completionHandler != null) {
completionHandler.updated(adaptedResult);
}
}
protected A adapt(B result) {
return adapter.adapt(result);
}
@SuppressWarnings("unchecked")
private static GenericAdapter getDirectAdapter() {
return DIRECT_ADAPTER;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy