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