Package com.linecorp.armeria.common
Interface AttributesSetters
- All Known Subinterfaces:
AttributesBuilder
,ConcurrentAttributes
Sets entries for building an
Attributes
or updating ConcurrentAttributes
.-
Method Summary
Modifier and TypeMethodDescription<T> T
getAndSet
(io.netty.util.AttributeKey<T> key, T value) Sets the specified value with the givenAttributeKey
.<T> boolean
remove
(io.netty.util.AttributeKey<T> key) Removes the value associated with the specifiedAttributeKey
in theAttributesGetters.ownAttrs()
.default <T> AttributesSetters
removeAndThen
(io.netty.util.AttributeKey<T> key) Removes the value associated with the specifiedAttributeKey
in theAttributesGetters.ownAttrs()
.set
(io.netty.util.AttributeKey<T> key, T value) Sets the specified value with the givenAttributeKey
.
-
Method Details
-
set
Sets the specified value with the givenAttributeKey
. The old value associated with theAttributeKey
is replaced by the specified value. If anull
value is specified, the value in theAttributesGetters.parent()
is hidden as well.static final AttributeKey<String> USER_ID = AttributeKey.valueOf("USER_ID"); static final AttributeKey<String> SECRET_TOKEN = AttributeKey.valueOf("SECRET_TOKEN"); static final AttributeKey<String> TRACE_ID = AttributeKey.valueOf("TRACE_ID"); Attributes attributes = Attributes.of(USER_ID, "Meri Kim", SECRET_TOKEN, "secret-1", TRACE_ID, "trace-1"); Attributes child = Attributes.builder(attributes) .set(SECRET_TOKEN, null) .set(TRACE_ID, "trace-2") .build(); // Any mutations in the child do not modify the value in the parent. assert attributes.attr(USER_ID).equals("Meri Kim"); assert attributes.attr(SECRET_TOKEN).equals("secret-1"); assert attributes.attr(TRACE_ID).equals("trace-1"); // Inherits the value of USER_ID from the parent. assert child.attr(USER_ID).equals("Meri Kim"); // Hides the value of SECRET_TOKEN that the parent has. assert child.attr(SECRET_TOKEN) == null; // Overrides the parent's TRACE_ID. assert child.attr(TRACE_ID).equals("trace-2");
-
getAndSet
Sets the specified value with the givenAttributeKey
. The old value associated with theAttributeKey
is replaced by the specified value. If anull
value is specified, the value in theAttributesGetters.parent()
is hidden as well.static final AttributeKey<String> USER_ID = AttributeKey.valueOf("USER_ID"); static final AttributeKey<String> SECRET_TOKEN = AttributeKey.valueOf("SECRET_TOKEN"); static final AttributeKey<String> TRACE_ID = AttributeKey.valueOf("TRACE_ID"); Attributes attributes = Attributes.of(USER_ID, "Meri Kim", SECRET_TOKEN, "secret-1", TRACE_ID, "trace-1"); AttributesBuilder newAttributesBuilder = attributes.toBuilder(); assert newAttributesBuilder.getAndSet(SECRET_TOKEN, null).equals("secret-1") assert newAttributesBuilder.getAndSet(TRACE, "trace-2").equals("trace-2") Attributes newAttributes = newAttributesBuilder.build(); // Any mutations in the child do not modify the value in the original attributes. assert attributes.attr(USER_ID).equals("Meri Kim"); assert attributes.attr(SECRET_TOKEN).equals("secret-1"); assert attributes.attr(TRACE_ID).equals("trace-1"); // Copies the value from the original attribute. assert newAttributes.attr(USER_ID).equals("Meri Kim"); // Removes the value of the original attribute. assert newAttributes.attr(SECRET_TOKEN) == null; // Overrides the value of the original attribute. assert newAttributes.attr(TRACE_ID).equals("trace-2");
- Returns:
- the previous value associated with the
AttributeKey
, ornull
if there was no mapping for theAttributeKey
. Anull
can be returned if theAttributeKey
is previously associated withnull
.
-
remove
<T> boolean remove(io.netty.util.AttributeKey<T> key) Removes the value associated with the specifiedAttributeKey
in theAttributesGetters.ownAttrs()
.Note that this method won't remove the value in
AttributesGetters.parent()
.static final AttributeKey<String> USER_ID = AttributeKey.valueOf("USER_ID"); static final AttributeKey<String> SECRET_TOKEN = AttributeKey.valueOf("SECRET_TOKEN"); static final AttributeKey<String> TRACE_ID = AttributeKey.valueOf("TRACE_ID"); Attributes attributes = Attributes.of(USER_ID, "Meri Kim", SECRET_TOKEN, "secret-1"); AttributesBuilder newAttributesBuilder = attributes.toBuilder(); assert newAttributesBuilder.remove(USER_ID); assert !newAttributesBuilder.remove(TRACE_ID); AttributesBuilder childAttributes = Attributes.builder(attributes) .set(TRACE_ID, "secret-2") .remove(USER_ID) .remove(TRACE_ID) .build(); assert attributes.attr(TRACE_ID) == null; // The value in the parent will not be removed. assert attributes.attr(USER_ID).equals("Meri Kim");
- Returns:
true
if the value associated theAttributeKey
has been removed.
-
removeAndThen
Removes the value associated with the specifiedAttributeKey
in theAttributesGetters.ownAttrs()
. Unlikeremove(AttributeKey)
this method returns itself so that the caller can chain the invocations.Note that this method won't remove the value in
AttributesGetters.parent()
.
-