BrooklynStorageImpl implements BrooklynStorage { @Override public <T> Reference<T> getReference(final String id) { WeakReference<Reference<?>> weakRef = refsCache.get(id); Reference<?> ref = (weakRef != null) ? weakRef.get() : null; if (ref == null) { ref = new BackedReference<T>(refsMap, id) { @Override protected void finalize() { refsCache.remove(id); } }; refsCache.putIfAbsent(id, new WeakReference<Reference<?>>(ref)); } return (Reference<T>) ref; } BrooklynStorageImpl(); @Override Reference<T> getReference(final String id); @Override Reference<List<T>> getNonConcurrentList(final String id); @Override ConcurrentMap<K, V> getMap(final String id); @Override void remove(String id); @Override void terminate(); @Override boolean isMostlyEmpty(); @Override Map<String, Object> getStorageMetrics(); @Override String toString();  }
@Test public void testReferenceGetAndSet() throws Exception { Reference<Object> ref = storage.getReference("mykey"); assertNull(ref.get()); assertTrue(ref.isNull()); assertFalse(ref.contains("different")); assertTrue(ref.contains(null)); ref.set("myval"); assertEqualsCommutative(ref.get(), storage.getReference("mykey").get(), "myval"); assertFalse(ref.isNull()); assertFalse(ref.contains("different")); assertTrue(ref.contains("myval")); ref.clear(); assertNull(ref.get()); }
@Test public void testReferenceAcceptsNullValue() throws Exception { Reference<Object> ref = storage.getReference("mykey"); ref.set(null); assertNull(ref.get()); assertTrue(ref.isNull()); assertTrue(ref.contains(null)); }