1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.pair.tags;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
7 | import org.openstreetmap.josm.gui.conflict.pair.MergeDecisionType;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * TagMergeItem represents an individual merge action for a specific pair of key/value.
|
---|
11 | *
|
---|
12 | * A TagMergeItem manages the values of the two key/value-pairs and keeps track of the applied
|
---|
13 | * merge decision.
|
---|
14 | *
|
---|
15 | */
|
---|
16 | public class TagMergeItem {
|
---|
17 |
|
---|
18 | private String key = null;
|
---|
19 | private String myTagValue = null;
|
---|
20 | private String theirTagValue = null;
|
---|
21 | private MergeDecisionType mergeDecision = MergeDecisionType.UNDECIDED;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * constructor
|
---|
25 | *
|
---|
26 | * @param key the common tag key. Must not be null.
|
---|
27 | * @param myTagValue the value for this key known in the local dataset
|
---|
28 | * @param theirTagValue the value for this key known in the dataset on the server
|
---|
29 | * @throws IllegalArgumentException if key is null
|
---|
30 | */
|
---|
31 | public TagMergeItem(String key, String myTagValue, String theirTagValue) {
|
---|
32 | if (key == null) {
|
---|
33 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "key"));
|
---|
34 | }
|
---|
35 | this.key = key;
|
---|
36 | this.myTagValue = myTagValue;
|
---|
37 | this.theirTagValue = theirTagValue;
|
---|
38 | this.mergeDecision = MergeDecisionType.UNDECIDED;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * constructor
|
---|
43 | *
|
---|
44 | * @param key the tag key common to the merged OSM primitives. Must not be null.
|
---|
45 | * @param my my version of the OSM primitive (i.e. the version known in the local dataset). Must not be null.
|
---|
46 | * @param their their version of the OSM primitive (i.e. the version known on the server). Must not be null.
|
---|
47 | * @throws IllegalArgumentException thrown if key is null
|
---|
48 | * @throws IllegalArgumentException thrown if my is null
|
---|
49 | * @throws IllegalArgumentException thrown if their is null
|
---|
50 | */
|
---|
51 | public TagMergeItem(String key, OsmPrimitive my, OsmPrimitive their) {
|
---|
52 | if (key == null) throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "key"));
|
---|
53 | if (my == null) throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "my"));
|
---|
54 | if (their == null) throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "their"));
|
---|
55 | this.key = key;
|
---|
56 | myTagValue = my.get(key);
|
---|
57 | theirTagValue = their.get(key);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * applies a merge decision to this merge item
|
---|
62 | *
|
---|
63 | * @param decision the merge decision. Must not be null.
|
---|
64 | * @exception IllegalArgumentException thrown if decision is null
|
---|
65 | *
|
---|
66 | */
|
---|
67 | public void decide(MergeDecisionType decision) throws IllegalArgumentException {
|
---|
68 | if (decision == null) throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "decision"));
|
---|
69 | this.mergeDecision = decision;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public String getKey() {
|
---|
73 | return key;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public String getMyTagValue() {
|
---|
77 | return myTagValue;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public String getTheirTagValue() {
|
---|
81 | return theirTagValue;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public MergeDecisionType getMergeDecision() {
|
---|
85 | return mergeDecision;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * applies the current merge decisions to the tag set of an OSM primitive. The
|
---|
90 | * OSM primitive has the role of primitive in the local dataset ('my' primitive,
|
---|
91 | * not 'their' primitive)
|
---|
92 | *
|
---|
93 | * @param primitive the OSM primitive. Must not be null.
|
---|
94 | * @exception IllegalArgumentException thrown, if primitive is null
|
---|
95 | * @exception IllegalStateException thrown, if this merge item is undecided
|
---|
96 | */
|
---|
97 | public void applyToMyPrimitive(OsmPrimitive primitive) throws IllegalArgumentException, IllegalStateException {
|
---|
98 | if (primitive == null) throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "primitive"));
|
---|
99 | if (mergeDecision == MergeDecisionType.UNDECIDED) {
|
---|
100 | throw new IllegalStateException(tr("Cannot apply undecided tag merge item."));
|
---|
101 | } else if (mergeDecision == MergeDecisionType.KEEP_THEIR) {
|
---|
102 | if (theirTagValue == null) {
|
---|
103 | primitive.remove(key);
|
---|
104 | } else if (theirTagValue != null) {
|
---|
105 | primitive.put(key, theirTagValue);
|
---|
106 | }
|
---|
107 | } else if (mergeDecision == MergeDecisionType.KEEP_MINE) {
|
---|
108 | if (myTagValue == null) {
|
---|
109 | primitive.remove(key);
|
---|
110 | } else if (myTagValue != null) {
|
---|
111 | primitive.put(key, myTagValue);
|
---|
112 | }
|
---|
113 | } else {
|
---|
114 | // should not happen
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|