KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > expressions > Accessor


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.expressions;
16
17 import com.gentlyweb.utils.Getter;
18
19 import org.josql.Query;
20 import org.josql.QueryExecutionException;
21 import org.josql.QueryParseException;
22
23 import org.josql.internal.Utilities;
24
25 /**
26  * Represents an "accessor" into an object. An accessor is basically a dot separated list
27  * of method names, such as: <code>myObj.id.name</code>.
28  * <p>
29  * All of the methods referenced must have no arguments and be "public" in the referring class.
30  * You can use either the actual method name or the JavaBean naming convention.
31  * Thus: <code>myObj.id.name</code> might also be represented as: <code>getMyObj.getId.getName</code>.
32  * <p>
33  * Last Modified By: $Author: barrygently $<br />
34  * Last Modified On: $Date: 2005/04/28 05:48:59 $<br />
35  * Current Revision: $Revision: 1.4 $<br />
36  */

37 public class Accessor extends ValueExpression
38 {
39
40     private String JavaDoc acc = null;
41     private Getter get = null;
42
43     public Class JavaDoc getExpectedReturnType (Query q)
44                                     throws QueryParseException
45     {
46
47     return this.get.getType ();
48
49     }
50
51     public void init (Query q)
52                   throws QueryParseException
53     {
54
55     // Now init the getter.
56
try
57     {
58
59         this.get = new Getter (this.acc,
60                    q.getFromObjectClass ());
61
62     } catch (Exception JavaDoc e) {
63
64         throw new QueryParseException ("Unable to create getter: " +
65                        this.acc,
66                        e);
67
68     }
69
70     }
71
72     public String JavaDoc getAccessor ()
73     {
74
75     return this.acc;
76
77     }
78
79     public void setAccessor (String JavaDoc a)
80     {
81
82     this.acc = a;
83
84     }
85
86     public Getter getGetter ()
87     {
88
89     return this.get;
90
91     }
92
93     public void setName (String JavaDoc name)
94     {
95
96     this.acc = name;
97
98     }
99
100     public boolean isTrue (Object JavaDoc o,
101                Query q)
102                        throws QueryExecutionException
103     {
104
105     o = this.evaluate (o,
106                q);
107
108     if (o == null)
109     {
110         
111         return false;
112
113     }
114
115     if (Utilities.isNumber (o))
116     {
117
118         return Utilities.getDouble (o) > 0;
119
120     }
121
122     if (o instanceof Boolean JavaDoc)
123     {
124
125         return ((Boolean JavaDoc) o).booleanValue ();
126
127     }
128
129     // Not null so return true...
130
return true;
131
132     }
133
134     public boolean hasFixedResult (Query q)
135     {
136
137     // Well duh...
138
return false;
139
140     }
141
142     public Object JavaDoc evaluate (Object JavaDoc o,
143                 Query q)
144                         throws QueryExecutionException
145     {
146
147     try
148     {
149
150         return this.get.getValue (o);
151
152     } catch (Exception JavaDoc e) {
153
154         throw new QueryExecutionException ("Unable to get value from: " +
155                            this +
156                            " passed in object type: " +
157                            o.getClass ().getName () +
158                            " expecting: " +
159                            this.get.getType ().getName (),
160                            e);
161
162     }
163
164     }
165
166     public boolean equals (Object JavaDoc o)
167     {
168
169     if (o == null)
170     {
171
172         return false;
173
174     }
175
176     if (!(o instanceof Accessor))
177     {
178
179         return false;
180
181     }
182
183     Accessor a = (Accessor) o;
184
185     return this.acc.equals (a.getAccessor ());
186
187     }
188
189     public String JavaDoc toString ()
190     {
191
192     if (this.isBracketed ())
193     {
194
195         return "(" + this.acc + ")";
196
197     }
198
199     return this.acc + "[detail: " + this.get + "]";
200
201     }
202
203 }
204
Popular Tags