source: josm/trunk/src/org/openstreetmap/josm/data/conflict/ConflictCollection.java@ 2181

Last change on this file since 2181 was 2181, checked in by stoecker, 15 years ago

lots of i18n fixes

File size: 9.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.conflict;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.Iterator;
10import java.util.List;
11import java.util.Set;
12import java.util.concurrent.CopyOnWriteArrayList;
13
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15
16/**
17 * This is a collection of {@see Conflict}s. This collection is {@see Iterable}, i.e.
18 * it can be used in <code>for</code>-loops as follows:
19 * <pre>
20 * ConflictCollection conflictCollection = ....
21 *
22 * for(Conflict c : conflictCollection) {
23 * // do something
24 * }
25 * </pre>
26 *
27 * This collection emits an event when the content of the collection changes. You can register
28 * and unregister for these events using:
29 * <ul>
30 * <li>{@see #addConflictListener(IConflictListener)}</li>
31 * <li>{@see #removeConflictListener(IConflictListener)}</li>
32 * </ul>
33 */
34public class ConflictCollection implements Iterable<Conflict<?>>{
35 private ArrayList<Conflict<?>> conflicts;
36 private CopyOnWriteArrayList<IConflictListener> listeners;
37
38 public ConflictCollection() {
39 conflicts = new ArrayList<Conflict<?>>();
40 listeners = new CopyOnWriteArrayList<IConflictListener>();
41 }
42
43 public void addConflictListener(IConflictListener listener) {
44 if (listener != null && ! listeners.contains(listener)) {
45 listeners.add(listener);
46 }
47 }
48
49 public void removeConflictListener(IConflictListener listener) {
50 if (listener != null) {
51 listeners.remove(listener);
52 }
53 }
54
55 protected void fireConflictAdded() {
56 Iterator<IConflictListener> it = listeners.iterator();
57 while(it.hasNext()) {
58 it.next().onConflictsAdded(this);
59 }
60 }
61
62 protected void fireConflictRemoved() {
63 Iterator<IConflictListener> it = listeners.iterator();
64 while(it.hasNext()) {
65 it.next().onConflictsRemoved(this);
66 }
67 }
68
69 /**
70 * Adds a conflict to the collection
71 *
72 * @param conflict the conflict
73 * @exception IllegalStateException thrown, if this collection already includes a
74 * conflict for conflict.getMy()
75 */
76 protected void addConflict(Conflict<?> conflict) throws IllegalStateException {
77 if (hasConflictForMy(conflict.getMy()))
78 throw new IllegalStateException(tr("Already registered a conflict for primitive ''{0}''.", conflict.getMy().toString()));
79 if (!conflicts.contains(conflict)) {
80 conflicts.add(conflict);
81 }
82 }
83
84 /**
85 * Adds a conflict to the collection of conflicts.
86 *
87 * @param conflict the conflict to to add. Must not be null.
88 * @throws IllegalArgumentException thrown, if conflict is null
89 * @throws IllegalStateException thrown if this collection already includes a conflict for conflict.getMy()
90 *
91 */
92 public void add(Conflict<?> conflict) throws IllegalStateException, IllegalArgumentException {
93 if (conflict == null)
94 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "conflict"));
95 addConflict(conflict);
96 fireConflictAdded();
97 }
98
99 /**
100 * Add the conflicts in <code>otherConflicts</code> to this collection of conflicts
101 *
102 * @param otherConflicts the collection of conflicts. Does nothing is conflicts is null.
103 */
104 public void add(Collection<Conflict<?>> otherConflicts) {
105 if (otherConflicts == null) return;
106 for(Conflict<?> c : otherConflicts) {
107 addConflict(c);
108 }
109 fireConflictAdded();
110 }
111
112 /**
113 * Adds a conflict for the pair of {@see OsmPrimitive}s given by <code>my</code> and
114 * <code>their</code>.
115 *
116 * @param my my primitive
117 * @param their their primitive
118 */
119 public void add(OsmPrimitive my, OsmPrimitive their) {
120 addConflict(new Conflict<OsmPrimitive>(my, their));
121 fireConflictAdded();
122 }
123
124 /**
125 * removes a conflict from this collection
126 *
127 * @param conflict the conflict
128 */
129 public void remove(Conflict<?> conflict) {
130 conflicts.remove(conflict);
131 fireConflictRemoved();
132 }
133
134 /**
135 * removes the conflict registered for {@see OsmPrimitive} <code>my</code> if any
136 *
137 * @param my the primitive
138 */
139 public void remove(OsmPrimitive my) {
140 Iterator<Conflict<?>> it = iterator();
141 while(it.hasNext()) {
142 if (it.next().isMatchingMy(my)) {
143 it.remove();
144 }
145 }
146 fireConflictRemoved();
147 }
148
149 /**
150 * Replies the conflict for the {@see OsmPrimitive} <code>my</code>, null
151 * if no such conflict exists.
152 *
153 * @param my my primitive
154 * @return the conflict for the {@see OsmPrimitive} <code>my</code>, null
155 * if no such conflict exists.
156 */
157 public Conflict<?> getConflictForMy(OsmPrimitive my) {
158 for(Conflict<?> c : conflicts) {
159 if (c.isMatchingMy(my))
160 return c;
161 }
162 return null;
163 }
164 /**
165 * Replies the conflict for the {@see OsmPrimitive} <code>their</code>, null
166 * if no such conflict exists.
167 *
168 * @param my my primitive
169 * @return the conflict for the {@see OsmPrimitive} <code>their</code>, null
170 * if no such conflict exists.
171 */
172 public Conflict<?> getConflictForTheir(OsmPrimitive their) {
173 for(Conflict<?> c : conflicts) {
174 if (c.isMatchingTheir(their))
175 return c;
176 }
177 return null;
178 }
179
180 /**
181 * Replies true, if this collection includes a conflict for <code>my</code>.
182 *
183 * @param my my primitive
184 * @return true, if this collection includes a conflict for <code>my</code>; false, otherwise
185 */
186 public boolean hasConflictForMy(OsmPrimitive my) {
187 return getConflictForMy(my) != null;
188 }
189
190 /**
191 * Replies true, if this collection includes a given conflict
192 *
193 * @param c the conflict
194 * @return true, if this collection includes the conflict; false, otherwise
195 */
196 public boolean hasConflict(Conflict<?> c) {
197 return hasConflictForMy(c.getMy());
198 }
199
200 /**
201 * Replies true, if this collection includes a conflict for <code>their</code>.
202 *
203 * @param their their primitive
204 * @return true, if this collection includes a conflict for <code>their</code>; false, otherwise
205 */
206 public boolean hasConflictForTheir(OsmPrimitive their) {
207 return getConflictForTheir(their) != null;
208 }
209
210 /**
211 * Removes any conflicts for the {@see OsmPrimitive} <code>my</code>.
212 *
213 * @param my the primitive
214 */
215 public void removeForMy(OsmPrimitive my) {
216 Iterator<Conflict<?>> it = iterator();
217 while(it.hasNext()) {
218 if (it.next().isMatchingMy(my)) {
219 it.remove();
220 }
221 }
222 }
223
224 /**
225 * Removes any conflicts for the {@see OsmPrimitive} <code>their</code>.
226 *
227 * @param their the primitive
228 */
229 public void removeForTheir(OsmPrimitive their) {
230 Iterator<Conflict<?>> it = iterator();
231 while(it.hasNext()) {
232 if (it.next().isMatchingTheir(their)) {
233 it.remove();
234 }
235 }
236 }
237
238 /**
239 * Replies the conflicts as list.
240 *
241 * @return the list of conflicts
242 */
243 public List<Conflict<?>> get() {
244 return conflicts;
245 }
246
247 /**
248 * Replies the size of the collection
249 *
250 * @return the size of the collection
251 */
252 public int size() {
253 return conflicts.size();
254 }
255
256 /**
257 * Replies the conflict at position <code>idx</code>
258 *
259 * @param idx the index
260 * @return the conflict at position <code>idx</code>
261 */
262 public Conflict<?> get(int idx) {
263 return conflicts.get(idx);
264 }
265
266 /**
267 * Replies the iterator for this collection.
268 *
269 * @return the iterator
270 */
271 public Iterator<Conflict<?>> iterator() {
272 return conflicts.iterator();
273 }
274
275 public void add(ConflictCollection other) {
276 for (Conflict<?> c : other) {
277 add(c);
278 }
279 }
280
281 /**
282 * Replies the set of {@see OsmPrimitive} which participate in the role
283 * of "my" in the conflicts managed by this collection.
284 *
285 * @return the set of {@see OsmPrimitive} which participate in the role
286 * of "my" in the conflicts managed by this collection.
287 */
288 public Set<OsmPrimitive> getMyConflictParties() {
289 HashSet<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
290 for (Conflict<?> c: conflicts) {
291 ret.add(c.getMy());
292 }
293 return ret;
294 }
295 /**
296 * Replies the set of {@see OsmPrimitive} which participate in the role
297 * of "their" in the conflicts managed by this collection.
298 *
299 * @return the set of {@see OsmPrimitive} which participate in the role
300 * of "their" in the conflicts managed by this collection.
301 */
302 public Set<OsmPrimitive> getTheirConflictParties() {
303 HashSet<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
304 for (Conflict<?> c: conflicts) {
305 ret.add(c.getTheir());
306 }
307 return ret;
308 }
309
310 /**
311 * Replies true if this collection is empty
312 *
313 * @return true, if this collection is empty; false, otherwise
314 */
315 public boolean isEmpty() {
316 return size() == 0;
317 }
318}
Note: See TracBrowser for help on using the repository browser.