Skip to content

Commit

Permalink
Add default methods to interfaces to reduce burden on implementations (
Browse files Browse the repository at this point in the history
…#14035)

Motivation:

Now that we use java8 we can have some default implementations.

Modifications:

Add default implementations to interfaces

Result:

Easier to implement interfaces
  • Loading branch information
normanmaurer committed May 12, 2024
1 parent d3fdf71 commit b2b6ccd
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 190 deletions.
Expand Up @@ -60,11 +60,6 @@ public EventExecutor next() {
return this;
}

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

@Override
public Iterator<EventExecutor> iterator() {
return selfCollection.iterator();
Expand Down Expand Up @@ -92,26 +87,6 @@ public List<Runnable> shutdownNow() {
return Collections.emptyList();
}

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

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

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

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

@Override
public Future<?> submit(Runnable task) {
return (Future<?>) super.submit(task);
Expand Down
26 changes: 15 additions & 11 deletions common/src/main/java/io/netty/util/concurrent/EventExecutor.java
Expand Up @@ -24,12 +24,6 @@
*/
public interface EventExecutor extends EventExecutorGroup {

/**
* Returns a reference to itself.
*/
@Override
EventExecutor next();

/**
* Return the {@link EventExecutorGroup} which is the parent of this {@link EventExecutor},
*/
Expand All @@ -38,7 +32,9 @@ public interface EventExecutor extends EventExecutorGroup {
/**
* Calls {@link #inEventLoop(Thread)} with {@link Thread#currentThread()} as argument
*/
boolean inEventLoop();
default boolean inEventLoop() {
return inEventLoop(Thread.currentThread());
}

/**
* Return {@code true} if the given {@link Thread} is executed in the event loop,
Expand All @@ -49,24 +45,32 @@ public interface EventExecutor extends EventExecutorGroup {
/**
* Return a new {@link Promise}.
*/
<V> Promise<V> newPromise();
default <V> Promise<V> newPromise() {
return new DefaultPromise<>(this);
}

/**
* Create a new {@link ProgressivePromise}.
*/
<V> ProgressivePromise<V> newProgressivePromise();
default <V> ProgressivePromise<V> newProgressivePromise() {
return new DefaultProgressivePromise<>(this);
}

/**
* Create a new {@link Future} which is marked as succeeded already. So {@link Future#isSuccess()}
* will return {@code true}. All {@link FutureListener} added to it will be notified directly. Also
* every call of blocking methods will just return without blocking.
*/
<V> Future<V> newSucceededFuture(V result);
default <V> Future<V> newSucceededFuture(V result) {
return new SucceededFuture<>(this, result);
}

/**
* Create a new {@link Future} which is marked as failed already. So {@link Future#isSuccess()}
* will return {@code false}. All {@link FutureListener} added to it will be notified directly. Also
* every call of blocking methods will just return without blocking.
*/
<V> Future<V> newFailedFuture(Throwable cause);
default <V> Future<V> newFailedFuture(Throwable cause) {
return new FailedFuture<>(this, cause);
}
}
140 changes: 0 additions & 140 deletions transport/src/main/java/io/netty/channel/AbstractChannel.java
Expand Up @@ -15,7 +15,6 @@
*/
package io.netty.channel;

import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.socket.ChannelOutputShutdownEvent;
import io.netty.channel.socket.ChannelOutputShutdownException;
import io.netty.util.DefaultAttributeMap;
Expand Down Expand Up @@ -120,28 +119,6 @@ protected DefaultChannelPipeline newChannelPipeline() {
return new DefaultChannelPipeline(this);
}

@Override
public boolean isWritable() {
ChannelOutboundBuffer buf = unsafe.outboundBuffer();
return buf != null && buf.isWritable();
}

@Override
public long bytesBeforeUnwritable() {
ChannelOutboundBuffer buf = unsafe.outboundBuffer();
// isWritable() is currently assuming if there is no outboundBuffer then the channel is not writable.
// We should be consistent with that here.
return buf != null ? buf.bytesBeforeUnwritable() : 0;
}

@Override
public long bytesBeforeWritable() {
ChannelOutboundBuffer buf = unsafe.outboundBuffer();
// isWritable() is currently assuming if there is no outboundBuffer then the channel is not writable.
// We should be consistent with that here.
return buf != null ? buf.bytesBeforeWritable() : Long.MAX_VALUE;
}

@Override
public Channel parent() {
return parent;
Expand All @@ -152,11 +129,6 @@ public ChannelPipeline pipeline() {
return pipeline;
}

@Override
public ByteBufAllocator alloc() {
return config().getAllocator();
}

@Override
public EventLoop eventLoop() {
EventLoop eventLoop = this.eventLoop;
Expand Down Expand Up @@ -219,118 +191,6 @@ public boolean isRegistered() {
return registered;
}

@Override
public ChannelFuture bind(SocketAddress localAddress) {
return pipeline.bind(localAddress);
}

@Override
public ChannelFuture connect(SocketAddress remoteAddress) {
return pipeline.connect(remoteAddress);
}

@Override
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) {
return pipeline.connect(remoteAddress, localAddress);
}

@Override
public ChannelFuture disconnect() {
return pipeline.disconnect();
}

@Override
public ChannelFuture close() {
return pipeline.close();
}

@Override
public ChannelFuture deregister() {
return pipeline.deregister();
}

@Override
public Channel flush() {
pipeline.flush();
return this;
}

@Override
public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
return pipeline.bind(localAddress, promise);
}

@Override
public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise) {
return pipeline.connect(remoteAddress, promise);
}

@Override
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
return pipeline.connect(remoteAddress, localAddress, promise);
}

@Override
public ChannelFuture disconnect(ChannelPromise promise) {
return pipeline.disconnect(promise);
}

@Override
public ChannelFuture close(ChannelPromise promise) {
return pipeline.close(promise);
}

@Override
public ChannelFuture deregister(ChannelPromise promise) {
return pipeline.deregister(promise);
}

@Override
public Channel read() {
pipeline.read();
return this;
}

@Override
public ChannelFuture write(Object msg) {
return pipeline.write(msg);
}

@Override
public ChannelFuture write(Object msg, ChannelPromise promise) {
return pipeline.write(msg, promise);
}

@Override
public ChannelFuture writeAndFlush(Object msg) {
return pipeline.writeAndFlush(msg);
}

@Override
public ChannelFuture writeAndFlush(Object msg, ChannelPromise promise) {
return pipeline.writeAndFlush(msg, promise);
}

@Override
public ChannelPromise newPromise() {
return pipeline.newPromise();
}

@Override
public ChannelProgressivePromise newProgressivePromise() {
return pipeline.newProgressivePromise();
}

@Override
public ChannelFuture newSucceededFuture() {
return pipeline.newSucceededFuture();
}

@Override
public ChannelFuture newFailedFuture(Throwable cause) {
return pipeline.newFailedFuture(cause);
}

@Override
public ChannelFuture closeFuture() {
return closeFuture;
Expand Down

0 comments on commit b2b6ccd

Please sign in to comment.