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.Set;
23  import org.apache.syncope.common.lib.types.ClientExceptionType;
24  
25  public class SyncopeClientException extends RuntimeException {
26  
27      private static final long serialVersionUID = 3380920886511913475L;
28  
29      private ClientExceptionType type;
30  
31      private final Set<String> elements = new HashSet<>();
32  
33      public static SyncopeClientException build(final ClientExceptionType type) {
34          if (type == ClientExceptionType.Composite) {
35              throw new IllegalArgumentException("Composite exceptions must be obtained via buildComposite()");
36          }
37          return new SyncopeClientException(type);
38      }
39  
40      public static SyncopeClientCompositeException buildComposite() {
41          return new SyncopeClientCompositeException();
42      }
43  
44      protected SyncopeClientException(final ClientExceptionType type) {
45          super();
46          setType(type);
47      }
48  
49      public boolean isComposite() {
50          return getType() == ClientExceptionType.Composite;
51      }
52  
53      public SyncopeClientCompositeException asComposite() {
54          if (!isComposite()) {
55              throw new IllegalArgumentException("This is not a composite exception");
56          }
57  
58          return (SyncopeClientCompositeException) this;
59      }
60  
61      public ClientExceptionType getType() {
62          return type;
63      }
64  
65      public final void setType(final ClientExceptionType type) {
66          this.type = type;
67      }
68  
69      public Set<String> getElements() {
70          return elements;
71      }
72  
73      public boolean isEmpty() {
74          return elements.isEmpty();
75      }
76  
77      public int size() {
78          return elements.size();
79      }
80  
81      @Override
82      public String getMessage() {
83          return new StringBuilder().
84                  append(getType()).
85                  append(' ').
86                  append(getElements()).
87                  toString();
88      }
89  
90      @Override
91      public String getLocalizedMessage() {
92          return getMessage();
93      }
94  }