1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // FirstNHandleStrategy.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.perfectjpattern.core.api.behavioral.chainofresponsibility.*;
24
25 /**
26 * Concrete implementation of <code>IChainStrategy</code> that allows
27 * only one <code>IHandler</code> to handle the given request then stops the
28 * processing of the chain.
29 * <br/><br/>
30 * <code>OnlyOneHandleStrategy</code> is stateless, reentrant and a Singleton
31 * therefore it may not be directly instantiated, neither it may be extended.
32 *
33 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
34 * @version $Revision: 1.0 $ $Date: Nov 8, 2007 9:57:12 PM $
35 */
36 public final
37 class OnlyOneHandleStrategy
38 implements IChainStrategy
39 {
40 //------------------------------------------------------------------------
41 /**
42 * Returns Singleton instance of <code>OnlyOneHandlesStrategy</code>.
43 *
44 * @return Singleton instance of <code>OnlyOneHandlesStrategy</code>.
45 */
46 public static IChainStrategy
47 getInstance()
48 {
49 return INSTANCE;
50 }
51
52 //------------------------------------------------------------------------
53 /**
54 * {@inheritDoc}
55 */
56 public void
57 process(IHandler<Object> aHandler, Object aRequest)
58 {
59 if (aHandler.canHandle(aRequest))
60 {
61 aHandler.handle(aRequest);
62 }
63 else
64 {
65 aHandler.getSuccessor().start(aRequest);
66 }
67 }
68
69 //------------------------------------------------------------------------
70 // private
71 //------------------------------------------------------------------------
72 private
73 OnlyOneHandleStrategy()
74 {
75 // do nothing
76 }
77
78 //------------------------------------------------------------------------
79 // members
80 //------------------------------------------------------------------------
81 private static final IChainStrategy INSTANCE = new OnlyOneHandleStrategy();
82 }