View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.peer;
21  
22  import java.util.Iterator;
23  import java.util.LinkedList;
24  import java.util.List;
25  
26  import org.xnap.event.ListListener;
27  import org.xnap.event.ListSupport;
28  
29  /***
30   * Manages {@link HotlistItem} objects.
31   */
32  public class HotlistManager
33  {
34  
35      //--- Constant(s) ---
36  
37      //--- Data field(s) ---
38  
39      private static HotlistManager instance = new HotlistManager();
40  
41      /***
42       * A list of {@link HotlistItem} objects.
43       */
44      private List items = new LinkedList();
45  
46      private ListSupport lsItems = new ListSupport(this);
47  
48      //--- Constructor(s) ---
49  
50      private HotlistManager()
51      {
52      }
53  
54      //--- Method(s) ---
55  
56      /***
57       * Returns the instance of HotlistManager.
58       */
59      public static HotlistManager getInstance()
60      {
61  		return instance;
62      }
63  
64      /***
65       * Adds <code>item</code> to the hotlist. 
66       */
67      public synchronized void add(HotlistItem item)
68      {
69  		items.add(item);
70  		lsItems.fireItemAdded(item);
71      }
72  
73      /***
74       * <code>Listener</code> is notified when a new item is added or removed.
75       */
76      public synchronized void addListListener(ListListener listener)
77      {
78  		lsItems.addListListener(listener);
79      }
80  
81      public synchronized HotlistItem[] getItems()
82      {
83  		return (HotlistItem[])items.toArray(new HotlistItem[0]);
84      }
85  
86      /***
87       * Removes <code>item</code> from the hotlist.
88       */
89      public synchronized void remove(HotlistItem item)
90      {
91  		items.remove(item);
92  		lsItems.fireItemRemoved(item);
93      }
94  
95      /***
96       * Removes all items of class <code>itemClass</code> from the hotlist.
97       */
98      public synchronized void removeByClass(Class itemClass)
99      {
100 		for (Iterator i = items.iterator(); i.hasNext();) {
101 			HotlistItem item = (HotlistItem)i.next();
102 			if (item.getClass() == itemClass) {
103 				i.remove();
104 				lsItems.fireItemRemoved(item);
105 			}
106 		}
107     }
108 
109     /***
110      *
111      */
112     public void removeListListener(ListListener listener)
113     {
114 		lsItems.removeListListener(listener);
115     }
116 
117 }