diff --git a/common/src/main/java/io/netty/util/Attribute.java b/common/src/main/java/io/netty/util/Attribute.java index 4e4e44b971..42f3e4fbb9 100644 --- a/common/src/main/java/io/netty/util/Attribute.java +++ b/common/src/main/java/io/netty/util/Attribute.java @@ -43,14 +43,17 @@ public interface Attribute { T getAndSet(T value); /** - * Atomically sets to the given value if this {@link Attribute} does not contain a value at the moment. + * Atomically sets to the given value if this {@link Attribute}'s value is {@code null}. * If it was not possible to set the value as it contains a value it will just return the current value. */ T setIfAbsent(T value); /** - * Removes this attribute from the {@link AttributeMap} and returns the old value.. Subsequent {@link #get()} - * calls will return @{code null}. + * Removes this attribute from the {@link AttributeMap} and returns the old value. Subsequent {@link #get()} + * calls will return {@code null}. + * + * If you only want to return the old value and clear the {@link Attribute} while still keep it in + * {@link AttributeMap} use {@link #getAndSet(Object)} with a value of {@code null}. */ T getAndRemove(); @@ -61,7 +64,10 @@ public interface Attribute { boolean compareAndSet(T oldValue, T newValue); /** - * Removes this attribute from the {@link AttributeMap}. Subsequent {@link #get()} calls will return @{code null}. + * Removes this attribute from the {@link AttributeMap}. Subsequent {@link #get()} calls will return @{code null}. + * + * If you only want to remove the value and clear the {@link Attribute} while still keep it in + * {@link AttributeMap} use {@link #set(Object)} with a value of {@code null}. */ void remove(); } diff --git a/common/src/test/java/io/netty/util/DefaultAttributeMapTest.java b/common/src/test/java/io/netty/util/DefaultAttributeMapTest.java index 1bdb8b019a..447512688b 100644 --- a/common/src/test/java/io/netty/util/DefaultAttributeMapTest.java +++ b/common/src/test/java/io/netty/util/DefaultAttributeMapTest.java @@ -73,10 +73,27 @@ public class DefaultAttributeMapTest { public void testSetRemove() { AttributeKey key = AttributeKey.valueOf("key"); - map.attr(key).set(1); - assertSame(1, map.attr(key).getAndRemove()); + Attribute attr = map.attr(key); + attr.set(1); + assertSame(1, attr.getAndRemove()); - map.attr(key).set(2); - assertSame(2, map.attr(key).get()); + Attribute attr2 = map.attr(key); + attr2.set(2); + assertSame(2, attr2.get()); + assertNotSame(attr, attr2); + } + + @Test + public void testGetAndSetWithNull() { + AttributeKey key = AttributeKey.valueOf("key"); + + Attribute attr = map.attr(key); + attr.set(1); + assertSame(1, attr.getAndSet(null)); + + Attribute attr2 = map.attr(key); + attr2.set(2); + assertSame(2, attr2.get()); + assertSame(attr, attr2); } }