author | Medo <smaxein@googlemail.com> |
Thu, 19 Jul 2012 18:58:18 +0200 | |
changeset 7342 | 0e29eec2df5c |
permissions | -rw-r--r-- |
7342
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
1 |
package org.hedgewars.hedgeroid.netplay; |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
2 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
import java.util.Collection; |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
import java.util.Collections; |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
import java.util.LinkedHashMap; |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
import java.util.LinkedList; |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
import java.util.List; |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
import java.util.Map; |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
9 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
10 |
/** |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
* A map of items, sorted by time of insertion (earliest first). |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
* Observers can be notified about insertions, deletions and changes (which don't change the order). |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
* This is useful for e.g. the lists of current rooms and players, because it allows easy addition |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
* and removal of entries on the one side, as well as reaction to these events by UI elements. |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
*/ |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
public class ObservableLinkedHashMap<K,V> { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
private LinkedHashMap<K,V> map = new LinkedHashMap<K,V>(); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
private List<Observer<K,V>> observers = new LinkedList<Observer<K,V>>(); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
public Collection<V> getValues() { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
return Collections.unmodifiableCollection(map.values()); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
public Map<K,V> getMap() { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
return Collections.unmodifiableMap(map); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
public void observe(Observer<K,V> observer) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
observers.add(observer); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
public void unobserve(Observer<K,V> observer) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
observers.remove(observer); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
// TODO ugh |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
public void clear() { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
while(!map.isEmpty()) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
remove(map.keySet().iterator().next()); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
public void put(K key, V value) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
V oldValue = map.put(key, value); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
Map<K,V> unmodifiableMap = Collections.unmodifiableMap(map); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
46 |
if(oldValue != null) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
47 |
for(Observer<K,V> o : observers) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
48 |
o.itemReplaced(unmodifiableMap, key, oldValue, value); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
49 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
50 |
} else { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
51 |
for(Observer<K,V> o : observers) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
52 |
o.itemAdded(unmodifiableMap, key, value); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
53 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
54 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
55 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
56 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
57 |
public void remove(K key) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
58 |
V oldValue = map.remove(key); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
59 |
if(oldValue != null) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
60 |
Map<K,V> unmodifiableMap = Collections.unmodifiableMap(map); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
61 |
for(Observer<K,V> o : observers) { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
62 |
o.itemRemoved(unmodifiableMap, key, oldValue); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
63 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
64 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
65 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
66 |
|
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
67 |
public static interface Observer<K,V> { |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
68 |
void itemAdded(Map<K,V> map, K key, V value); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
69 |
void itemRemoved(Map<K,V> map, K key, V oldValue); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
70 |
void itemReplaced(Map<K,V> map, K key, V oldValue, V newValue); |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
71 |
} |
0e29eec2df5c
Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
72 |
} |