1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // Invoker.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.command;
22
23 import org.apache.commons.lang.*;
24 import org.perfectjpattern.core.api.behavioral.command.*;
25
26
27 /**
28 * Base core implementation of <code>IInvoker</code> interface.
29 * <br/>
30 *
31 * @see IInvoker
32 *
33 * @param <P> Command Parameter context-specific.
34 * @param <R> Command Result context-specific.
35 *
36 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
37 * @version $Revision: 1.0 $ $Date: Jun 23, 2007 2:34:57 AM $
38 */
39 public
40 class Invoker<P, R>
41 implements IInvoker<P, R>
42 {
43 //------------------------------------------------------------------------
44 // public
45 //------------------------------------------------------------------------
46 /**
47 * {@inheritDoc}
48 */
49 public R
50 getResult()
51 throws IllegalStateException
52 {
53 if (theCommand == null)
54 {
55 throw new IllegalStateException("ICommand was not set.");
56 }
57
58 R myResult = theCommand.getResult();
59 if (myResult == null)
60 {
61 throw new IllegalStateException("No result available.");
62 }
63
64 return myResult;
65 }
66
67 //------------------------------------------------------------------------
68 /**
69 * {@inheritDoc}
70 */
71 public void
72 invoke()
73 throws IllegalStateException
74 {
75 if (theCommand == null)
76 {
77 throw new IllegalStateException("ICommand was not set.");
78 }
79
80 if (theParameter != null)
81 {
82 theCommand.setParameter(theParameter);
83 }
84
85 theCommand.execute();
86 }
87
88 //------------------------------------------------------------------------
89 /**
90 * {@inheritDoc}
91 */
92 public void
93 setCommand(ICommand<P, R> aCommand)
94 {
95 theCommand = aCommand;
96 }
97
98 //------------------------------------------------------------------------
99 /**
100 * {@inheritDoc}
101 */
102 public void
103 setParameter(P aParameter)
104 throws IllegalArgumentException
105 {
106 Validate.notNull(aParameter, "'aParameter' must not be null");
107
108 theParameter = aParameter;
109 }
110
111 //------------------------------------------------------------------------
112 // members
113 //------------------------------------------------------------------------
114 /**
115 * The <code>ICommand</code> instance.
116 */
117 private ICommand<P, R> theCommand;
118
119 /**
120 * The <code>IParameter</code> instance.
121 */
122 private P theParameter;
123 }