/** * Attaches an arbitrary object to this context only if the object was not already attached. If a value has already * been attached with the key provided, the current value associated with the key is returned. * * @param key they attachment key used to ensure uniqueness and used for retrieval of the value. * @param value the value to store. * @param <V> the value type of the attachment. * * @return the previous value associated with the key or {@code null} if there was no previous value. */ public <V> V attachIfAbsent(final AttachmentKey<V> key, final V value) { assert key != null; return key.cast(contextAttachments.putIfAbsent(key, value)); }
/** * Attaches an arbitrary object to this context only if the object was not already attached. If a value has already * been attached with the key provided, the current value associated with the key is returned. * * @param key they attachment key used to ensure uniqueness and used for retrieval of the value. * @param value the value to store. * @param <V> the value type of the attachment. * * @return the previous value associated with the key or {@code null} if there was no previous value. */ public <V> V attachIfAbsent(final AttachmentKey<V> key, final V value) { assert key != null; return key.cast(contextAttachments.putIfAbsent(key, value)); }
/** * Attaches an arbitrary object to this context. * * @param key they attachment key used to ensure uniqueness and used for retrieval of the value. * @param value the value to store. * @param <V> the value type of the attachment. * * @return the previous value associated with the key or {@code null} if there was no previous value. */ public <V> V attach(final AttachmentKey<V> key, final V value) { assert key != null; return key.cast(contextAttachments.put(key, value)); }
/** * Construct a new simple attachment key. * * @param valueClass the value class * @param <T> the attachment type * * @return the new instance */ @SuppressWarnings("unchecked") public static <T> AttachmentKey<T> create(final Class<? super T> valueClass) { return new AttachmentKey(valueClass); } }
/** * Construct a new simple attachment key. * * @param valueClass the value class * @param <T> the attachment type * * @return the new instance */ @SuppressWarnings("unchecked") public static <T> AttachmentKey<T> create(final Class<? super T> valueClass) { return new AttachmentKey(valueClass); } }
/** * Attaches an arbitrary object to this context. * * @param key they attachment key used to ensure uniqueness and used for retrieval of the value. * @param value the value to store. * @param <V> the value type of the attachment. * * @return the previous value associated with the key or {@code null} if there was no previous value. */ public <V> V attach(final AttachmentKey<V> key, final V value) { assert key != null; return key.cast(contextAttachments.put(key, value)); }
/** * Detaches or removes the value from this context. * * @param key the key to the attachment. * @param <V> the value type of the attachment. * * @return the attachment if found otherwise {@code null}. */ public <V> V detach(final AttachmentKey<V> key) { assert key != null; return key.cast(contextAttachments.remove(key)); }
/** * Retrieves an object that has been attached to this context. * * @param key the key to the attachment. * @param <V> the value type of the attachment. * * @return the attachment if found otherwise {@code null}. */ public <V> V getAttachment(final AttachmentKey<V> key) { assert key != null; return key.cast(contextAttachments.get(key)); }
/** * Retrieves an object that has been attached to this context. * * @param key the key to the attachment. * @param <V> the value type of the attachment. * * @return the attachment if found otherwise {@code null}. */ public <V> V getAttachment(final AttachmentKey<V> key) { assert key != null; return key.cast(contextAttachments.get(key)); }
/** * Detaches or removes the value from this context. * * @param key the key to the attachment. * @param <V> the value type of the attachment. * * @return the attachment if found otherwise {@code null}. */ public <V> V detach(final AttachmentKey<V> key) { assert key != null; return key.cast(contextAttachments.remove(key)); }