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