Rename Deref.isInstanceOf to referentIsInstanceOf
Motivation: Derefs are not necessarily their referents. This is the case for Send, for instance. Modification: The Deref.isInstanceOf method is renamed to referentIsInstanceOf. And a Send.isSendOf method has been added, that simplifies the check for sends, since it could be the case that one also needs to check if the object in question is also a Send instance. Result: Cleaner code that is easier to read, when working with Sends. This fixes https://github.com/netty/netty-incubator-buffer-api/issues/46
This commit is contained in:
parent
b541a20b4c
commit
2decac081a
@ -48,5 +48,5 @@ public interface Deref<T extends Rc<T>> extends Supplier<T> {
|
||||
* @param cls The type to check.
|
||||
* @return {@code true} if the object in this {@code Deref} can be assigned fields or variables of the given type.
|
||||
*/
|
||||
boolean isInstanceOf(Class<?> cls);
|
||||
boolean referentIsInstanceOf(Class<?> cls);
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ public interface Rc<I extends Rc<I>> extends AutoCloseable, Deref<I> {
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean isInstanceOf(Class<?> cls) {
|
||||
default boolean referentIsInstanceOf(Class<?> cls) {
|
||||
return cls.isInstance(this);
|
||||
}
|
||||
|
||||
|
@ -56,7 +56,7 @@ public interface Send<T extends Rc<T>> extends Deref<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInstanceOf(Class<?> cls) {
|
||||
public boolean referentIsInstanceOf(Class<?> cls) {
|
||||
return cls.isAssignableFrom(concreteObjectType);
|
||||
}
|
||||
|
||||
@ -69,6 +69,19 @@ public interface Send<T extends Rc<T>> extends Deref<T> {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given candidate object is an instance of a {@link Send} from which an object of the given type
|
||||
* can be received.
|
||||
*
|
||||
* @param type The type of object we wish to receive.
|
||||
* @param candidate The candidate object that might be a {@link Send} of an object of the given type.
|
||||
* @return {@code true} if the candidate object is a {@link Send} that would deliver an object of the given type,
|
||||
* otherwise {@code false}.
|
||||
*/
|
||||
static boolean isSendOf(Class<?> type, Object candidate) {
|
||||
return candidate instanceof Send && ((Send<?>) candidate).referentIsInstanceOf(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive the {@link Rc} instance being sent, and bind its ownership to the calling thread. The invalidation of the
|
||||
* sent Rc in the sending thread happens-before the return of this method.
|
||||
|
@ -55,7 +55,7 @@ class TransferSend<I extends Rc<I>, T extends Rc<I>> implements Send<I> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInstanceOf(Class<?> cls) {
|
||||
public boolean referentIsInstanceOf(Class<?> cls) {
|
||||
return cls.isAssignableFrom(concreteType);
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package io.netty.buffer.api;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
@ -131,4 +132,26 @@ public class BufferSendTest extends BufferTestSupport {
|
||||
assertEquals(72, bifB.readInt());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isSendOfMustCheckObjectTypes() {
|
||||
try (BufferAllocator allocator = BufferAllocator.heap()) {
|
||||
Send<Buffer> bufferSend = allocator.allocate(8).send();
|
||||
Send<BufferRef> bufferRefSend = new BufferRef(allocator.allocate(8).send()).send();
|
||||
try {
|
||||
assertTrue(Send.isSendOf(Buffer.class, bufferSend));
|
||||
assertFalse(Send.isSendOf(BufferRef.class, bufferSend));
|
||||
assertFalse(Send.isSendOf(Buffer.class, bufferRefSend));
|
||||
assertTrue(Send.isSendOf(BufferRef.class, bufferRefSend));
|
||||
assertFalse(Send.isSendOf(Buffer.class, new Object()));
|
||||
assertFalse(Send.isSendOf(Object.class, new Object()));
|
||||
} finally {
|
||||
bufferSend.discard();
|
||||
bufferRefSend.discard();
|
||||
}
|
||||
// Type checks must still pass after the sends have been received.
|
||||
assertTrue(Send.isSendOf(Buffer.class, bufferSend));
|
||||
assertTrue(Send.isSendOf(BufferRef.class, bufferRefSend));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user