SharedTagContent { public void addDetachListener(final AbstractHtml tag, final DetachListener<T> detachListener) { final long stamp = lock.writeLock(); try { final Set<DetachListener<T>> listeners; if (detachListeners == null) { listeners = new LinkedHashSet<>(4); detachListeners = new WeakHashMap<>(4, 0.75F); detachListeners.put(tag, listeners); } else { listeners = detachListeners.computeIfAbsent(tag, k -> new LinkedHashSet<>(4)); } listeners.add(detachListener); } finally { lock.unlockWrite(stamp); } } SharedTagContent(final T content);  SharedTagContent(final UpdateClientNature updateClientNature,
            final T content);  SharedTagContent(final T content, final boolean contentTypeHtml);  SharedTagContent(final UpdateClientNature updateClientNature,
            final T content, final boolean contentTypeHtml);  SharedTagContent(final boolean shared, final T content,
            final boolean contentTypeHtml);  SharedTagContent(final boolean shared, final T content); T getContent(); boolean isContentTypeHtml(); boolean isShared(); UpdateClientNature getUpdateClientNature(); void setUpdateClientNature(
            final UpdateClientNature updateClientNature); boolean isUpdateClient(); void setUpdateClient(final boolean updateClient); void setShared(final boolean shared); void setContent(final T content); void setContent(final UpdateClientNature updateClientNature,
            final T content); void setContent(final T content, final boolean contentTypeHtml); void setContent(final UpdateClientNature updateClientNature,
            final T content, final boolean contentTypeHtml); void setContent(final Set<AbstractHtml> exclusionTags,
            final T content); void setContent(final Set<AbstractHtml> exclusionTags,
            final T content, final boolean contentTypeHtml); void detach(final Set<AbstractHtml> exclusionTags); void detach(final boolean removeContent); void detach(final boolean removeContent,
            final Set<AbstractHtml> exclusionTags); void detach(final boolean removeContent,
            final Set<AbstractHtml> exclusionTags,
            final Set<AbstractHtml> exclusionClientUpdateTags); void addContentChangeListener(final AbstractHtml tag,
            final ContentChangeListener<T> contentChangeListener); void addDetachListener(final AbstractHtml tag,
            final DetachListener<T> detachListener); void removeContentChangeListener(
            final ContentChangeListener<T> contentChangeListener); void removeContentChangeListener(final AbstractHtml tag,
            final ContentChangeListener<T> contentChangeListener); void removeDetachListener(final DetachListener<T> detachListener); void removeDetachListener(final AbstractHtml tag,
            final DetachListener<T> detachListener); void removeContentChangeListeners(final AbstractHtml tag,
            final Collection<ContentChangeListener<T>> contentChangeListeners); void removeDetachListeners(final AbstractHtml tag,
            final Collection<DetachListener<T>> detachListeners); void removeAllContentChangeListeners(final AbstractHtml tag); void removeAllContentChangeListeners(); void removeAllDetachListeners(final AbstractHtml tag); void removeAllDetachListeners(); ContentFormatter<T> getContentFormatter(final AbstractHtml tag); Set<ContentChangeListener<T>> getContentChangeListeners(
            final AbstractHtml tag); Set<DetachListener<T>> getDetachListeners(final AbstractHtml tag);  }
@Test public void testAddDetachListener() { { SharedTagContent<String> stc = new SharedTagContent<>(UpdateClientNature.SEQUENTIAL, "Test content", true); Div div = new Div(null); Span spanChild1 = new Span(div); spanChild1.addInnerHtml(stc); P pChild2 = new P(div); pChild2.addInnerHtml(stc); AtomicBoolean listenerInvoked = new AtomicBoolean(); stc.addDetachListener(pChild2, new SharedTagContent.DetachListener<String>() { @Override public Runnable detached(DetachEvent<String> detachEvent) { listenerInvoked.set(true); assertEquals("Test content", detachEvent.getContent().getContent()); return () -> { assertEquals("Test content", stc.getContent());}; } }); assertTrue(stc.isContentTypeHtml()); assertEquals("Test content", stc.getContent()); assertEquals("<div><span>Test content</span><p>Test content</p></div>", div.toHtmlString()); Div div2 = new Div(null); div2.addInnerHtml(stc); assertEquals("<div>Test content</div>", div2.toHtmlString()); stc.detach(false); stc.setContent("Content Changed", false); assertFalse(stc.isContentTypeHtml()); assertEquals(UpdateClientNature.SEQUENTIAL, stc.getUpdateClientNature()); assertEquals("Content Changed", stc.getContent()); assertEquals("<div><span>Test content</span><p>Test content</p></div>", div.toHtmlString()); assertEquals("<div>Test content</div>", div2.toHtmlString()); stc.setContent(new HashSet<>(Arrays.asList(spanChild1)), "Content Changed", true); assertTrue(stc.isContentTypeHtml()); assertEquals(UpdateClientNature.SEQUENTIAL, stc.getUpdateClientNature()); } { SharedTagContent<String> stc = new SharedTagContent<>(UpdateClientNature.SEQUENTIAL, "Test content", true); Div div = new Div(null); Span spanChild1 = new Span(div); spanChild1.addInnerHtml(stc); P pChild2 = new P(div); pChild2.addInnerHtml(stc); AtomicBoolean listenerInvoked = new AtomicBoolean(); stc.addDetachListener(pChild2, new SharedTagContent.DetachListener<String>() { @Override public Runnable detached(DetachEvent<String> detachEvent) { listenerInvoked.set(true); assertEquals("Test content", detachEvent.getContent().getContent()); return () -> { assertEquals("Test content", stc.getContent());}; } }); assertTrue(stc.isContentTypeHtml()); assertEquals("Test content", stc.getContent()); assertEquals("<div><span>Test content</span><p>Test content</p></div>", div.toHtmlString()); Div div2 = new Div(null); div2.addInnerHtml(stc); assertEquals("<div>Test content</div>", div2.toHtmlString()); stc.detach(true); stc.setContent("Content Changed", false); assertFalse(stc.isContentTypeHtml()); assertEquals(UpdateClientNature.SEQUENTIAL, stc.getUpdateClientNature()); assertEquals("Content Changed", stc.getContent()); assertEquals("<div><span></span><p></p></div>", div.toHtmlString()); assertEquals("<div></div>", div2.toHtmlString()); stc.setContent(new HashSet<>(Arrays.asList(spanChild1)), "Content Changed", true); assertTrue(stc.isContentTypeHtml()); assertEquals(UpdateClientNature.SEQUENTIAL, stc.getUpdateClientNature()); } }