1 | |
package cz.cuni.amis.utils.collections; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Collection; |
5 | |
import java.util.Collections; |
6 | |
import java.util.Iterator; |
7 | |
import java.util.LinkedList; |
8 | |
|
9 | |
import cz.cuni.amis.utils.listener.Listeners; |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
public class ObservableCollection<E> implements Collection<E> { |
18 | |
|
19 | 0 | protected Collection<E> col = null; |
20 | 0 | protected Listeners<CollectionEventListener> eventListeners = new Listeners<CollectionEventListener>(); |
21 | |
|
22 | 0 | private abstract class ListenerNotifier implements Listeners.ListenerNotifier<CollectionEventListener> { |
23 | |
|
24 | |
public Collection what; |
25 | |
public Collection where; |
26 | |
|
27 | 0 | public ListenerNotifier() { |
28 | 0 | } |
29 | |
|
30 | |
@Override |
31 | |
public Object getEvent() { |
32 | 0 | return what; |
33 | |
} |
34 | |
|
35 | |
@Override |
36 | |
public abstract void notify(CollectionEventListener listener); |
37 | |
|
38 | |
} |
39 | |
|
40 | 0 | private ListenerNotifier preAddNotifier = new ListenerNotifier() { |
41 | |
|
42 | |
@Override |
43 | |
public void notify(CollectionEventListener listener) { |
44 | 0 | listener.preAddEvent(what, where); |
45 | 0 | } |
46 | |
|
47 | |
}; |
48 | |
|
49 | 0 | private ListenerNotifier postAddNotifier = new ListenerNotifier() { |
50 | |
|
51 | |
@Override |
52 | |
public void notify(CollectionEventListener listener) { |
53 | 0 | listener.postAddEvent(what, where); |
54 | 0 | } |
55 | |
|
56 | |
}; |
57 | |
|
58 | 0 | private ListenerNotifier preRemoveNotifier = new ListenerNotifier() { |
59 | |
|
60 | |
@Override |
61 | |
public void notify(CollectionEventListener listener) { |
62 | 0 | listener.preRemoveEvent(what, where); |
63 | 0 | } |
64 | |
|
65 | |
}; |
66 | |
|
67 | 0 | private ListenerNotifier postRemoveNotifier = new ListenerNotifier() { |
68 | |
|
69 | |
@Override |
70 | |
public void notify(CollectionEventListener listener) { |
71 | 0 | listener.postRemoveEvent(what, where); |
72 | 0 | } |
73 | |
|
74 | |
}; |
75 | |
|
76 | |
public void addCollectionListener(CollectionEventListener listener) { |
77 | 0 | eventListeners.addStrongListener(listener); |
78 | 0 | } |
79 | |
|
80 | |
public boolean removeCollectionListener(CollectionEventListener listener) { |
81 | 0 | return eventListeners.removeListener(listener) > 0; |
82 | |
} |
83 | |
|
84 | 0 | public ObservableCollection(Collection c) { |
85 | 0 | col = c; |
86 | 0 | } |
87 | |
|
88 | |
@Override |
89 | |
public int size() { |
90 | 0 | return col.size(); |
91 | |
} |
92 | |
|
93 | |
@Override |
94 | |
public boolean isEmpty() { |
95 | 0 | return col.isEmpty(); |
96 | |
} |
97 | |
|
98 | |
@Override |
99 | |
public boolean contains(Object o) { |
100 | 0 | return col.contains(o); |
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public Iterator<E> iterator() { |
105 | 0 | return col.iterator(); |
106 | |
} |
107 | |
|
108 | |
@Override |
109 | |
public Object[] toArray() { |
110 | 0 | return col.toArray(); |
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public <T> T[] toArray(T[] a) { |
115 | 0 | return col.toArray(a); |
116 | |
} |
117 | |
|
118 | |
@Override |
119 | |
public boolean add(E e) { |
120 | 0 | Collection add = Collections.singletonList(e); |
121 | 0 | notifyPreAdd(add); |
122 | 0 | boolean ret = col.add(e); |
123 | 0 | notifyPostAdd(add); |
124 | 0 | return ret; |
125 | |
} |
126 | |
|
127 | |
@Override |
128 | |
public boolean remove(Object o) { |
129 | 0 | Collection rem = Collections.singletonList(o); |
130 | 0 | notifyPreRemove(rem); |
131 | 0 | boolean ret = col.remove(o); |
132 | 0 | notifyPostRemove(rem); |
133 | 0 | return ret; |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public boolean containsAll(Collection<?> c) { |
138 | 0 | return col.containsAll(c); |
139 | |
} |
140 | |
|
141 | |
@Override |
142 | |
public boolean addAll(Collection<? extends E> c) { |
143 | 0 | notifyPreAdd(c); |
144 | 0 | boolean ret = col.addAll(c); |
145 | 0 | notifyPostAdd(c); |
146 | 0 | return ret; |
147 | |
|
148 | |
} |
149 | |
|
150 | |
@Override |
151 | |
public boolean removeAll(Collection<?> c) { |
152 | 0 | notifyPreRemove(c); |
153 | 0 | boolean ret = col.removeAll(c); |
154 | 0 | notifyPostRemove(c); |
155 | 0 | return ret; |
156 | |
} |
157 | |
|
158 | |
@Override |
159 | |
public boolean retainAll(Collection<?> c) { |
160 | |
|
161 | 0 | Collection<Object> toRemove = new LinkedList<Object>(); |
162 | 0 | for (Object o : col) { |
163 | 0 | if (!c.contains(o)) { |
164 | 0 | toRemove.add(o); |
165 | |
} |
166 | |
} |
167 | 0 | notifyPreRemove(toRemove); |
168 | 0 | boolean ret = col.retainAll(c); |
169 | 0 | notifyPostRemove(toRemove); |
170 | |
|
171 | 0 | return ret; |
172 | |
} |
173 | |
|
174 | |
@Override |
175 | |
public void clear() { |
176 | 0 | Collection<Object> copy = new ArrayList<Object>(col); |
177 | 0 | notifyPreRemove(copy); |
178 | 0 | col.clear(); |
179 | 0 | notifyPostRemove(copy); |
180 | 0 | } |
181 | |
|
182 | |
protected synchronized void notifyPreAdd(Collection<? extends E> add) { |
183 | 0 | preAddNotifier.what = add; |
184 | 0 | preAddNotifier.where = this; |
185 | 0 | eventListeners.notify(preAddNotifier); |
186 | 0 | } |
187 | |
|
188 | |
protected synchronized void notifyPostAdd(Collection<? extends E> add) { |
189 | 0 | postAddNotifier.what = add; |
190 | 0 | postAddNotifier.where = this; |
191 | 0 | eventListeners.notify(postAddNotifier); |
192 | 0 | } |
193 | |
|
194 | |
protected synchronized void notifyPreRemove(Collection<?> remove) { |
195 | 0 | preRemoveNotifier.what = remove; |
196 | 0 | preRemoveNotifier.where = this; |
197 | 0 | eventListeners.notify(preRemoveNotifier); |
198 | 0 | } |
199 | |
|
200 | |
protected synchronized void notifyPostRemove(Collection<?> remove) { |
201 | 0 | postRemoveNotifier.what = remove; |
202 | 0 | postRemoveNotifier.where = this; |
203 | 0 | eventListeners.notify(postRemoveNotifier); |
204 | 0 | } |
205 | |
} |