Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to start by saying that you have a couple of requirements that don't make sense together. You're looking for a collection that removes duplicates and supports weak references, which indicates to me that listeners may appear and disappear at indeterminate times. Yet you want to maintain insertion order, and allow one listener to cancel all <em>subsequent</em> notifications. To me, this sounds like a recipe for hard-to-find bugs, and I strongly suggest rethinking it.</p> <p>That said, you have one requirement that pretty much drives the solution: you don't want the <code>ConcurrentModificationException</code> that could come from a normal iterator. Which means that you're going to have to copy the original list. Along the way, you can check and remove the empty references:</p> <pre><code>// the master list List&lt;WeakReference&lt;MyListener&gt;&gt; _list = new ArrayList&lt;WeakReference&lt;MyListener&gt;&gt;(); // inside your send-notification method List&lt;MyListener&gt; toNotify = new ArrayList&lt;MyListener&gt;(_list.size()); Iterator&lt;WeakReference&lt;MyListener&gt;&gt; itx = _list.iterator(); while (itx.hasNext()) { WeakReference&lt;MyListener&gt; ref = itx.next(); MyListener lsnr = ref.get(); if (lsnr != null) toNotify.add(lsnr); else itx.remove(); } // now iterate "toNotify" and invoke the listeners </code></pre> <p>You're probably freaking out now, saying "a List! that's a linear data structure! I can't use that, insertion is O(N)!"</p> <p>Well, yes you can. I don't know how many listeners you're planning to have. But as long as you're &lt; 100 (and more likely &lt; 100,000), the cost of a linear search for insert and remove isn't going to matter.</p> <p>Far more interesting from a coding perspective is how you deal with the weak reference. You'll note that I explicitly dereference it, into a variable, before testing the referent for null. This is critically important code when dealing with reference objects: although it's extremely unlikely that the referent will be collected between two calls to <code>get()</code>, it's possible.</p> <p>Which brings me to the <code>WeakReference</code> itself. You'll need to create your own subclass that overrides the <code>equals()</code> and <code>hashCode()</code> methods to delegate to its referent. I thought I had just such a class lying around, but apparently not, so will leave it for you to implement.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload