1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.mail2.core;
19
20 import java.io.OutputStreamWriter;
21 import java.io.PrintStream;
22 import java.io.PrintWriter;
23 import java.nio.charset.Charset;
24 import java.util.Collection;
25 import java.util.concurrent.Callable;
26 import java.util.function.Supplier;
27
28
29
30
31
32
33
34
35
36
37
38
39 public class EmailException extends Exception {
40
41
42 private static final long serialVersionUID = 5550674499282474616L;
43
44 public static <V> V call(final Callable<V> callable) throws EmailException {
45 try {
46 return callable.call();
47 } catch (final Exception e) {
48 throw new EmailException(e);
49 }
50 }
51
52 public static <T> T check(final Supplier<Boolean> test, final T subject, final Supplier<String> message) throws EmailException {
53 if (test.get()) {
54 throw new EmailException(message.get());
55 }
56 return subject;
57 }
58
59 public static <T> Collection<T> checkNonEmpty(final Collection<T> value, final Supplier<String> message) throws EmailException {
60 return check(() -> EmailUtils.isEmpty(value), value, message);
61 }
62
63 public static String checkNonEmpty(final String value, final Supplier<String> message) throws EmailException {
64 return check(() -> EmailUtils.isEmpty(value), value, message);
65 }
66
67 public static <T> T[] checkNonEmpty(final T[] value, final Supplier<String> message) throws EmailException {
68 return check(() -> EmailUtils.isEmpty(value), value, message);
69 }
70
71 public static <T> T checkNonNull(final T test, final Supplier<String> message) throws EmailException {
72 if (test == null) {
73 throw new EmailException(message.get());
74 }
75 return test;
76 }
77
78
79
80
81 public EmailException() {
82 }
83
84
85
86
87
88
89 public EmailException(final String msg) {
90 super(msg);
91 }
92
93
94
95
96
97
98
99 public EmailException(final String msg, final Throwable rootCause) {
100 super(msg, rootCause);
101 }
102
103
104
105
106
107
108 public EmailException(final Throwable rootCause) {
109 super(rootCause);
110 }
111
112
113
114
115 @Override
116 public void printStackTrace() {
117 printStackTrace(System.err);
118 }
119
120
121
122
123
124
125 @Override
126 public void printStackTrace(final PrintStream out) {
127 synchronized (out) {
128 final PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, Charset.defaultCharset()), false);
129 printStackTrace(pw);
130
131 pw.flush();
132 }
133 }
134
135
136
137
138
139
140 @Override
141 public void printStackTrace(final PrintWriter out) {
142 synchronized (out) {
143 super.printStackTrace(out);
144 }
145 }
146 }