1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.common.lib;
20
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Optional;
24 import java.util.Set;
25 import org.apache.syncope.common.lib.types.ClientExceptionType;
26
27 public class SyncopeClientCompositeException extends SyncopeClientException {
28
29 private static final long serialVersionUID = 7882118041134372129L;
30
31 private final Set<SyncopeClientException> exceptions = new HashSet<>();
32
33 protected SyncopeClientCompositeException() {
34 super(ClientExceptionType.Composite);
35 }
36
37 public boolean hasExceptions() {
38 return !exceptions.isEmpty();
39 }
40
41 public boolean hasException(final ClientExceptionType exceptionType) {
42 return getException(exceptionType) != null;
43 }
44
45 public SyncopeClientException getException(final ClientExceptionType exceptionType) {
46 boolean found = false;
47 SyncopeClientException syncopeClientException = null;
48 for (Iterator<SyncopeClientException> itor = exceptions.iterator(); itor.hasNext() && !found;) {
49 syncopeClientException = itor.next();
50 if (syncopeClientException.getType().equals(exceptionType)) {
51 found = true;
52 }
53 }
54
55 return found
56 ? syncopeClientException
57 : null;
58 }
59
60 public Set<SyncopeClientException> getExceptions() {
61 return exceptions;
62 }
63
64 public boolean addException(final SyncopeClientException exception) {
65 if (exception.getType() == null) {
66 throw new IllegalArgumentException(exception + " does not have the right "
67 + ClientExceptionType.class.getName() + " set");
68 }
69
70 Optional<SyncopeClientException> alreadyAdded =
71 exceptions.stream().filter(ex -> ex.getType() == exception.getType()).findFirst();
72
73 return alreadyAdded.map(e -> e.getElements()
74 .addAll(exception.getElements())).orElseGet(() -> exceptions.add(exception));
75 }
76
77 @Override
78 public String getMessage() {
79 StringBuilder message = new StringBuilder();
80
81 message.append('{');
82 Iterator<SyncopeClientException> iter = getExceptions().iterator();
83 while (iter.hasNext()) {
84 SyncopeClientException e = iter.next();
85 message.append('[').
86 append(e.getMessage()).
87 append(']');
88 if (iter.hasNext()) {
89 message.append(", ");
90 }
91 }
92 message.append('}');
93
94 return message.toString();
95 }
96
97 @Override
98 public String getLocalizedMessage() {
99 return getMessage();
100 }
101 }