View Javadoc

1   /*
2    *  Copyright 2001-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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>&quot;ExceptionClosure invoked&quot;</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  }