com.sinosoft.one.mvc.web.portal.impl.WindowFuture.java Source code

Java tutorial

Introduction

Here is the source code for com.sinosoft.one.mvc.web.portal.impl.WindowFuture.java

Source

/*
 * Copyright 2007-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinosoft.one.mvc.web.portal.impl;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.sinosoft.one.mvc.web.portal.WindowListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 *
 * 
 */
class WindowFuture<T> implements Future<T> {

    private static Log logger = LogFactory.getLog(WindowFuture.class);

    private final Future<T> future;

    private final WindowImpl window;

    private boolean cancelRequested = false;

    public WindowFuture(Future<T> future, WindowImpl window) {
        this.future = future;
        this.window = window;
    }

    public boolean isCancelRequested() {
        return cancelRequested;
    }

    public boolean cancel(final boolean mayInterruptIfRunning) {
        this.cancelRequested = true;
        // when mayInterruptIfRunning is false
        if (!mayInterruptIfRunning) {
            return cancel0(mayInterruptIfRunning);
        } else {
            // when mayInterruptIfRunning is true
            if (this.window.mayInterruptIfRunning()) {
                return cancel0(mayInterruptIfRunning);
            } else {
                return cancel0(false);
            }
        }
    }

    private boolean cancel0(boolean mayInterruptIfRunning) {
        if (logger.isDebugEnabled()) {
            logger.debug("canceling inner future: " + window.getName() + "  mayInterruptIfRunning="
                    + mayInterruptIfRunning);
        }
        if (future.cancel(mayInterruptIfRunning)) {
            ((WindowListener) window.getContainer()).onWindowCanceled(window);
            return true;
        } else {
            return false;
        }
    }

    public T get() throws InterruptedException, ExecutionException {
        return future.get();
    }

    public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return future.get(timeout, unit);
    }

    public boolean isCancelled() {
        return future.isCancelled();
    }

    public boolean isDone() {
        return future.isDone();
    }

    public Future<T> getInnerFuture() {
        return future;
    }

}