View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }