1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.collections15.functors.closure;
17
18 import net.sf.collections15.Closure;
19 import net.sf.collections15.functors.FunctorException;
20
21 import java.io.Serializable;
22
23
24 /***
25 * <code>Closure</code> implementation that always throws an exception.
26 *
27 * @author Stephen Colebourne
28 * @author Chris Lambrou (port to Java 5.0)
29 * @since Collections15 1.0
30 */
31 public final class ExceptionClosure <E> implements Closure<E>, Serializable
32 {
33 static final long serialVersionUID = -7399394837445226116L;
34
35 /***
36 * The message of the exceptions thrown by the {@link #execute} method.
37 */
38 private String message;
39
40 /***
41 * Returns an instance of <code>ExceptionClosure</code>.
42 * Exceptions thrown by the {@link #execute} method will contain the message
43 * string <tt>"ExceptionClosure invoked"</tt>.
44 *
45 * @return An instance of <code>ExceptionClosure</code>.
46 *
47 * @since Collections15 1.0
48 */
49 public static <T> ExceptionClosure<T> getInstance()
50 {
51 return getInstance("ExceptionClosure invoked");
52 }
53
54 /***
55 * Returns an instance of <code>ExceptionClosure</code>.
56 * Exceptions thrown by the {@link #execute} method will contain the
57 * specified message string.
58 *
59 * @param message The message string of the exceptions thrown by the {@link #execute} method.
60 * @return An instance of <code>ExceptionClosure</code>.
61 *
62 * @since Collections15 1.0
63 */
64 public static <T> ExceptionClosure<T> getInstance(String message)
65 {
66 return new ExceptionClosure<T>(message);
67 }
68
69 /***
70 * Constructs a new instance.
71 */
72 protected ExceptionClosure(String message)
73 {
74 if (message == null)
75 {
76 throw new IllegalArgumentException("null message");
77 }
78 this.message = message;
79 }
80
81 /***
82 * Always throws an exception.
83 *
84 * @param input The input object
85 *
86 * @throws FunctorException Always thrown.
87 */
88 public void execute(E input)
89 {
90 throw new FunctorException(message);
91 }
92 }