All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.netty.util.concurrent.AbstractEventExecutor Maven / Gradle / Ivy

There is a newer version: 5.0.0.Alpha2
Show newest version
/*
 * Copyright 2013 The Netty Project
 *
 * The Netty Project licenses this file to you 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 io.netty.util.concurrent;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.TimeUnit;

/**
 * Abstract base class for {@link EventExecutor} implementations.
 */
public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {

    static final long DEFAULT_SHUTDOWN_QUIET_PERIOD = 2;
    static final long DEFAULT_SHUTDOWN_TIMEOUT = 15;

    private final EventExecutorGroup parent;
    private final Collection selfCollection = Collections.singleton(this);

    protected AbstractEventExecutor() {
        this(null);
    }

    protected AbstractEventExecutor(EventExecutorGroup parent) {
        this.parent = parent;
    }

    @Override
    public EventExecutorGroup parent() {
        return parent;
    }

    @Override
    public EventExecutor next() {
        return this;
    }

    @Override
    public boolean inEventLoop() {
        return inEventLoop(Thread.currentThread());
    }

    @Override
    public Iterator iterator() {
        return new EventExecutorIterator();
    }

    @Override
    public  Set children() {
        return (Set) selfCollection;
    }

    @Override
    public Future shutdownGracefully() {
        return shutdownGracefully(DEFAULT_SHUTDOWN_QUIET_PERIOD, DEFAULT_SHUTDOWN_TIMEOUT, TimeUnit.SECONDS);
    }

    /**
     * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead.
     */
    @Override
    @Deprecated
    public abstract void shutdown();

    /**
     * @deprecated {@link #shutdownGracefully(long, long, TimeUnit)} or {@link #shutdownGracefully()} instead.
     */
    @Override
    @Deprecated
    public List shutdownNow() {
        shutdown();
        return Collections.emptyList();
    }

    @Override
    public  Promise newPromise() {
        return new DefaultPromise(this);
    }

    @Override
    public  ProgressivePromise newProgressivePromise() {
        return new DefaultProgressivePromise(this);
    }

    @Override
    public  Future newSucceededFuture(V result) {
        return new SucceededFuture(this, result);
    }

    @Override
    public  Future newFailedFuture(Throwable cause) {
        return new FailedFuture(this, cause);
    }

    @Override
    public Future submit(Runnable task) {
        return (Future) super.submit(task);
    }

    @Override
    public  Future submit(Runnable task, T result) {
        return (Future) super.submit(task, result);
    }

    @Override
    public  Future submit(Callable task) {
        return (Future) super.submit(task);
    }

    @Override
    protected final  RunnableFuture newTaskFor(Runnable runnable, T value) {
        return new PromiseTask(this, runnable, value);
    }

    @Override
    protected final  RunnableFuture newTaskFor(Callable callable) {
        return new PromiseTask(this, callable);
    }

    @Override
    public ScheduledFuture schedule(Runnable command, long delay,
                                       TimeUnit unit) {
        throw new UnsupportedOperationException();
    }

    @Override
    public  ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) {
        throw new UnsupportedOperationException();
    }

    @Override
    public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
        throw new UnsupportedOperationException();
    }

    @Override
    public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
        throw new UnsupportedOperationException();
    }

    private final class EventExecutorIterator implements Iterator {
        private boolean nextCalled;

        @Override
        public boolean hasNext() {
            return !nextCalled;
        }

        @Override
        public EventExecutor next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            nextCalled = true;
            return AbstractEventExecutor.this;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("read-only");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy