KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > solo > EqualFields


1 /**
2  * $Id: EqualFields.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.1 of the License, or (at your option) any
9  * 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 (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.condition.solo;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.ProjectComponent;
34 import org.apache.tools.ant.taskdefs.condition.Condition;
35
36 import com.idaremedia.antx.AntX;
37 import com.idaremedia.antx.AssertableProjectComponent;
38 import com.idaremedia.antx.helpers.Strings;
39
40 /**
41  * Diagnostics condition that verifies two fields on two different objects are equivalent.
42  * Can also be configured to verify that a single object field is <i>null</i>.
43  *
44  * @since JWare/AntX 0.3
45  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
46  * @version 0.5
47  * @.safety multiple
48  * @.group impl,test,helper
49  * @see AssertTask
50  **/

51
52 final class EqualFields extends AssertableProjectComponent
53     implements Condition
54 {
55     /**
56      * Initializes a new EqualFields condition.
57      **/

58     EqualFields()
59     {
60         super(AntX.conditions);
61         initFields();
62     }
63
64
65     /**
66      * Initializes a new CV-labeled EqualFields condition.
67      * @param iam CV-label (non-null)
68      **/

69     EqualFields(String JavaDoc iam)
70     {
71         super(iam);
72         initFields();
73     }
74
75
76     /**
77      * Sets the name of the first reference.
78      * @param refid reference identifier (non-null)
79      **/

80     void setItem1(String JavaDoc refid)
81     {
82         require_(refid!=null,"setItem1- nonzro refid");
83         m_item1 = refid;
84     }
85
86
87     /**
88      * Returns reference identifier for first item. Returns
89      * <i>null</i> if never set.
90      **/

91     String JavaDoc getItem1()
92     {
93         return (m_item1==__NULL1) ? null : m_item1;
94     }
95
96
97     /**
98      * Sets the name of the second reference. Pass the string
99      * "null" for null comparison.
100      **/

101     void setItem2(String JavaDoc refid)
102     {
103         if (refid==null || Strings.NULL.equalsIgnoreCase(refid)) {
104             m_item2 = null;
105         } else {
106             m_item2 = refid;
107         }
108     }
109
110
111     /**
112      * Returns reference identifier for second item. Returns
113      * <i>null</i> if never set. Returns the string "null" if
114      * will used as <i>null</i>.
115      **/

116     String JavaDoc getItem2()
117     {
118         return (m_item2==__NULL2) ? null :
119             (m_item2==null) ? Strings.NULL : m_item2;
120     }
121
122
123     /**
124      * Returns project field for given reference item. Can be easily
125      * genericized to look for any field if this is ever useful.
126      * @throws BuildException if field not properly specified
127      **/

128     private Object JavaDoc getItemField(String JavaDoc refid, boolean refRequired)
129     {
130         if (refid==null || refid==__NULL1 || refid==__NULL2) {
131             if (refRequired) {
132                 String JavaDoc ermsg = uistrs().get("brul.assert.samefield.need.item");
133                 log(ermsg,Project.MSG_ERR);
134                 throw new BuildException(ermsg);
135             }
136             return refid==null ? null : getProject();
137         }
138         //NB: Would generalize to use reflection here (ssmc)\\
139
Object JavaDoc obj = getProject().getReference(refid);
140
141         if (!(obj instanceof ProjectComponent)) {
142             String JavaDoc ermsg = uistrs().get("brul.assert.samefield.missing.field",
143                                         refid, "Project");
144             log(ermsg,Project.MSG_ERR);
145             throw new BuildException(ermsg);
146         }
147         return ((ProjectComponent)obj).getProject();
148     }
149
150
151     /**
152      * Returns <i>true</i> if the two objects have equivalent projects.
153      * @throws BuildException if not properly defined
154      **/

155     public boolean eval()
156     {
157         verifyInProject_("eval");
158         Object JavaDoc P1 = getItemField(m_item1,true);
159         Object JavaDoc P2 = getItemField(m_item2,false);
160         return equalObjects(P1,P2,true);
161     }
162
163
164     /**
165      * Returns <i>true</i> if a equals b. Handles nulls. Does not handle
166      * Java arrays.
167      **/

168     static boolean equalObjects(Object JavaDoc a, Object JavaDoc b, boolean identical)
169     {
170         if (a==null) {
171             return b==null;
172         }
173         if (b==null) {
174             return false;
175         }
176         if (a==b) {
177             return true;
178         }
179         return identical ? false : a.equals(b);
180     }
181
182
183     /**
184      * Allows us to initializes the two fields to something that isn't
185      * <i>null</i> but that won't match anything user likely to specify.
186      **/

187     private static final String JavaDoc __NULL1, __NULL2; //NB: make different
188
static {
189         StringBuffer JavaDoc sb = new StringBuffer JavaDoc
190             (String.valueOf(System.identityHashCode(EqualFields.class)));
191         sb.append("_");
192         __NULL1 = sb.substring(0);
193         sb.append("_");
194         __NULL2 = sb.substring(0);
195         sb = null;
196     }
197     private void initFields()
198     {
199         m_item1 = __NULL1;
200         m_item2 = __NULL2;
201     }
202
203     private String JavaDoc m_item1, m_item2;
204 }
205
206 /* end-of-EqualFields.java */
207
Popular Tags