001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.conflict; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.util.ArrayList; 007import java.util.Collection; 008import java.util.Iterator; 009import java.util.List; 010import java.util.Objects; 011import java.util.Set; 012import java.util.concurrent.CopyOnWriteArrayList; 013import java.util.function.Predicate; 014import java.util.stream.Collectors; 015 016import org.openstreetmap.josm.data.osm.Node; 017import org.openstreetmap.josm.data.osm.OsmPrimitive; 018import org.openstreetmap.josm.data.osm.Relation; 019import org.openstreetmap.josm.data.osm.Way; 020import org.openstreetmap.josm.tools.CheckParameterUtil; 021import org.openstreetmap.josm.tools.SubclassFilteredCollection; 022 023/** 024 * This is a collection of {@link Conflict}s. This collection is {@link Iterable}, i.e. 025 * it can be used in <code>for</code>-loops as follows: 026 * <pre> 027 * ConflictCollection conflictCollection = .... 028 * 029 * for (Conflict c : conflictCollection) { 030 * // do something 031 * } 032 * </pre> 033 * 034 * This collection emits an event when the content of the collection changes. You can register 035 * and unregister for these events using: 036 * <ul> 037 * <li>{@link #addConflictListener(IConflictListener)}</li> 038 * <li>{@link #removeConflictListener(IConflictListener)}</li> 039 * </ul> 040 */ 041public class ConflictCollection implements Iterable<Conflict<? extends OsmPrimitive>> { 042 private final List<Conflict<? extends OsmPrimitive>> conflicts; 043 private final CopyOnWriteArrayList<IConflictListener> listeners; 044 045 /** 046 * Constructs a new {@code ConflictCollection}. 047 */ 048 public ConflictCollection() { 049 conflicts = new ArrayList<>(); 050 listeners = new CopyOnWriteArrayList<>(); 051 } 052 053 /** 054 * Adds the specified conflict listener, if not already present. 055 * @param listener The conflict listener to add 056 */ 057 public void addConflictListener(IConflictListener listener) { 058 if (listener != null) { 059 listeners.addIfAbsent(listener); 060 } 061 } 062 063 /** 064 * Removes the specified conflict listener. 065 * @param listener The conflict listener to remove 066 */ 067 public void removeConflictListener(IConflictListener listener) { 068 listeners.remove(listener); 069 } 070 071 protected void fireConflictAdded() { 072 listeners.forEach(listener -> listener.onConflictsAdded(this)); 073 } 074 075 protected void fireConflictRemoved() { 076 listeners.forEach(listener -> listener.onConflictsRemoved(this)); 077 } 078 079 /** 080 * Adds a conflict to the collection 081 * 082 * @param conflict the conflict 083 * @throws IllegalStateException if this collection already includes a conflict for conflict.getMy() 084 */ 085 protected void addConflict(Conflict<?> conflict) { 086 if (hasConflictForMy(conflict.getMy())) 087 throw new IllegalStateException(tr("Already registered a conflict for primitive ''{0}''.", conflict.getMy().toString())); 088 if (!conflicts.contains(conflict)) { 089 conflicts.add(conflict); 090 } 091 } 092 093 /** 094 * Adds a conflict to the collection of conflicts. 095 * 096 * @param conflict the conflict to add. Must not be null. 097 * @throws IllegalArgumentException if conflict is null 098 * @throws IllegalStateException if this collection already includes a conflict for conflict.getMy() 099 */ 100 public void add(Conflict<?> conflict) { 101 CheckParameterUtil.ensureParameterNotNull(conflict, "conflict"); 102 addConflict(conflict); 103 fireConflictAdded(); 104 } 105 106 /** 107 * Add the conflicts in <code>otherConflicts</code> to this collection of conflicts 108 * 109 * @param otherConflicts the collection of conflicts. Does nothing is conflicts is null. 110 */ 111 public void add(Collection<Conflict<?>> otherConflicts) { 112 if (otherConflicts == null) return; 113 otherConflicts.forEach(this::addConflict); 114 fireConflictAdded(); 115 } 116 117 /** 118 * Adds a conflict for the pair of {@link OsmPrimitive}s given by <code>my</code> and 119 * <code>their</code>. 120 * 121 * @param my my primitive 122 * @param their their primitive 123 */ 124 public void add(OsmPrimitive my, OsmPrimitive their) { 125 addConflict(new Conflict<>(my, their)); 126 fireConflictAdded(); 127 } 128 129 /** 130 * removes a conflict from this collection 131 * 132 * @param conflict the conflict 133 */ 134 public void remove(Conflict<?> conflict) { 135 conflicts.remove(conflict); 136 fireConflictRemoved(); 137 } 138 139 /** 140 * Replies the conflict for the {@link OsmPrimitive} <code>my</code>, null 141 * if no such conflict exists. 142 * 143 * @param my my primitive 144 * @return the conflict for the {@link OsmPrimitive} <code>my</code>, null 145 * if no such conflict exists. 146 */ 147 public Conflict<?> getConflictForMy(OsmPrimitive my) { 148 if (conflicts.isEmpty()) { 149 return null; 150 } 151 return conflicts.stream() 152 .filter(c -> c.isMatchingMy(my)) 153 .findFirst() 154 .orElse(null); 155 } 156 157 /** 158 * Replies the conflict for the {@link OsmPrimitive} <code>their</code>, null 159 * if no such conflict exists. 160 * 161 * @param their their primitive 162 * @return the conflict for the {@link OsmPrimitive} <code>their</code>, null 163 * if no such conflict exists. 164 */ 165 public Conflict<?> getConflictForTheir(OsmPrimitive their) { 166 if (conflicts.isEmpty()) { 167 return null; 168 } 169 return conflicts.stream() 170 .filter(c -> c.isMatchingTheir(their)) 171 .findFirst() 172 .orElse(null); 173 } 174 175 /** 176 * Replies true, if this collection includes a conflict for <code>my</code>. 177 * 178 * @param my my primitive 179 * @return true, if this collection includes a conflict for <code>my</code>; false, otherwise 180 */ 181 public boolean hasConflictForMy(OsmPrimitive my) { 182 return getConflictForMy(my) != null; 183 } 184 185 /** 186 * Replies true, if this collection includes a given conflict 187 * 188 * @param c the conflict 189 * @return true, if this collection includes the conflict; false, otherwise 190 */ 191 public boolean hasConflict(Conflict<?> c) { 192 return hasConflictForMy(c.getMy()); 193 } 194 195 /** 196 * Replies true, if this collection includes a conflict for <code>their</code>. 197 * 198 * @param their their primitive 199 * @return true, if this collection includes a conflict for <code>their</code>; false, otherwise 200 */ 201 public boolean hasConflictForTheir(OsmPrimitive their) { 202 return getConflictForTheir(their) != null; 203 } 204 205 /** 206 * Removes any conflicts for the {@link OsmPrimitive} <code>my</code>. 207 * 208 * @param my the primitive 209 */ 210 public void removeForMy(OsmPrimitive my) { 211 if (conflicts.removeIf(c -> c.isMatchingMy(my))) { 212 fireConflictRemoved(); 213 } 214 } 215 216 /** 217 * Removes any conflicts for the {@link OsmPrimitive} <code>their</code>. 218 * 219 * @param their the primitive 220 */ 221 public void removeForTheir(OsmPrimitive their) { 222 if (conflicts.removeIf(c -> c.isMatchingTheir(their))) { 223 fireConflictRemoved(); 224 } 225 } 226 227 /** 228 * Replies the conflicts as list. 229 * 230 * @return the list of conflicts 231 */ 232 public List<Conflict<?>> get() { 233 return conflicts; 234 } 235 236 /** 237 * Replies the size of the collection 238 * 239 * @return the size of the collection 240 */ 241 public int size() { 242 return conflicts.size(); 243 } 244 245 /** 246 * Replies the conflict at position <code>idx</code> 247 * 248 * @param idx the index 249 * @return the conflict at position <code>idx</code> 250 */ 251 public Conflict<?> get(int idx) { 252 return conflicts.get(idx); 253 } 254 255 /** 256 * Replies the iterator for this collection. 257 * 258 * @return the iterator 259 */ 260 @Override 261 public Iterator<Conflict<?>> iterator() { 262 return conflicts.iterator(); 263 } 264 265 /** 266 * Adds all conflicts from another collection. 267 * @param other The other collection of conflicts to add 268 */ 269 public void add(ConflictCollection other) { 270 other.conflicts.stream() 271 .filter(c -> !hasConflict(c)) 272 .forEach(this::add); 273 } 274 275 /** 276 * Replies the set of {@link OsmPrimitive} which participate in the role 277 * of "my" in the conflicts managed by this collection. 278 * 279 * @return the set of {@link OsmPrimitive} which participate in the role 280 * of "my" in the conflicts managed by this collection. 281 */ 282 public Set<OsmPrimitive> getMyConflictParties() { 283 return conflicts.stream() 284 .map(Conflict::getMy) 285 .collect(Collectors.toSet()); 286 } 287 288 /** 289 * Replies the set of {@link OsmPrimitive} which participate in the role 290 * of "their" in the conflicts managed by this collection. 291 * 292 * @return the set of {@link OsmPrimitive} which participate in the role 293 * of "their" in the conflicts managed by this collection. 294 */ 295 public Set<OsmPrimitive> getTheirConflictParties() { 296 return conflicts.stream() 297 .map(Conflict::getTheir) 298 .collect(Collectors.toSet()); 299 } 300 301 /** 302 * Replies true if this collection is empty 303 * 304 * @return true, if this collection is empty; false, otherwise 305 */ 306 public boolean isEmpty() { 307 return size() == 0; 308 } 309 310 @Override 311 public String toString() { 312 return conflicts.toString(); 313 } 314 315 /** 316 * Returns the list of conflicts involving nodes. 317 * @return The list of conflicts involving nodes. 318 * @since 6555 319 */ 320 public final Collection<Conflict<? extends OsmPrimitive>> getNodeConflicts() { 321 return SubclassFilteredCollection.filter(conflicts, c -> c != null && c.getMy() instanceof Node); 322 } 323 324 /** 325 * Returns the list of conflicts involving ways. 326 * @return The list of conflicts involving ways. 327 * @since 6555 328 */ 329 public final Collection<Conflict<? extends OsmPrimitive>> getWayConflicts() { 330 return SubclassFilteredCollection.filter(conflicts, c -> c != null && c.getMy() instanceof Way); 331 } 332 333 /** 334 * Returns the list of conflicts involving relations. 335 * @return The list of conflicts involving relations. 336 * @since 6555 337 */ 338 public final Collection<Conflict<? extends OsmPrimitive>> getRelationConflicts() { 339 return SubclassFilteredCollection.filter(conflicts, c -> c != null && c.getMy() instanceof Relation); 340 } 341 342 /** 343 * Returns the number of conflicts involving nodes. 344 * @return The number of conflicts involving nodes. 345 * @since 17524 346 */ 347 public final long getNumberOfNodeConflicts() { 348 return getNumberOfConflicts(c -> c.getMy() instanceof Node); 349 } 350 351 /** 352 * Returns the number of conflicts involving nodes. 353 * @return The number of conflicts involving nodes. 354 * @since 17524 355 */ 356 public final long getNumberOfWayConflicts() { 357 return getNumberOfConflicts(c -> c.getMy() instanceof Way); 358 } 359 360 /** 361 * Returns the number of conflicts involving nodes. 362 * @return The number of conflicts involving nodes. 363 * @since 17524 364 */ 365 public final long getNumberOfRelationConflicts() { 366 return getNumberOfConflicts(c -> c.getMy() instanceof Relation); 367 } 368 369 private long getNumberOfConflicts(Predicate<Conflict<?>> predicate) { 370 return conflicts.isEmpty() ? 0 : conflicts.stream().filter(Objects::nonNull).filter(predicate).count(); 371 } 372 373 @Override 374 public int hashCode() { 375 return Objects.hash(conflicts, listeners); 376 } 377 378 @Override 379 public boolean equals(Object obj) { 380 if (this == obj) return true; 381 if (obj == null || getClass() != obj.getClass()) return false; 382 ConflictCollection conflicts1 = (ConflictCollection) obj; 383 return Objects.equals(conflicts, conflicts1.conflicts) && 384 Objects.equals(listeners, conflicts1.listeners); 385 } 386}