1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.function.Predicate;
|
---|
6 | import java.util.stream.Collectors;
|
---|
7 | import java.util.stream.Stream;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.command.Command;
|
---|
10 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Exception thrown when a primitive or data set does not pass its integrity checks.
|
---|
14 | * @since 2399
|
---|
15 | */
|
---|
16 | public class DataIntegrityProblemException extends RuntimeException {
|
---|
17 |
|
---|
18 | private final String htmlMessage;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Constructs a new {@code DataIntegrityProblemException}.
|
---|
22 | * @param message the detail message
|
---|
23 | */
|
---|
24 | public DataIntegrityProblemException(String message) {
|
---|
25 | this(message, null);
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code DataIntegrityProblemException}.
|
---|
30 | * @param message the detail message
|
---|
31 | * @param htmlMessage HTML-formatted error message. Can be null
|
---|
32 | * @param p the primitive involved in this integrity problem (used for constructing a detailed message)
|
---|
33 | */
|
---|
34 | public DataIntegrityProblemException(String message, String htmlMessage, OsmPrimitive... p) {
|
---|
35 | super(message + relevantCommands(p));
|
---|
36 | this.htmlMessage = htmlMessage;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Returns the HTML-formatted error message.
|
---|
41 | * @return the HTML-formatted error message, or null
|
---|
42 | */
|
---|
43 | public String getHtmlMessage() {
|
---|
44 | return htmlMessage;
|
---|
45 | }
|
---|
46 |
|
---|
47 | private static String relevantCommands(OsmPrimitive... p) {
|
---|
48 | if (p == null || p.length == 0) {
|
---|
49 | return "";
|
---|
50 | }
|
---|
51 | Predicate<Command> isParticipating = c -> Arrays.stream(p).anyMatch(c.getParticipatingPrimitives()::contains);
|
---|
52 | Stream<String> undo = UndoRedoHandler.getInstance().getUndoCommands().stream()
|
---|
53 | .filter(isParticipating)
|
---|
54 | .map(c -> "[" + c.getDescriptionText() + "]");
|
---|
55 | Stream<String> redo = UndoRedoHandler.getInstance().getRedoCommands().stream()
|
---|
56 | .filter(isParticipating)
|
---|
57 | .map(c -> "[" + c.getDescriptionText() + " (undone)]");
|
---|
58 | return Stream.concat(undo, redo)
|
---|
59 | .collect(Collectors.joining(", ", " (changed by the following commands: ", ")"));
|
---|
60 | }
|
---|
61 | }
|
---|