1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.salesforce.apt.graph.model.errors;
28
29 import java.text.MessageFormat;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.function.Function;
33 import java.util.stream.Collectors;
34
35 import com.salesforce.apt.graph.model.AbstractModel;
36
37 public class ErrorModel {
38
39 private final ErrorType message;
40 private final List<AbstractModel> causes;
41 private final List<AbstractModel> involved;
42
43 public ErrorType getMessage() {
44 return message;
45 }
46
47 public List<AbstractModel> getCauses() {
48 return causes;
49 }
50
51 public List<AbstractModel> getInvolved() {
52 return involved;
53 }
54
55 public boolean isCyclic() {
56 return message.isCycle();
57 }
58
59 public String toString() {
60 return message.name() + " on " + involved.toString() + " caused by " + causes;
61 }
62
63
64
65
66
67
68
69 public ErrorModel(ErrorType message, List<? extends AbstractModel> causes, List<? extends AbstractModel> involved) {
70 super();
71 this.message = message;
72 if (message.isCycle() && !causes.equals(involved)) {
73 throw new IllegalArgumentException("Malformed ErrorModel");
74 }
75 this.causes = Collections.unmodifiableList(causes);
76 this.involved = Collections.unmodifiableList(involved);
77 }
78
79
80
81
82
83
84
85
86
87 public String getMessageOn(AbstractModel on, Function<ErrorType, String> converter) {
88 if (!isCyclic()) {
89 if (getCauses().size() == 1) {
90 return MessageFormat.format(converter.apply(getMessage()), getCauses().get(0));
91 } else {
92 return MessageFormat.format(converter.apply(getMessage()),
93 getCauses().stream().filter(m -> !m.equals(on)).map(m -> m.toString()).collect(Collectors.joining(", ")));
94 }
95 }
96 StringBuilder builder = new StringBuilder();
97 int index = getInvolved().indexOf(on);
98 if (index != -1) {
99 for (int i = index; i < getInvolved().size(); i++) {
100 builder.append(getInvolved().get(i).toString());
101 if (i + 1 < getInvolved().size() || index != 0) {
102 builder.append(" -> ");
103 }
104 }
105 for (int i = 0; i < index; i++) {
106 builder.append(getInvolved().get(i).toString());
107 if (i + 1 < index) {
108 builder.append(" -> ");
109 }
110 }
111 }
112 return MessageFormat.format(converter.apply(getMessage()), builder.toString());
113 }
114
115 }