Make BufHolder protected methods final if they're not meant to be overwritten

This commit is contained in:
Chris Vest 2020-12-16 17:06:22 +01:00
parent 6b91751bea
commit 008c5ed6ec
4 changed files with 35 additions and 21 deletions

View File

@ -86,12 +86,7 @@ public abstract class BufHolder<T extends BufHolder<T>> implements Rc<T> {
@Override @Override
public Send<T> send() { public Send<T> send() {
var send = buf.send(); var send = buf.send();
return new Send<T>() { return () -> receive(send.receive());
@Override
public T receive() {
return BufHolder.this.receive(send.receive());
}
};
} }
/** /**
@ -116,7 +111,7 @@ public abstract class BufHolder<T extends BufHolder<T>> implements Rc<T> {
* *
* @param newBuf The new {@link Buf} instance that is replacing the currently held buffer. * @param newBuf The new {@link Buf} instance that is replacing the currently held buffer.
*/ */
protected void replace(Buf newBuf) { protected final void replaceBuf(Buf newBuf) {
try (var ignore = buf) { try (var ignore = buf) {
buf = newBuf.acquire(); buf = newBuf.acquire();
} }
@ -134,7 +129,7 @@ public abstract class BufHolder<T extends BufHolder<T>> implements Rc<T> {
* *
* @param send The new {@link Buf} instance that is replacing the currently held buffer. * @param send The new {@link Buf} instance that is replacing the currently held buffer.
*/ */
protected void replace(Send<Buf> send) { protected final void replaceBuf(Send<Buf> send) {
try (var ignore = buf) { try (var ignore = buf) {
buf = send.receive(); buf = send.receive();
} }
@ -152,7 +147,7 @@ public abstract class BufHolder<T extends BufHolder<T>> implements Rc<T> {
* *
* @param newBuf The new {@link Buf} instance that is replacing the currently held buffer. * @param newBuf The new {@link Buf} instance that is replacing the currently held buffer.
*/ */
protected void replaceVolatile(Buf newBuf) { protected final void replaceBufVolatile(Buf newBuf) {
var prev = (Buf) BUF.getAndSet(this, newBuf.acquire()); var prev = (Buf) BUF.getAndSet(this, newBuf.acquire());
prev.close(); prev.close();
} }
@ -167,9 +162,9 @@ public abstract class BufHolder<T extends BufHolder<T>> implements Rc<T> {
* <p> * <p>
* The buffer assignment is performed using a volatile store. * The buffer assignment is performed using a volatile store.
* *
* @param send The new {@link Buf} instance that is replacing the currently held buffer. * @param send The {@link Send} with the new {@link Buf} instance that is replacing the currently held buffer.
*/ */
protected void replaceVolatile(Send<Buf> send) { protected final void replaceBufVolatile(Send<Buf> send) {
var prev = (Buf) BUF.getAndSet(this, send.receive()); var prev = (Buf) BUF.getAndSet(this, send.receive());
prev.close(); prev.close();
} }
@ -181,7 +176,7 @@ public abstract class BufHolder<T extends BufHolder<T>> implements Rc<T> {
* *
* @return The {@link Buf} instance being held by this {@linkplain T buffer holder}. * @return The {@link Buf} instance being held by this {@linkplain T buffer holder}.
*/ */
protected Buf getBuf() { protected final Buf getBuf() {
return buf; return buf;
} }
@ -192,7 +187,7 @@ public abstract class BufHolder<T extends BufHolder<T>> implements Rc<T> {
* *
* @return The {@link Buf} instance being held by this {@linkplain T buffer holder}. * @return The {@link Buf} instance being held by this {@linkplain T buffer holder}.
*/ */
protected Buf getBufVolatile() { protected final Buf getBufVolatile() {
return (Buf) BUF.getVolatile(this); return (Buf) BUF.getVolatile(this);
} }
} }

View File

@ -49,14 +49,32 @@ public final class BufRef extends BufHolder<BufRef> {
return new BufRef(buf); return new BufRef(buf);
} }
@Override /**
public void replaceVolatile(Buf newBuf) { * Replace the underlying referenced buffer with the given buffer.
super.replaceVolatile(newBuf); * <p>
* <strong>Note:</strong> this method decreases the reference count of the current buffer,
* and increases the reference count of the new buffer.
* <p>
* The buffer assignment is performed using a volatile store.
*
* @param newBuf The new {@link Buf} instance that is replacing the currently held buffer.
*/
public void replace(Buf newBuf) {
replaceBufVolatile(newBuf);
} }
@Override /**
public void replaceVolatile(Send<Buf> send) { * Replace the underlying referenced buffer with the given buffer.
super.replaceVolatile(send); * <p>
* <strong>Note:</strong> this method decreases the reference count of the current buffer,
* and takes exclusive ownership of the sent buffer.
* <p>
* The buffer assignment is performed using a volatile store.
*
* @param send The {@link Send} with the new {@link Buf} instance that is replacing the currently held buffer.
*/
public void replace(Send<Buf> send) {
replaceBufVolatile(send);
} }
/** /**

View File

@ -28,6 +28,7 @@ package io.netty.buffer.api;
* *
* @param <T> * @param <T>
*/ */
@FunctionalInterface
public interface Send<T extends Rc<T>> { public interface Send<T extends Rc<T>> {
/** /**
* Receive the {@link Rc} instance being sent, and bind its ownership to the calling thread. The invalidation of the * Receive the {@link Rc} instance being sent, and bind its ownership to the calling thread. The invalidation of the

View File

@ -60,7 +60,7 @@ class BufRefTest {
assertThat(ref.contents().readInt()).isEqualTo(42); assertThat(ref.contents().readInt()).isEqualTo(42);
try (Buf buf = allocator.allocate(8)) { try (Buf buf = allocator.allocate(8)) {
ref.replaceVolatile(buf); // Pass replacement directly. ref.replace(buf); // Pass replacement directly.
} }
assertThrows(IllegalStateException.class, () -> orig.writeInt(32)); assertThrows(IllegalStateException.class, () -> orig.writeInt(32));
@ -84,7 +84,7 @@ class BufRefTest {
assertThat(ref.contents().readInt()).isEqualTo(42); assertThat(ref.contents().readInt()).isEqualTo(42);
try (Buf buf = allocator.allocate(8)) { try (Buf buf = allocator.allocate(8)) {
ref.replaceVolatile(buf.send()); // Pass replacement via send(). ref.replace(buf.send()); // Pass replacement via send().
} }
assertThrows(IllegalStateException.class, () -> orig.writeInt(32)); assertThrows(IllegalStateException.class, () -> orig.writeInt(32));