| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.data.conflict; |
|---|
| 3 | |
|---|
| 4 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
|---|
| 5 | import org.openstreetmap.josm.data.osm.PrimitiveId; |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * Represents a conflict between two {@see OsmPrimitive}s. It is represented as |
|---|
| 9 | * a pair of {@see OsmPrimitive}s where one element of the pair has the role <em>my</em> |
|---|
| 10 | * and the other has the role <em>their</em>. |
|---|
| 11 | * <ul> |
|---|
| 12 | * <li><code>my</code> is the {@see OsmPrimitive} in the local dataset</li> |
|---|
| 13 | * <li><code>their</code> is the {@see OsmPrimitive} which caused the conflict when it |
|---|
| 14 | * it was tried to merge it onto <code>my</code>. <code>their</code> is usually the |
|---|
| 15 | * {@see OsmPrimitive} from the dataset in another layer or the one retrieved from the server.</li> |
|---|
| 16 | * </ul> |
|---|
| 17 | * |
|---|
| 18 | * |
|---|
| 19 | */ |
|---|
| 20 | public class Conflict<T extends OsmPrimitive> { |
|---|
| 21 | private final T my; |
|---|
| 22 | private final T their; |
|---|
| 23 | private final boolean isMyDeleted; |
|---|
| 24 | |
|---|
| 25 | public Conflict(T my, T their) { |
|---|
| 26 | this(my, their, false); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | public Conflict(T my, T their, boolean isMyDeleted) { |
|---|
| 30 | this.my = my; |
|---|
| 31 | this.their = their; |
|---|
| 32 | this.isMyDeleted = isMyDeleted; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | public T getMy() { |
|---|
| 36 | return my; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | public T getTheir() { |
|---|
| 40 | return their; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public boolean isMatchingMy(OsmPrimitive my) { |
|---|
| 44 | return this.my == my; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public boolean isMatchingTheir(OsmPrimitive their) { |
|---|
| 48 | return this.their == their; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Replies true if the primitive <code>primitive</code> is participating |
|---|
| 53 | * in this conflict |
|---|
| 54 | * |
|---|
| 55 | * @param primitive the primitive |
|---|
| 56 | * @return true if the primitive <code>primitive</code> is participating |
|---|
| 57 | * in this conflict |
|---|
| 58 | */ |
|---|
| 59 | public boolean isParticipating(OsmPrimitive primitive) { |
|---|
| 60 | if (primitive == null) return false; |
|---|
| 61 | return primitive.getPrimitiveId().equals(my.getPrimitiveId()) |
|---|
| 62 | || primitive.getPrimitiveId().equals(their.getPrimitiveId()); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Replies true if the primitive with id <code>id</code> is participating |
|---|
| 67 | * in this conflict |
|---|
| 68 | * |
|---|
| 69 | * @param id the primitive id |
|---|
| 70 | * @return true if the primitive <code>primitive</code> is participating |
|---|
| 71 | * in this conflict |
|---|
| 72 | */ |
|---|
| 73 | public boolean isParticipating(PrimitiveId id) { |
|---|
| 74 | if (id == null) return false; |
|---|
| 75 | return id.equals(my.getPrimitiveId()) |
|---|
| 76 | || id.equals(their.getPrimitiveId()); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | @Override |
|---|
| 80 | public int hashCode() { |
|---|
| 81 | final int prime = 31; |
|---|
| 82 | int result = 1; |
|---|
| 83 | result = prime * result + ((my == null) ? 0 : my.hashCode()); |
|---|
| 84 | result = prime * result + ((their == null) ? 0 : their.hashCode()); |
|---|
| 85 | return result; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | @SuppressWarnings("unchecked") |
|---|
| 89 | @Override |
|---|
| 90 | public boolean equals(Object obj) { |
|---|
| 91 | if (this == obj) |
|---|
| 92 | return true; |
|---|
| 93 | if (obj == null) |
|---|
| 94 | return false; |
|---|
| 95 | if (getClass() != obj.getClass()) |
|---|
| 96 | return false; |
|---|
| 97 | Conflict<T> other = (Conflict<T>) obj; |
|---|
| 98 | if (my != other.my) |
|---|
| 99 | return false; |
|---|
| 100 | if(their != other.their) |
|---|
| 101 | return false; |
|---|
| 102 | return true; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * |
|---|
| 107 | * @return True if my primitive was deleted but it has set non deleted status because it's referred by another |
|---|
| 108 | * primitive and references to deleted primitives are not allowed. |
|---|
| 109 | */ |
|---|
| 110 | public boolean isMyDeleted() { |
|---|
| 111 | return isMyDeleted; |
|---|
| 112 | } |
|---|
| 113 | } |
|---|