From 1f8e192e21fbb5a0689967919507fe4d7aacd226 Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Tue, 10 May 2016 17:51:37 -0700 Subject: [PATCH] Remove EventExecutor.children Motivation: EventExecutor.children uses generics in such a way that an entire colleciton must be cast to a specific type of object. This interface is not very flexible and is impossible to implement if the EventExecutor type must be wrapped. The current usage of this method also does not have any clear need within Netty. The Iterator interface allows for EventExecutor to be wrapped and forces the caller to make assumptions about types instead of building the assumptions into the interface. Motivation: - Remove EventExecutor.children and undeprecate the iterator() interface Result: EventExecutor interface has one less method and is easier to wrap. --- .../concurrent/AbstractEventExecutor.java | 34 ++----------------- .../netty/util/concurrent/EventExecutor.java | 6 ---- .../util/concurrent/EventExecutorGroup.java | 9 ----- .../MultithreadEventExecutorGroup.java | 8 +---- .../channel/epoll/EpollEventLoopGroup.java | 2 +- .../ThreadPerChannelEventLoopGroup.java | 7 ---- .../netty/channel/nio/NioEventLoopGroup.java | 4 +-- 7 files changed, 6 insertions(+), 64 deletions(-) diff --git a/common/src/main/java/io/netty/util/concurrent/AbstractEventExecutor.java b/common/src/main/java/io/netty/util/concurrent/AbstractEventExecutor.java index 0a7d654ef6..5ef1b7e94b 100644 --- a/common/src/main/java/io/netty/util/concurrent/AbstractEventExecutor.java +++ b/common/src/main/java/io/netty/util/concurrent/AbstractEventExecutor.java @@ -19,8 +19,6 @@ 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; @@ -35,7 +33,7 @@ public abstract class AbstractEventExecutor extends AbstractExecutorService impl static final long DEFAULT_SHUTDOWN_TIMEOUT = 15; private final EventExecutorGroup parent; - private final Collection selfCollection = Collections.singleton(this); + private final Collection selfCollection = Collections.singleton(this); protected AbstractEventExecutor() { this(null); @@ -62,12 +60,7 @@ public abstract class AbstractEventExecutor extends AbstractExecutorService impl @Override public Iterator iterator() { - return new EventExecutorIterator(); - } - - @Override - public Set children() { - return (Set) selfCollection; + return selfCollection.iterator(); } @Override @@ -157,27 +150,4 @@ public abstract class AbstractEventExecutor extends AbstractExecutorService impl 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"); - } - } } diff --git a/common/src/main/java/io/netty/util/concurrent/EventExecutor.java b/common/src/main/java/io/netty/util/concurrent/EventExecutor.java index c54fbeb3f0..027e79d151 100644 --- a/common/src/main/java/io/netty/util/concurrent/EventExecutor.java +++ b/common/src/main/java/io/netty/util/concurrent/EventExecutor.java @@ -32,12 +32,6 @@ public interface EventExecutor extends EventExecutorGroup { @Override EventExecutor next(); - /** - * Returns an unmodifiable singleton set which contains itself. - */ - @Override - Set children(); - /** * Return the {@link EventExecutorGroup} which is the parent of this {@link EventExecutor}, */ diff --git a/common/src/main/java/io/netty/util/concurrent/EventExecutorGroup.java b/common/src/main/java/io/netty/util/concurrent/EventExecutorGroup.java index 0ca5c82e60..a4daa78614 100644 --- a/common/src/main/java/io/netty/util/concurrent/EventExecutorGroup.java +++ b/common/src/main/java/io/netty/util/concurrent/EventExecutorGroup.java @@ -84,18 +84,9 @@ public interface EventExecutorGroup extends ScheduledExecutorService, Iterable iterator(); - /** - * Returns the unmodifiable set of {@link EventExecutor}s managed by this {@link EventExecutorGroup}. - */ - Set children(); - @Override Future submit(Runnable task); diff --git a/common/src/main/java/io/netty/util/concurrent/MultithreadEventExecutorGroup.java b/common/src/main/java/io/netty/util/concurrent/MultithreadEventExecutorGroup.java index 5782b6fe28..e0518f236c 100644 --- a/common/src/main/java/io/netty/util/concurrent/MultithreadEventExecutorGroup.java +++ b/common/src/main/java/io/netty/util/concurrent/MultithreadEventExecutorGroup.java @@ -130,7 +130,7 @@ public abstract class MultithreadEventExecutorGroup extends AbstractEventExecuto @Override public Iterator iterator() { - return children().iterator(); + return readonlyChildren.iterator(); } /** @@ -141,12 +141,6 @@ public abstract class MultithreadEventExecutorGroup extends AbstractEventExecuto return children.length; } - @Override - @SuppressWarnings("unchecked") - public final Set children() { - return (Set) readonlyChildren; - } - /** * Create a new EventExecutor which will later then accessible via the {@link #next()} method. This method will be * called for each thread that will serve this {@link MultithreadEventExecutorGroup}. diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java index 6a7b3162bb..6f9351210d 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollEventLoopGroup.java @@ -106,7 +106,7 @@ public final class EpollEventLoopGroup extends MultithreadEventLoopGroup { * {@code 50}, which means the event loop will try to spend the same amount of time for I/O as for non-I/O tasks. */ public void setIoRatio(int ioRatio) { - for (EventExecutor e: children()) { + for (EventExecutor e: this) { ((EpollEventLoop) e).setIoRatio(ioRatio); } } diff --git a/transport/src/main/java/io/netty/channel/ThreadPerChannelEventLoopGroup.java b/transport/src/main/java/io/netty/channel/ThreadPerChannelEventLoopGroup.java index 700625506c..55c90ced42 100644 --- a/transport/src/main/java/io/netty/channel/ThreadPerChannelEventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/ThreadPerChannelEventLoopGroup.java @@ -49,7 +49,6 @@ public class ThreadPerChannelEventLoopGroup extends AbstractEventExecutorGroup i final Executor executor; final Set activeChildren = Collections.newSetFromMap(PlatformDependent.newConcurrentHashMap()); - private final Set readOnlyActiveChildren = Collections.unmodifiableSet(activeChildren); final Queue idleChildren = new ConcurrentLinkedQueue(); private final ChannelException tooManyChannels; @@ -147,12 +146,6 @@ public class ThreadPerChannelEventLoopGroup extends AbstractEventExecutorGroup i return new ReadOnlyIterator(activeChildren.iterator()); } - @Override - @SuppressWarnings("unchecked") - public Set children() { - return (Set) readOnlyActiveChildren; - } - @Override public EventLoop next() { throw new UnsupportedOperationException(); diff --git a/transport/src/main/java/io/netty/channel/nio/NioEventLoopGroup.java b/transport/src/main/java/io/netty/channel/nio/NioEventLoopGroup.java index 98a7dac497..d23ae83ebb 100644 --- a/transport/src/main/java/io/netty/channel/nio/NioEventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/nio/NioEventLoopGroup.java @@ -89,7 +89,7 @@ public class NioEventLoopGroup extends MultithreadEventLoopGroup { * {@code 50}, which means the event loop will try to spend the same amount of time for I/O as for non-I/O tasks. */ public void setIoRatio(int ioRatio) { - for (EventExecutor e: children()) { + for (EventExecutor e: this) { ((NioEventLoop) e).setIoRatio(ioRatio); } } @@ -99,7 +99,7 @@ public class NioEventLoopGroup extends MultithreadEventLoopGroup { * around the infamous epoll 100% CPU bug. */ public void rebuildSelectors() { - for (EventExecutor e: children()) { + for (EventExecutor e: this) { ((NioEventLoop) e).rebuildSelector(); } }