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