View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // AbstractHandler.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.core.behavioral.chainofresponsibility;
22  
23  import org.apache.commons.lang.*;
24  import org.perfectjpattern.core.api.behavioral.chainofresponsibility.*;
25  
26  
27  /**
28   * Abstract reusable implementation of <code>IHandler</code> interface.
29   * <br/><br/>
30   * 
31   * <b>The Default Strategy is <code>OnlyOneHandleStrategy</code></b> same as 
32   * in GoF.
33   *  
34   * @see IHandler
35   * @see NullHandler
36   *
37   * @param <R> Request parameter type
38   *
39   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
40   * @version $Revision: 1.0 $ $Date: Jun 23, 2007 1:31:27 PM $
41   */
42  public abstract 
43  class AbstractHandler<R> 
44  implements IHandler<R>
45  {
46      //------------------------------------------------------------------------
47      // public
48      //------------------------------------------------------------------------
49      /**
50       * Constructs a AbstractHandler from a NullHandler
51       */
52      @SuppressWarnings("unchecked")
53      public 
54      AbstractHandler() 
55      {
56          theSuccessor = (IHandler<R>) NullHandler.getInstance();
57      }    
58  
59      //------------------------------------------------------------------------
60      /**
61       * Constructs a AbstractHandler from a Successor
62       * 
63       * @param aSuccessor Successor handler.
64       * @throws IllegalArgumentException 'aSuccessor' must not be null.
65       */
66      public 
67      AbstractHandler(IHandler<R> aSuccessor)
68      throws IllegalArgumentException
69      {
70          Validate.notNull(aSuccessor, "'aSuccessor' must not be null");
71          
72          theSuccessor = aSuccessor;
73      }    
74      
75      //------------------------------------------------------------------------
76      /** 
77       * {@inheritDoc}
78       */
79      @SuppressWarnings("unchecked")
80      public final void 
81      start(R aRequest) 
82      throws IllegalArgumentException
83      {
84          IHandler<Object> myHandler = (IHandler<Object>) this;
85          
86          theStrategy.process(myHandler, aRequest);
87      }        
88      
89      //------------------------------------------------------------------------
90      /** 
91       * {@inheritDoc}
92       */
93      public final IHandler<R>
94      getSuccessor() 
95      {        
96          return theSuccessor;
97      }
98  
99      //------------------------------------------------------------------------
100     /** 
101      * {@inheritDoc}
102      */
103     public boolean 
104     canHandle(R aRequest) 
105     throws IllegalArgumentException
106     {
107         return true;
108     }
109 
110     //------------------------------------------------------------------------
111     /** 
112      * {@inheritDoc}
113      */
114     public void
115     handle(R aRequest) 
116     throws IllegalArgumentException
117     {
118         Validate.notNull(aRequest, "'aRequest' must not be null");
119         Validate.isTrue(canHandle(aRequest), "'aRequest' can not be handled.");
120     }    
121 
122     //------------------------------------------------------------------------
123     /** 
124      * {@inheritDoc}
125      */
126     public final void 
127     setSuccessor(IHandler<R> aSuccessor) 
128     throws IllegalArgumentException
129     {
130         Validate.notNull(aSuccessor, "'aSuccessor' must not be null");
131         
132         theSuccessor = aSuccessor;
133     }
134     
135     //------------------------------------------------------------------------
136     /** 
137      * {@inheritDoc}
138      */
139     public final void 
140     setChainStrategy(IChainStrategy aStrategy)
141     throws IllegalArgumentException
142     {
143         theStrategy = aStrategy;
144     }    
145     
146     //------------------------------------------------------------------------
147     // members
148     //------------------------------------------------------------------------
149     /**
150      * Successor Handler
151      */
152     private IHandler<R> theSuccessor;
153     
154     /**
155      * Defines the Chain Strategy. <b>The Default Strategy is <code>
156      * OnlyOneHandleStrategy</code></b> same as in GoF.
157      */
158     private IChainStrategy theStrategy = OnlyOneHandleStrategy.getInstance();
159 }