KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > go > Go


1 /**
2  * $Id: Go.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.go;
30
31 import java.util.Collections JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.apache.tools.ant.Project;
36 import com.idaremedia.apis.FixtureStarter;
37 import com.idaremedia.antx.helpers.Tk;
38
39 /**
40  * Namespace for the collection of simple tests used by conditionally executed
41  * AntX components.
42  *
43  * @since JWare/AntX 0.4
44  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
45  * @version 0.5
46  * @.safety multiple
47  * @.group impl,helper
48  * @.pattern GoF.Strategy
49  * @see com.idaremedia.antx.parameters.FlexConditional FlexConditional
50  **/

51
52 public final class Go
53 {
54     /**
55      * Single execution criterion test for a conditionally executed
56      * Ant component. "Go" tests typically customize a generic, criteria
57      * decision strategy to a specific Ant task or application request.
58      *
59      * @since JWare/AntX 0.4
60      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
61      * @version 0.5
62      * @.safety n/a
63      * @.group impl,infra
64      * @see Go.TestList
65      **/

66     public interface Test
67     {
68         /**
69          * Returns <i>true</i> if test is passed and component can run.
70          * @param P project from which fixture information should
71          * be read (non-null).
72          **/

73         boolean pass(Project P);
74
75         /**
76          * Returns this test's parameter string. Will return
77          * <i>null</i> if never defined.
78          **/

79         String JavaDoc getParameter();
80
81         /**
82          * Returns this test's parameter's public-facing name.
83          * Will return the empty string if never defined.
84          **/

85         String JavaDoc getParameterName();
86     }
87
88
89     /**
90      * Starter implementation of an instance-configurable
91      * execute ("go") test.
92      *
93      * @since JWare/AntX 0.4
94      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
95      * @version 0.5
96      * @.safety multiple
97      * @.pattern GoF.Strategy
98      * @.group impl,helper
99      **/

100     public static abstract class TestSkeleton implements Test
101     {
102         /**
103          * Initializes new test. Test's parameter must
104          * be specified.
105          * @see #setParameter setParameter
106          **/

107         protected TestSkeleton() {
108         }
109
110         /**
111          * Initializes new test with parameter.
112          * @param param test's parameter (non-null)
113          **/

114         protected TestSkeleton(String JavaDoc param) {
115             setParameter(param);
116         }
117
118         /**
119          * Initializes this test's parameter string. Should
120          * be done before first call to {@linkplain #pass pass()}.
121          **/

122         public final void setParameter(String JavaDoc parameter) {
123             m_param = parameter;
124         }
125
126         /**
127          * Returns this test's parameter string. Will return
128          * <i>null</i> if never defined.
129          **/

130         public final String JavaDoc getParameter() {
131             return m_param;
132         }
133
134         /* @no.javadoc
135          * Returns leaf name of this class. Never <i>null</i>.
136          */

137         public String JavaDoc getParameterName() {
138             return Tk.lowercaseFrom(Tk.leafNameFrom(getClass()));
139         }
140
141         /**
142          * Ensures this test's parameter string is defined.
143          * @throws IllegalStateException if not fully defined.
144          **/

145         protected void verifyInited() {
146             if (getParameter()==null) {
147                 throw new IllegalStateException JavaDoc();
148             }
149         }
150
151         /**
152          * Returns a simple diagnostics string like=
153          * "<span class="src">parametername=parameter</span>" or
154          * (for example) "<span class="src">ifAll='p0,p2,p3'</span>".
155          **/

156         public String JavaDoc toString() {
157             return getParameterName()+"='"+getParameter()+"'";
158         }
159
160         private String JavaDoc m_param;
161     }
162
163
164     /**
165      * A simple sequence of execution ("go") tests.
166      *
167      * @since JWare/AntX 0.4
168      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
169      * @version 0.5
170      * @.safety special (depends on factory method used)
171      * @.group impl,helper
172      **/

173     public static final class TestList
174     {
175         /**
176          * Factory method for new unsynchronized list of
177          * default capacity (4 slots).
178          **/

179         public static TestList newList() {
180             return new TestList(false,4);
181         }
182
183         /**
184          * Factory method for new unsynchronized list of
185          * custom capacity.
186          **/

187         public static TestList newList(int iniCapacity) {
188             return new TestList(false,iniCapacity);
189         }
190
191         /**
192          * Factory method for new synchronized list of
193          * default capacity (4 slots).
194          **/

195         public static TestList newSynchronizedList() {
196             return new TestList(true,4);
197         }
198
199         /**
200          * Factory method for new synchronized list of
201          * custom capacity.
202          **/

203         public static TestList newSynchronizedList(int iniCapacity) {
204             return new TestList(true,iniCapacity);
205         }
206
207         /**
208          * Hidden constructor. Available through factory methods.
209          **/

210         TestList(boolean mtsafe, int iniCapacity) {
211             if (mtsafe) {
212                 m_tests = FixtureStarter.newSynchronizedList(iniCapacity);
213             } else {
214                 m_tests = FixtureStarter.newList(iniCapacity);
215             }
216         }
217
218         /**
219          * Returns <i>true</i> if this list is empty.
220          **/

221         public boolean isEmpty() {
222             return m_tests.isEmpty();
223         }
224
225         /**
226          * Appends a new go test to this list. New test is
227          * not checked for duplication.
228          * @param t new test (non-null)
229          **/

230         public void add(Go.Test t) {
231             if (t==null) {
232                 throw new IllegalArgumentException JavaDoc();
233             }
234             m_tests.add(t);
235         }
236
237         /**
238          * Appends an existing list of tests to this list.
239          * New tests are not checked for duplication.
240          * @param l list of tests (non-null)
241          **/

242         public void add(TestList l) {
243             if (l==null) {
244                 throw new IllegalArgumentException JavaDoc();
245             }
246             if (!l.isEmpty()) {
247                 m_tests.addAll(l.m_tests);
248             }
249         }
250
251         /**
252          * Removes all tests from this list. On return this
253          * list is {@linkplain #isEmpty empty}.
254          **/

255         public void clear() {
256             m_tests.clear();
257         }
258
259         /**
260          * Returns the number of tests in this list.
261          **/

262         public int size() {
263             return m_tests.size();
264         }
265
266         /**
267          * Returns the index'th test.
268          **/

269         public Go.Test get(int i) {
270             return (Go.Test)m_tests.get(i);
271         }
272
273         /**
274          * Returns the first test of specified class in
275          * this list. Returns <i>null</i> if no match.
276          * @param c class to test for (non-null)
277          **/

278         public Go.Test get(Class JavaDoc c)
279         {
280             if (c==null) {
281                 throw new IllegalArgumentException JavaDoc();
282             }
283             synchronized(m_tests) {
284                 for (int i=0,N=size();i<N;i++) {
285                     Go.Test t= (Go.Test)m_tests.get(i);
286                     if (c.isInstance(t)) {
287                         return t;
288                     }
289                 }
290             }
291             return null;
292         }
293
294
295         /**
296          * Returns <i>true</i> if this list contains at
297          * least one test of the given class.
298          * @param c class to test for (non-null)
299          **/

300         public boolean contains(Class JavaDoc c)
301         {
302             return get(c)!=null;
303         }
304
305         /**
306          * Returns a <em>readonly</em> ordered iterator for
307          * this list. Items are returned in the order they
308          * were added.
309          **/

310         public Iterator JavaDoc iterator()
311         {
312             return Collections.unmodifiableList(m_tests).iterator();
313         }
314
315         /**
316          * Returns <i>true</i> if this list is equivalent
317          * to another list.
318          * @param o object to check
319          **/

320         public boolean equals(Object JavaDoc o) {
321             if (o==this) {
322                 return true;
323             }
324             if (o==null) {
325                 return false;
326             }
327             if (o.getClass()==TestList.class) {
328                 return m_tests.equals(((TestList)o).m_tests);
329             }
330             return false;
331         }
332
333         /**
334          * Returns a hash value for this list. Returned
335          * value depends on this list's current tests.
336          **/

337         public int hashCode()
338         {
339             return m_tests.hashCode();
340         }
341
342         private final List JavaDoc m_tests;
343     }
344
345
346     /**
347      * Prevent; only strategies public.
348      **/

349     private Go()
350     {
351     }
352 }
353
354
355 /* end-of-Go.java */
356
Popular Tags