/** * 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; } }
/** * 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; } }
@Test public void test() { GridMetadataAwareAdapter ma = new GridMetadataAwareAdapter(); assert ma.addMeta(attr1, "val1") == null; assert ma.addMeta(attr2, 1) == null; assert ma.hasMeta(attr1); assert !ma.hasMeta(attr3); assert ma.hasMeta(attr1, "val1"); assert !ma.hasMeta(attr1, "some another val"); assertEquals("val1", ma.meta(attr1)); assertEquals(new Integer(1), ma.meta(attr2)); Object[] allMeta = ma.allMeta(); assert ma.addMetaIfAbsent(attr2, 2) == 1; assert ma.addMetaIfAbsent(attr3, 3) == 3; assert ma.addMetaIfAbsent(attr2, new Callable<Integer>() { @Override public Integer call() throws Exception { return 5; assert ma.addMetaIfAbsent(attr4, new Callable<Integer>() { @Override public Integer call() throws Exception { return 5;
/** * Tests whether or not given metadata is set. * * @param key key of the metadata to test. * @return Whether or not given metadata is set. */ public boolean hasMeta(int key) { return meta(key) != null; }
/** {@inheritDoc} */ @Override public Object clone() { try { GridMetadataAwareAdapter clone = (GridMetadataAwareAdapter)super.clone(); clone.copyMeta(this); return clone; } catch (CloneNotSupportedException ignore) { throw new InternalError(); } } }
/** * Tests whether or not given metadata is set. * * @param key Key of the metadata to test. * @return Whether or not given metadata is set. */ public <V> boolean hasMeta(int key, V val) { Object v = meta(key); return v != null && v.equals(val); }
/** {@inheritDoc} */ @SuppressWarnings({"CloneDoesntDeclareCloneNotSupportedException", "OverriddenMethodCallDuringObjectConstruction"}) @Override public Object clone() { try { GridMetadataAwareAdapter clone = (GridMetadataAwareAdapter)super.clone(); clone.copyMeta(this); return clone; } catch (CloneNotSupportedException ignore) { throw new InternalError(); } } }
/** * 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; } }
/** * 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; } }
/** * Tests whether or not given metadata is set. * * @param key key of the metadata to test. * @return Whether or not given metadata is set. */ public boolean hasMeta(int key) { return meta(key) != null; }
/** * 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; } }
/** * Tests whether or not given metadata is set. * * @param key Key of the metadata to test. * @return Whether or not given metadata is set. */ public <V> boolean hasMeta(int key, V val) { Object v = meta(key); return v != null && v.equals(val); }
/** * 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; } }