Debian Codegraph @

src/java/org/astrogrid/samp/client/TrackedClientSet.java

raw
1 package org.astrogrid.samp.client;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6 import org.astrogrid.samp.Client;
7
8 /**
9 * Collection of Client objects which can be notified and interrogated
10 * about the clients which are currently registered.
11 * Instances of this class are thread-safe.
12 *
13 * @author Mark Taylor
14 * @since 25 Nov 2008
15 */
16 public class TrackedClientSet {
17
18 private final Map clientMap_;
19 private final Map clientMapView_;
20
21 /**
22 * Constructor.
23 */
24 public TrackedClientSet() {
25 clientMap_ = new HashMap();
26 clientMapView_ =
27 Collections.synchronizedMap( Collections
28 .unmodifiableMap( clientMap_ ) );
29 }
30
31 /**
32 * Adds a client to this model.
33 * Listeners are informed. May be called from any thread.
34 *
35 * @param client client to add
36 */
37 public void addClient( Client client ) {
38 synchronized ( clientMapView_ ) {
39 clientMap_.put( client.getId(), client );
40 clientMapView_.notifyAll();
41 }
42 }
43
44 /**
45 * Removes a client from this model.
46 * Listeners are informed. May be called from any thread.
47 *
48 * @param client client to remove
49 */
50 public synchronized void removeClient( Client client ) {
51 Client c;
52 synchronized ( clientMapView_ ) {
53 c = (Client) clientMap_.remove( client.getId() );
54 clientMapView_.notifyAll();
55 }
56 boolean removed = c != null;
57 if ( ! removed ) {
58 throw new IllegalArgumentException( "No such client " + client );
59 }
60 assert client.equals( c );
61 }
62
63 /**
64 * Sets the contents of this model to a given list.
65 * Listeners are informed. May be called from any thread.
66 *
67 * @param clients current client list
68 */
69 public synchronized void setClients( Client[] clients ) {
70 synchronized ( clientMapView_ ) {
71 clientMap_.clear();
72 for ( int ic = 0; ic < clients.length; ic++ ) {
73 Client client = clients[ ic ];
74 clientMap_.put( client.getId(), client );
75 }
76 clientMapView_.notifyAll();
77 }
78 }
79
80 /**
81 * Notifies listeners that a given client's attributes (may) have
82 * changed. May be called from any thread.
83 *
84 * @param client modified client
85 * @param metaChanged true if metadata may have changed
86 * (false if known unchanged)
87 * @param subsChanged true if subscriptions may have changed
88 * (false if known unchanged)
89 */
90 public void updateClient( Client client,
91 boolean metaChanged, boolean subsChanged ) {
92 synchronized ( clientMapView_ ) {
93 clientMapView_.notifyAll();
94 }
95 }
96
97 /**
98 * Returns an unmodifiable Map representing the client list.
99 * Keys are client IDs and values are {@link org.astrogrid.samp.Client}
100 * objects.
101 * <p>This map is {@link java.util.Collections#synchronizedMap synchronized}
102 * which means that to iterate over any of its views
103 * you must synchronize on it.
104 * When the map or any of its contents changes, it will receive a
105 * {@link java.lang.Object#notifyAll}.
106 *
107 * @return id -&gt; Client map
108 */
109 public Map getClientMap() {
110 return clientMapView_;
111 }
112 }