KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > AssertableProjectComponent


1 /**
2  * $Id: AssertableProjectComponent.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-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 as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your option) any later
9  * 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 (GNU Lesser General Public License) 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 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;
30
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.ProjectComponent;
33
34 import com.idaremedia.apis.UIStringManager;
35
36 import com.idaremedia.antx.apis.ProjectDependent;
37 import com.idaremedia.antx.helpers.Tk;
38
39 /**
40  * Extension of basic Ant <i>ProjectComponent</i> that adds builtin assertions.
41  *
42  * @since JWare/AntX 0.1
43  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
44  * @version 0.5
45  * @.safety multiple
46  * @.group impl,infra
47  **/

48
49 public abstract class AssertableProjectComponent extends ProjectComponent
50     implements ProjectDependent
51 {
52     /**
53      * Creates new unlabeled component.
54      **/

55     protected AssertableProjectComponent()
56     {
57         super();
58         Iam_="";
59     }
60
61
62     /**
63      * Creates new cv-labeled component.
64      * @param iam CV-label (non-null)
65      **/

66     protected AssertableProjectComponent(String JavaDoc iam)
67     {
68         super();
69         Iam_= Tk.cvlabelFrom(iam);
70     }
71
72
73     /**
74      * Returns this component's CV-label. Never <i>null</i>.
75      **/

76     protected final String JavaDoc cvlabel_()
77     {
78         return Iam_;
79     }
80
81
82     /**
83      * Shortcut that returns this task's internal AntX UI
84      * strings manager. Never returns <i>null</i>.
85      * @see AntX#uistrs
86      **/

87     public final UIStringManager uistrs()
88     {
89         return AntX.uistrs();
90     }
91
92
93     /**
94      * Throws assertion error if pre-condtion is not met.
95      * @param c pre-condition
96      * @param msg [optional] failure message (if not met)
97      * @throws IllegalArgumentException if condition not met
98      **/

99     protected final void require_(boolean c, String JavaDoc msg)
100     {
101         if (!c) {
102             String JavaDoc ermsg = uistrs().get("cv.require",Iam_,msg);
103             log(ermsg, Project.MSG_ERR);
104             throw new IllegalArgumentException JavaDoc(ermsg);
105         }
106     }
107
108
109     /**
110      * Throws assertion error if post-condition is not met. Used
111      * for post-condition verification.
112      * @param c post-condition
113      * @param msg [optional] failure message (if not met)
114      * @throws IllegalStateException if condition not met
115      **/

116     protected final void ensure_(boolean c, String JavaDoc msg)
117     {
118         if (!c) {
119             String JavaDoc ermsg = uistrs().get("cv.ensure",Iam_,msg);
120             log(ermsg, Project.MSG_ERR);
121             throw new IllegalStateException JavaDoc(ermsg);
122         }
123     }
124
125
126     /**
127      * Throws assertion error if condition is not met. Used for
128      * block and invariant verification.
129      * @param c condition
130      * @param msg [optional] failure message (if not met)
131      * @throws IllegalStateException if condition not met
132      **/

133     protected final void verify_(boolean c, String JavaDoc msg)
134     {
135         if (!c) {
136             String JavaDoc ermsg = uistrs().get("cv.verify",Iam_,msg);
137             log(ermsg, Project.MSG_ERR);
138             throw new IllegalStateException JavaDoc(ermsg);
139         }
140     }
141
142
143     /**
144      * Notes an unexpected but manageable problem has occured.
145      * Just logs a warning by default.
146      * @param t [optional] causing throwable
147      * @param msg caller's additional (context) message
148      **/

149     protected final void unexpected_(Throwable JavaDoc t, String JavaDoc msg)
150     {
151         String JavaDoc ermsg = uistrs().get("cv.unexpected",Iam_,msg,t);
152         log(ermsg, Project.MSG_WARN);
153     }
154
155
156     /**
157      * Verifies we're in a live project (created from build
158      * process).
159      **/

160     protected final void verifyInProject_(String JavaDoc calr)
161     {
162         if (getProject()==null) {
163             String JavaDoc ermsg = uistrs().get("cv.verifyInP",Iam_,calr);
164             log(ermsg, Project.MSG_ERR);
165             throw new IllegalStateException JavaDoc(ermsg);
166         }
167     }
168
169
170     private final String JavaDoc Iam_;
171 }
172
173 /* end-of-AssertableProjectComponent.java */
174
Popular Tags