1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // Command.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>ICommand</code> interface.
29 * <br/>
30 *
31 * @see ICommand
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 3:00:54 AM $
38 */
39 public
40 class Command<P, R>
41 implements ICommand<P, R>
42 {
43 //------------------------------------------------------------------------
44 // public
45 //------------------------------------------------------------------------
46 public
47 Command()
48 {
49 // empty
50 }
51
52 //------------------------------------------------------------------------
53 public
54 Command(IReceiver<P, R> aReceiver)
55 {
56 super();
57
58 setReceiver(aReceiver);
59 }
60
61 //------------------------------------------------------------------------
62 /**
63 * {@inheritDoc}
64 */
65 public R
66 getResult()
67 throws IllegalStateException
68 {
69 if (theReceiver == null)
70 {
71 throw new IllegalStateException("IReceiver was not set.");
72 }
73
74 R myResult = theReceiver.getResult();
75
76 if (myResult == null)
77 {
78 throw new IllegalStateException("No result available.");
79 }
80
81 return myResult;
82 }
83
84 //------------------------------------------------------------------------
85 /**
86 * {@inheritDoc}
87 */
88 public void
89 setParameter(P aParameter)
90 throws IllegalArgumentException
91 {
92 Validate.notNull(aParameter, "'aParameter' must not be null");
93
94 theParameter = aParameter;
95 }
96
97 //------------------------------------------------------------------------
98 /**
99 * {@inheritDoc}
100 */
101 public void
102 setReceiver(IReceiver<P, R> aReceiver)
103 throws IllegalArgumentException
104 {
105 Validate.notNull(aReceiver, "'aReceiver' must not be null");
106
107 theReceiver = aReceiver;
108 }
109
110 //------------------------------------------------------------------------
111 /**
112 * {@inheritDoc}
113 */
114 public void
115 execute()
116 throws IllegalStateException
117 {
118 if (theReceiver == null)
119 {
120 throw new IllegalStateException("IReceiver was not set.");
121 }
122
123 if (theParameter != null)
124 {
125 theReceiver.setParameter(theParameter);
126 }
127
128 theReceiver.execute();
129 }
130
131 //------------------------------------------------------------------------
132 // members
133 //------------------------------------------------------------------------
134 /**
135 * The <code>IReceiver</code> instance.
136 */
137 private IReceiver<P, R> theReceiver;
138
139 /**
140 * The <code>IParameter</code> instance.
141 */
142 private P theParameter;
143 }