/** * Adds given metadata value only if it was absent. * * @param key Metadata key. * @param val Value to add if it's not attached already. * @param <V> Type of the value. * @return {@code null} if new value was put, or current value if put didn't happen. */ @Nullable public <V> V putMetaIfAbsent(int key, V val) { assert val != null; synchronized (this) { V v = (V)meta(key); if (v == null) return addMeta(key, val); return v; } }
/** * Adds given metadata value only if it was absent. This method always returns the latest value and never previous * one. * * @param key Metadata key. * @param val Value to add if it's not attached already. * @param <V> Type of the value. * @return The value of the metadata after execution of this method. */ public <V> V addMetaIfAbsent(int key, V val) { assert val != null; synchronized (this) { V v = (V)meta(key); if (v == null) addMeta(key, v = val); return v; } }
/** * Adds given metadata value only if it was absent. * * @param key Metadata key. * @param c Factory closure to produce value to add if it's not attached already. Not that unlike {@link * #addMeta(int, Object)} method the factory closure will not be called unless the value is required and therefore * value will only be created when it is actually needed. If {@code null} and metadata value is missing - {@code * null} will be returned from this method. * @param <V> Type of the value. * @return The value of the metadata after execution of this method. */ @Nullable public <V> V addMetaIfAbsent(int key, @Nullable Callable<V> c) { assert c != null; synchronized (this) { V v = (V)meta(key); if (v == null && c != null) try { addMeta(key, v = c.call()); } catch (Exception e) { throw F.wrap(e); } return v; } }
/** * Replaces given metadata with new {@code newVal} value only if its current value is equal to {@code curVal}. * Otherwise, it is no-op. * * @param key Key of the metadata. * @param curVal Current value to check. * @param newVal New value. * @return {@code true} if replacement occurred, {@code false} otherwise. */ @SuppressWarnings({"RedundantTypeArguments"}) public <V> boolean replaceMeta(int key, V curVal, V newVal) { assert newVal != null; assert curVal != null; synchronized (this) { if (hasMeta(key)) { V val = this.<V>meta(key); if (val != null && val.equals(curVal)) { addMeta(key, newVal); return true; } } return false; } }
/** * Adds given metadata value only if it was absent. This method always returns the latest value and never previous * one. * * @param key Metadata key. * @param val Value to add if it's not attached already. * @param <V> Type of the value. * @return The value of the metadata after execution of this method. */ @SuppressWarnings({"unchecked"}) public <V> V addMetaIfAbsent(int key, V val) { assert val != null; synchronized (this) { V v = (V)meta(key); if (v == null) addMeta(key, v = val); return v; } }
/** * Adds given metadata value only if it was absent. * * @param key Metadata key. * @param val Value to add if it's not attached already. * @param <V> Type of the value. * @return {@code null} if new value was put, or current value if put didn't happen. */ @SuppressWarnings({"unchecked"}) @Nullable public <V> V putMetaIfAbsent(int key, V val) { assert val != null; synchronized (this) { V v = (V)meta(key); if (v == null) return addMeta(key, val); return v; } }
/** * Adds given metadata value only if it was absent. * * @param key Metadata key. * @param c Factory closure to produce value to add if it's not attached already. Not that unlike {@link * #addMeta(int, Object)} method the factory closure will not be called unless the value is required and therefore * value will only be created when it is actually needed. If {@code null} and metadata value is missing - {@code * null} will be returned from this method. * @param <V> Type of the value. * @return The value of the metadata after execution of this method. */ @SuppressWarnings({"unchecked"}) @Nullable public <V> V addMetaIfAbsent(int key, @Nullable Callable<V> c) { assert c != null; synchronized (this) { V v = (V)meta(key); if (v == null && c != null) try { addMeta(key, v = c.call()); } catch (Exception e) { throw F.wrap(e); } return v; } }
/** * Replaces given metadata with new {@code newVal} value only if its current value is equal to {@code curVal}. * Otherwise, it is no-op. * * @param key Key of the metadata. * @param curVal Current value to check. * @param newVal New value. * @return {@code true} if replacement occurred, {@code false} otherwise. */ @SuppressWarnings({"RedundantTypeArguments"}) public <V> boolean replaceMeta(int key, V curVal, V newVal) { assert newVal != null; assert curVal != null; synchronized (this) { if (hasMeta(key)) { V val = this.<V>meta(key); if (val != null && val.equals(curVal)) { addMeta(key, newVal); return true; } } return false; } }