View Javadoc

1   /*
2    * $Id: LibraryChangeSupport.java,v 1.4 2005/05/31 16:04:12 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.core.libraries;
10  
11  import java.util.ArrayList;
12  import java.util.Iterator;
13  
14  /***
15   * A utility class that will allow classes to use the delegate
16   * pattern to easily handle library change listeners.
17   * 
18   * @author aperezlo
19   */
20  public class LibraryChangeSupport
21  {
22     private ArrayList _listeners = new ArrayList();
23     
24     // all the methods are synchronized, because they basically either are altering 
25     // the collection of listeners, or traversing it, and adding or removing 
26     // listeners during a traversal is not permitted.
27     
28     /***
29      * Notifies all of this object's listeners of the event.
30      * 
31      * @param lce the library change event 
32      */
33     public synchronized void fireLibraryChange(LibraryChangeEvent lce)
34     {
35        Iterator i = _listeners.iterator();
36        ILibraryChangeListener l = null;
37        while(i.hasNext())
38        {
39           l = (ILibraryChangeListener) i.next();
40           l.libraryChanged(lce);
41        }
42     }
43     
44     
45     /***
46      * Unregisters a listener from library change events.  If the specified
47      * listener is not registered, this method has no effect.
48      * @param lcl the listener that should be unregistered
49      */
50     public synchronized void removeLibraryChangeListener(ILibraryChangeListener lcl)
51     {
52        _listeners.remove(lcl);
53     }
54     
55     /***
56      * Registers a listener for library change events.  Duplicate 
57      * registrations have no effect.
58      * 
59      * @param lcl the listener that wants to register for events  
60      */
61     public synchronized void addLibraryChangeListener(ILibraryChangeListener lcl)
62     {
63        if(!_listeners.contains(lcl))
64        {
65           _listeners.add(lcl);
66        }
67     }
68  }