1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.upload;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Stack;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * This is an exception that is thrown if the user attempts to upload a list of relations with a cyclic dependency in them
|
---|
15 | */
|
---|
16 | public class CyclicUploadDependencyException extends Exception {
|
---|
17 | private final List<Relation> cycle;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Creates a new {@link CyclicUploadDependencyException}
|
---|
21 | * @param cycle The cycle that was found
|
---|
22 | */
|
---|
23 | public CyclicUploadDependencyException(Stack<Relation> cycle) {
|
---|
24 | this.cycle = new ArrayList<>(cycle);
|
---|
25 | }
|
---|
26 |
|
---|
27 | protected String formatRelation(Relation r) {
|
---|
28 | StringBuilder sb = new StringBuilder();
|
---|
29 | if (r.getName() != null) {
|
---|
30 | sb.append('\'').append(r.getName()).append('\'');
|
---|
31 | } else if (!r.isNew()) {
|
---|
32 | sb.append(r.getId());
|
---|
33 | } else {
|
---|
34 | sb.append("relation@").append(r.hashCode());
|
---|
35 | }
|
---|
36 | return sb.toString();
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public String getMessage() {
|
---|
41 | StringBuilder sb = new StringBuilder();
|
---|
42 | sb.append(tr("Cyclic dependency between relations:"))
|
---|
43 | .append('[');
|
---|
44 | for (int i = 0; i < cycle.size(); i++) {
|
---|
45 | if (i > 0) {
|
---|
46 | sb.append(',');
|
---|
47 | }
|
---|
48 | sb.append(formatRelation(cycle.get(i)));
|
---|
49 | }
|
---|
50 | sb.append(']');
|
---|
51 | return sb.toString();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Gets the cycle
|
---|
56 | * @return The cycle that was detected
|
---|
57 | */
|
---|
58 | public List<Relation> getCyclicUploadDependency() {
|
---|
59 | return Collections.unmodifiableList(cycle);
|
---|
60 | }
|
---|
61 | }
|
---|