Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Is this ObserverList a correct use of WeakReferences?

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 120 Times in 104 Posts

    Default Is this ObserverList a correct use of WeakReferences?

    Hi there,

    I never really worked with weak references before. Now I created an ObserverList and used WeakReference to solve the Lapsed Observer problem.
    However, I am not quite sure if I did it right. Unfortunately these kinds of classes are not that easy to test.
    I would really appreciate some input from more experienced programmers on this.

    As a backing List I use a CopyOnWriteArrayList. I also use a custom iterator to filter out lapsed observers.
    Lapsed observers _should_ (if I implemented this correctly) be filtered out everytime the list is iterated over.
    import java.lang.ref.WeakReference;
    import java.util.ConcurrentModificationException;
    import java.util.Iterator;
    import java.util.concurrent.CopyOnWriteArrayList;
     
    public class CowalObsList<E> implements ObsList<E> {
     
    	/**
    	 * We use a copy on write array list to make out observer list thread safe and to protect us against ConcurrentModificationException's.
    	 * 
    	 * @see CopyOnWriteArrayList
    	 * @see ConcurrentModificationException
    	 */
    	private final CopyOnWriteArrayList<WeakReference<E>> list = new CopyOnWriteArrayList<>();
     
    	/**
    	 * Adds the given observer to this observer list.<br>
    	 * This method will not result in a ConcurrentModificationException even if an iteration is currently taking place.<br>
    	 * The added observer will be included in the next iteration over this observer list.
    	 * 
    	 * @param obs
    	 * 		the observer to be added
    	 * @throws NullPointerException
    	 * 		if obs is null
    	 * @see ConcurrentModificationException
    	 */
    	public void addObserver(E obs) {
    		if (obs == null) {
    			throw new NullPointerException();
    		}
    		WeakReference<E> ref = new WeakReference<>(obs);
    		list.add(ref);
    	}
     
    	/**
    	 * Removes the observer from this observer list if it is contained.<br>
    	 * This method will not result in a ConcurrentModificationException even if an iteration is currently taking place.<br>
    	 * If the observer is not contained in this observer list this method does nothing.
    	 * 
    	 * @param obs
    	 * 		the observer to be removed
    	 * @throws NullPointerException
    	 * 		if obs is null
    	 * @see ConcurrentModificationException
    	 */
    	public void removeObserver(E obs) {
    		if (obs == null) {
    			throw new NullPointerException();
    		}
    		for (WeakReference<E> ref : list) {
    			if (ref.get() == obs || ref.get() == null) {
    				list.remove(ref);
    			}
    		}
    	}
     
    	/**
    	 * Returns an iterator over the elements in this observer list in a non defined sequence.<br>
    	 * The returned iterator provides a snapshot of the state of the list when the iterator was constructed.<br>
    	 * No synchronization is needed while traversing the iterator. The iterator does NOT support the remove method.
    	 * 
    	 * @see CopyOnWriteArrayList#iterator()
    	 */
    	public Iterator<E> iterator() {
    		return new CowalObsListIterator<>(this);
    	}
     
    	/**
    	 * This custom Iterator is used to filter out lapsed observers from this observer list.<br>
    	 * It also makes sure that the user does not have to deal with WeakReference's.
    	 */
    	private static class CowalObsListIterator<E> implements Iterator<E> {
     
    		/**
    		 * The CowalObserverList that created this iterator.
    		 */
    		final CowalObsList<E> cowalList;
    		/**
    		 * An iterator of the List that is backing the cowalList.
    		 */
    		final Iterator<WeakReference<E>> delegate;
    		/**
    		 * The next reference from which to return the referent in the {@link #next()} method. 
    		 */
    		WeakReference<E> next;
     
    		public CowalObsListIterator(CowalObsList<E> source) {
    			cowalList = source;
    			delegate = cowalList.list.iterator();
    			next = null;
    		}
     
    		public boolean hasNext() {
    			// Filter out lapsed observers
    			while (next == null && delegate.hasNext()) {
    				WeakReference<E> ref = delegate.next();
    				if (ref.get() == null) {
    					// The reference is referring to a lapsed observer and must be removed from the backing list.
    					cowalList.list.remove(ref);
    				} else {
    					// Breaks the loop.
    					next = ref;
    				}
    			}
    			return next != null;
    		}
     
    		public E next() {
    			E element = next.get();
    			// Important to set next to null after a call to the next() method!
    			next = null;
    			return element;
    		}
     
    	}
     
    }

    Thank you for your time.


  2. #2
    Junior Member
    Join Date
    Jul 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is this ObserverList a correct use of WeakReferences

    The British did that except they did it as the actual rifle finish, not touch up. The commies used it to hide stuff, rust, bad blue, and so on.

Similar Threads

  1. Cannot get the correct output
    By akif13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 6th, 2013, 04:15 AM
  2. Is this a correct use of synchronization?
    By Cornix in forum Threads
    Replies: 3
    Last Post: September 2nd, 2013, 07:21 PM
  3. Not getting the correct output
    By KNAYERS in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 23rd, 2012, 01:59 PM
  4. Can anybody correct the code?
    By ur2cdanger in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 24th, 2011, 01:50 AM
  5. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 08:07 PM