1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.tags;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.text.MessageFormat;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
13 | import org.openstreetmap.josm.command.Command;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
16 | import org.openstreetmap.josm.data.osm.TagCollection;
|
---|
17 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
18 | /**
|
---|
19 | * Represents a decision for a conflict due to multiple possible value for a tag.
|
---|
20 | *
|
---|
21 | *
|
---|
22 | */
|
---|
23 | public class MultiValueResolutionDecision {
|
---|
24 |
|
---|
25 | /** the type of decision */
|
---|
26 | private MultiValueDecisionType type;
|
---|
27 | /** the collection of tags for which a decision is needed */
|
---|
28 | private TagCollection tags;
|
---|
29 | /** the selected value if {@see #type} is {@see MultiValueDecisionType#KEEP_ONE} */
|
---|
30 | private String value;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * constuctor
|
---|
34 | */
|
---|
35 | public MultiValueResolutionDecision() {
|
---|
36 | type = MultiValueDecisionType.UNDECIDED;
|
---|
37 | tags = new TagCollection();
|
---|
38 | autoDecide();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Creates a new decision for the tag collection <code>tags</code>.
|
---|
43 | * All tags must have the same key.
|
---|
44 | *
|
---|
45 | * @param tags the tags. Must not be null.
|
---|
46 | * @exception IllegalArgumentException thrown if tags is null
|
---|
47 | * @exception IllegalArgumentException thrown if there are more than one keys
|
---|
48 | * @exception IllegalArgumentException thrown if tags is empty
|
---|
49 | */
|
---|
50 | public MultiValueResolutionDecision(TagCollection tags) throws IllegalArgumentException {
|
---|
51 | CheckParameterUtil.ensureParameterNotNull(tags, "tags");
|
---|
52 | if (tags.isEmpty())
|
---|
53 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be empty.", "tags"));
|
---|
54 | if (tags.getKeys().size() != 1)
|
---|
55 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' with tags for exactly one key expected. Got {1}.", "tags", tags.getKeys().size()));
|
---|
56 | this.tags = tags;
|
---|
57 | autoDecide();
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Tries to find the best decision based on the current values.
|
---|
62 | */
|
---|
63 | protected void autoDecide() {
|
---|
64 | this.type = MultiValueDecisionType.UNDECIDED;
|
---|
65 | // exactly one empty value ? -> delete the tag
|
---|
66 | if (tags.size() == 1 && tags.getValues().contains("")) {
|
---|
67 | this.type = MultiValueDecisionType.KEEP_NONE;
|
---|
68 |
|
---|
69 | // exactly one non empty value? -> keep this value
|
---|
70 | } else if (tags.size() == 1) {
|
---|
71 | this.type = MultiValueDecisionType.KEEP_ONE;
|
---|
72 | this.value = tags.getValues().iterator().next();
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Apply the decision to keep no value
|
---|
78 | */
|
---|
79 | public void keepNone() {
|
---|
80 | this.type = MultiValueDecisionType.KEEP_NONE;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Apply the decision to keep all values
|
---|
85 | */
|
---|
86 | public void keepAll() {
|
---|
87 | this.type = MultiValueDecisionType.KEEP_ALL;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Apply the decision to keep exactly one value
|
---|
92 | *
|
---|
93 | * @param value the value to keep
|
---|
94 | * @throws IllegalArgumentException thrown if value is null
|
---|
95 | * @throws IllegalStateException thrown if value is not in the list of known values for this tag
|
---|
96 | */
|
---|
97 | public void keepOne(String value) throws IllegalArgumentException, IllegalStateException {
|
---|
98 | CheckParameterUtil.ensureParameterNotNull(value, "value");
|
---|
99 | if (!tags.getValues().contains(value))
|
---|
100 | throw new IllegalStateException(tr("Tag collection does not include the selected value ''{0}''.", value));
|
---|
101 | this.value = value;
|
---|
102 | this.type = MultiValueDecisionType.KEEP_ONE;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * sets a new value for this
|
---|
107 | *
|
---|
108 | * @param value the new vlaue
|
---|
109 | */
|
---|
110 | public void setNew(String value) {
|
---|
111 | if (value == null) {
|
---|
112 | value = "";
|
---|
113 | }
|
---|
114 | this.value = value;
|
---|
115 | this.type = MultiValueDecisionType.KEEP_ONE;
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * marks this as undecided
|
---|
121 | *
|
---|
122 | */
|
---|
123 | public void undecide() {
|
---|
124 | this.type = MultiValueDecisionType.UNDECIDED;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Replies the chosen value
|
---|
129 | *
|
---|
130 | * @return the chosen value
|
---|
131 | * @throws IllegalStateException thrown if this resolution is not yet decided
|
---|
132 | */
|
---|
133 | public String getChosenValue() throws IllegalStateException {
|
---|
134 | switch(type) {
|
---|
135 | case UNDECIDED: throw new IllegalStateException(tr("Not decided yet."));
|
---|
136 | case KEEP_ONE: return value;
|
---|
137 | case KEEP_NONE: return null;
|
---|
138 | case KEEP_ALL: return tags.getJoinedValues(getKey());
|
---|
139 | }
|
---|
140 | // should not happen
|
---|
141 | return null;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Replies the list of possible, non empty values
|
---|
146 | *
|
---|
147 | * @return the list of possible, non empty values
|
---|
148 | */
|
---|
149 | public List<String> getValues() {
|
---|
150 | ArrayList<String> ret = new ArrayList<String>(tags.getValues());
|
---|
151 | ret.remove("");
|
---|
152 | ret.remove(null);
|
---|
153 | Collections.sort(ret);
|
---|
154 | return ret;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Replies the key of the tag to be resolved by this resolution
|
---|
159 | *
|
---|
160 | * @return the key of the tag to be resolved by this resolution
|
---|
161 | */
|
---|
162 | public String getKey() {
|
---|
163 | return tags.getKeys().iterator().next();
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Replies true if the empty value is a possible value in this resolution
|
---|
168 | *
|
---|
169 | * @return true if the empty value is a possible value in this resolution
|
---|
170 | */
|
---|
171 | public boolean canKeepNone() {
|
---|
172 | return tags.getValues().contains("");
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Replies true, if this resolution has more than 1 possible non-empty values
|
---|
177 | *
|
---|
178 | * @return true, if this resolution has more than 1 possible non-empty values
|
---|
179 | */
|
---|
180 | public boolean canKeepAll() {
|
---|
181 | return getValues().size() > 1;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Replies true if this resolution is decided
|
---|
186 | *
|
---|
187 | * @return true if this resolution is decided
|
---|
188 | */
|
---|
189 | public boolean isDecided() {
|
---|
190 | return !type.equals(MultiValueDecisionType.UNDECIDED);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Replies the type of the resolution
|
---|
195 | *
|
---|
196 | * @return the type of the resolution
|
---|
197 | */
|
---|
198 | public MultiValueDecisionType getDecisionType() {
|
---|
199 | return type;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Applies the resolution to an {@see OsmPrimitive}
|
---|
204 | *
|
---|
205 | * @param primitive the primitive
|
---|
206 | * @throws IllegalStateException thrown if this resolution is not resolved yet
|
---|
207 | *
|
---|
208 | */
|
---|
209 | public void applyTo(OsmPrimitive primitive) throws IllegalStateException{
|
---|
210 | if (primitive == null) return;
|
---|
211 | if (!isDecided())
|
---|
212 | throw new IllegalStateException(tr("Not decided yet."));
|
---|
213 | String key = tags.getKeys().iterator().next();
|
---|
214 | String value = getChosenValue();
|
---|
215 | if (type.equals(MultiValueDecisionType.KEEP_NONE)) {
|
---|
216 | primitive.remove(key);
|
---|
217 | } else {
|
---|
218 | primitive.put(key, value);
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Applies this resolution to a collection of primitives
|
---|
224 | *
|
---|
225 | * @param primitives the collection of primitives
|
---|
226 | * @throws IllegalStateException thrown if this resolution is not resolved yet
|
---|
227 | */
|
---|
228 | public void applyTo(Collection<? extends OsmPrimitive> primitives) throws IllegalStateException {
|
---|
229 | if (primitives == null) return;
|
---|
230 | for (OsmPrimitive primitive: primitives) {
|
---|
231 | if (primitive == null) {
|
---|
232 | continue;
|
---|
233 | }
|
---|
234 | applyTo(primitive);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Builds a change command for applying this resolution to a primitive
|
---|
240 | *
|
---|
241 | * @param primitive the primitive
|
---|
242 | * @return the change command
|
---|
243 | * @throws IllegalArgumentException thrown if primitive is null
|
---|
244 | * @throws IllegalStateException thrown if this resolution is not resolved yet
|
---|
245 | */
|
---|
246 | public Command buildChangeCommand(OsmPrimitive primitive) throws IllegalArgumentException, IllegalStateException {
|
---|
247 | CheckParameterUtil.ensureParameterNotNull(primitive, "primitive");
|
---|
248 | if (!isDecided())
|
---|
249 | throw new IllegalStateException(tr("Not decided yet."));
|
---|
250 | String key = tags.getKeys().iterator().next();
|
---|
251 | String value = getChosenValue();
|
---|
252 | ChangePropertyCommand cmd = new ChangePropertyCommand(primitive, key,value);
|
---|
253 | return cmd;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Builds a change command for applying this resolution to a collection of primitives
|
---|
258 | *
|
---|
259 | * @param primitives the collection of primitives
|
---|
260 | * @return the change command
|
---|
261 | * @throws IllegalArgumentException thrown if primitives is null
|
---|
262 | * @throws IllegalStateException thrown if this resolution is not resolved yet
|
---|
263 | */
|
---|
264 | public Command buildChangeCommand(Collection<? extends OsmPrimitive> primitives) {
|
---|
265 | CheckParameterUtil.ensureParameterNotNull(primitives, "primitives");
|
---|
266 | if (!isDecided())
|
---|
267 | throw new IllegalStateException(tr("Not decided yet."));
|
---|
268 | String key = tags.getKeys().iterator().next();
|
---|
269 | String value = getChosenValue();
|
---|
270 | ChangePropertyCommand cmd = new ChangePropertyCommand(primitives, key,value);
|
---|
271 | return cmd;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Replies a tag representing the current resolution. Null, if this resolution is not resolved
|
---|
276 | * yet.
|
---|
277 | *
|
---|
278 | * @return a tag representing the current resolution. Null, if this resolution is not resolved
|
---|
279 | * yet
|
---|
280 | */
|
---|
281 | public Tag getResolution() {
|
---|
282 | switch(type) {
|
---|
283 | case KEEP_ALL: return new Tag(getKey(), tags.getJoinedValues(getKey()));
|
---|
284 | case KEEP_ONE: return new Tag(getKey(),value);
|
---|
285 | case KEEP_NONE: return new Tag(getKey(), "");
|
---|
286 | case UNDECIDED: return null;
|
---|
287 | }
|
---|
288 | return null;
|
---|
289 | }
|
---|
290 | }
|
---|