|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| EventNotifier.java | - | 79.2% | 80% | 79.3% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model; | |
| 38 | ||
| 39 | import java.util.LinkedList; | |
| 40 | import edu.rice.cs.util.ReaderWriterLock; | |
| 41 | ||
| 42 | /** Base class for all component-specific EventNotifiers. This class provides common methods to | |
| 43 | * manage listeners of a specific type. T the type of the listener class to be managed. | |
| 44 | * @version $Id: EventNotifier.java 5175 2010-01-20 08:46:32Z mgricken $ | |
| 45 | */ | |
| 46 | public abstract class EventNotifier<T> { | |
| 47 | /** All T Listeners that are listening to the model. Accesses to this collection are protected by the | |
| 48 | * ReaderWriterLock. The collection must be synchronized, since multiple readers could access it at once. | |
| 49 | */ | |
| 50 | protected final LinkedList<T> _listeners = new LinkedList<T>(); | |
| 51 | ||
| 52 | /** Provides synchronization primitives for solving the readers/writers problem. In EventNotifier, adding and | |
| 53 | * removing listeners are considered write operations, and all notifications are considered read operations. Multiple | |
| 54 | * reads can occur simultaneously, but only one write can occur at a time, and no reads can occur during a write. | |
| 55 | */ | |
| 56 | protected final ReaderWriterLock _lock = new ReaderWriterLock(); | |
| 57 | ||
| 58 | /** Adds a listener to the notifier. | |
| 59 | * @param listener a listener that reacts on events | |
| 60 | */ | |
| 61 | 3600 | public void addListener(T listener) { |
| 62 | 3600 | _lock.startWrite(); |
| 63 | 3600 | try { _listeners.add(listener); } |
| 64 | 3600 | finally { _lock.endWrite(); } |
| 65 | } | |
| 66 | ||
| 67 | /** Removes a listener from the notifier. If the thread already holds the lock, | |
| 68 | * then the listener is removed later, but as soon as possible. | |
| 69 | * Note: It is NOT guaranteed that the listener will not be executed again. | |
| 70 | * @param listener a listener that reacts on events | |
| 71 | */ | |
| 72 | 195 | public void removeListener(final T listener) { |
| 73 | 195 | try { |
| 74 | 195 | _lock.startWrite(); |
| 75 | 194 | try { _listeners.remove(listener); } |
| 76 | 194 | finally { _lock.endWrite(); } |
| 77 | } | |
| 78 | catch(ReaderWriterLock.DeadlockException e) { | |
| 79 | // couldn't remove right now because this thread already owns a lock | |
| 80 | // remember to remove it later | |
| 81 | 1 | new Thread(new Runnable() { |
| 82 | 1 | public void run() { |
| 83 | 1 | _lock.startWrite(); |
| 84 | 1 | try { _listeners.remove(listener); } |
| 85 | 1 | finally { _lock.endWrite(); } |
| 86 | } | |
| 87 | }, "Pending Listener Removal").start(); | |
| 88 | // synchronized(_listenersToRemove) { | |
| 89 | // _listenersToRemove.add(listener); | |
| 90 | // } | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| 94 | /** Removes all listeners from this notifier. If the thread already holds the lock, | |
| 95 | * then the listener is removed later, but as soon as possible. | |
| 96 | * Note: It is NOT guaranteed that the listener will not be executed again. */ | |
| 97 | 267 | public void removeAllListeners() { |
| 98 | 267 | try { |
| 99 | 267 | _lock.startWrite(); |
| 100 | 267 | try { _listeners.clear(); } |
| 101 | 267 | finally { _lock.endWrite(); } |
| 102 | } | |
| 103 | catch(ReaderWriterLock.DeadlockException e) { | |
| 104 | // couldn't remove right now because this thread already owns a lock | |
| 105 | // remember to remove it later | |
| 106 | 0 | new Thread(new Runnable() { |
| 107 | 0 | public void run() { |
| 108 | 0 | _lock.startWrite(); |
| 109 | 0 | try { _listeners.clear(); } |
| 110 | 0 | finally { _lock.endWrite(); } |
| 111 | } | |
| 112 | }, "Pending Listener Removal").start(); | |
| 113 | } | |
| 114 | } | |
| 115 | } |
|
||||||||||