@Test public void testGetSetInt() { AttributeKey<Integer> key = AttributeKey.valueOf("Nada"); Attribute<Integer> one = map.attr(key); assertSame(one, map.attr(key)); one.setIfAbsent(3653); assertEquals(one.get(), Integer.valueOf(3653)); one.setIfAbsent(1); assertNotSame(one.get(), 1); one.remove(); assertNull(one.get()); }
@Override public <T> Attribute<T> attr(AttributeKey<T> key) { Map<AttributeKey<?>, Attribute<?>> map = this.map; if (map == null) { map = new IdentityHashMap<AttributeKey<?>, Attribute<?>>(2); if (!updater.compareAndSet(this, null, map)) { map = this.map; } } synchronized (map) { @SuppressWarnings("unchecked") Attribute<T> attr = (Attribute<T>) map.get(key); if (attr == null) { attr = new DefaultAttribute<T>(map, key); map.put(key, attr); } return attr; } }
DefaultAttributeMap implements AttributeMap { @Override public <T> Attribute<T> attr(AttributeKey<T> key) { Map<AttributeKey<?>, Attribute<?>> map = this.map; if (map == null) { map = new IdentityHashMap<AttributeKey<?>, Attribute<?>>(2); if (!updater.compareAndSet(this, null, map)) { map = this.map; } } synchronized (map) { @SuppressWarnings("unchecked") Attribute<T> attr = (Attribute<T>) map.get(key); if (attr == null) { attr = new DefaultAttribute<T>(map, key); map.put(key, attr); } return attr; } } }
DefaultAttributeMap implements AttributeMap { @Override public <T> Attribute<T> attr(AttributeKey<T> key) { Map<AttributeKey<?>, Attribute<?>> map = this.map; if (map == null) { map = new IdentityHashMap<AttributeKey<?>, Attribute<?>>(2); if (!updater.compareAndSet(this, null, map)) { map = this.map; } } synchronized (map) { @SuppressWarnings("unchecked") Attribute<T> attr = (Attribute<T>) map.get(key); if (attr == null) { attr = new DefaultAttribute<T>(map, key); map.put(key, attr); } return attr; } }  }
DefaultAttributeMap implements AttributeMap { @Override public <T> Attribute<T> attr(AttributeKey<T> key) { Map<AttributeKey<?>, Attribute<?>> map = this.map; if (map == null) { map = new IdentityHashMap<AttributeKey<?>, Attribute<?>>(2); if (!updater.compareAndSet(this, null, map)) { map = this.map; } } synchronized (map) { @SuppressWarnings("unchecked") Attribute<T> attr = (Attribute<T>) map.get(key); if (attr == null) { attr = new DefaultAttribute<T>(map, key); map.put(key, attr); } return attr; } }  @Override Attribute<T> attr(AttributeKey<T> key); }
DefaultAttributeMap implements AttributeMap { @Override public <T> Attribute<T> attr(AttributeKey<T> key) { Map<AttributeKey<?>, Attribute<?>> map = this.map; if (map == null) { map = new IdentityHashMap<AttributeKey<?>, Attribute<?>>(2); if (!updater.compareAndSet(this, null, map)) { map = this.map; } } synchronized (map) { @SuppressWarnings("unchecked") Attribute<T> attr = (Attribute<T>) map.get(key); if (attr == null) { attr = new DefaultAttribute<T>(map, key); map.put(key, attr); } return attr; } }  @Override Attribute<T> attr(AttributeKey<T> key);  }