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