-
Notifications
You must be signed in to change notification settings - Fork 5
/
ExceptionUtils.java
66 lines (60 loc) · 1.91 KB
/
ExceptionUtils.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.utils;
import edu.ie3.datamodel.models.Entity;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class ExceptionUtils {
private ExceptionUtils() {
throw new IllegalStateException("Utility classes cannot be instantiated");
}
/**
* Creates a string containing multiple exception messages.
*
* @param exceptions list of exceptions
* @return str containing the messages
*/
public static String getMessages(List<? extends Exception> exceptions) {
return exceptions.stream()
.map(Throwable::getMessage)
.reduce("", (a, b) -> a + "\n " + b)
.replaceFirst("\n ", "");
}
/**
* Creates a string containing multiple exception messages.
*
* @param exceptions list of exceptions
* @return str containing the messages
*/
public static String getFullMessages(List<? extends Exception> exceptions) {
return exceptions.stream()
.map(e -> e.getMessage() + printStackTrace(e.getStackTrace()))
.reduce("", (a, b) -> a + "\n " + b)
.replaceFirst("\n ", "");
}
/**
* Combines multiple {@link Entity} into a string.
*
* @param entities to be combined
* @return a string
*/
public static String combine(Collection<? extends Entity> entities) {
return "{" + entities.stream().map(Entity::toString).collect(Collectors.joining(", ")) + "}";
}
/**
* Method for combining {@link StackTraceElement}s.
*
* @param elements to be combined
* @return a string
*/
public static String printStackTrace(StackTraceElement... elements) {
return Arrays.stream(elements)
.map(StackTraceElement::toString)
.collect(Collectors.joining("\n "));
}
}