KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > storage > search > implementation > BasicStepField


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.storage.search.implementation;
11
12 import org.mmbase.bridge.Field;
13 import org.mmbase.core.CoreField;
14 import org.mmbase.storage.search.*;
15
16 /**
17  * Basic implementation.
18  * The field alias is not set on default.
19  *
20  * @author Rob van Maris
21  * @version $Id: BasicStepField.java,v 1.23 2006/07/04 13:04:00 michiel Exp $
22  * @since MMBase-1.7
23  */

24 public class BasicStepField implements StepField {
25
26     /** Associated field definition. */
27     private CoreField field = null;
28
29     /** Associated step. */
30     private Step step = null;
31
32     /** Alias property. */
33     private String JavaDoc alias = null;
34
35     /**
36      * Tests if a value is acceptable for comparison with a certain field.
37      * @param value The value to be tested.
38      * @param field The non-null field.
39      * @throws IllegalArgumentException when the value is not acceptable
40      * for this field.
41      */

42     // package visibility!
43
static void testValue(Object JavaDoc value, StepField field) {
44         int type = field.getType();
45
46         // Test for null value.
47
if (value == null) {
48             throw new IllegalArgumentException JavaDoc("Invalid value for " + org.mmbase.core.util.Fields.getTypeDescription(type) + " field: " + value);
49         }
50
51         // Test for compatible type.
52
boolean ok;
53         switch (type) {
54         case Field.TYPE_BINARY:
55             ok = value instanceof byte[];
56             break;
57             // Numerical types.
58
case Field.TYPE_INTEGER:
59         case Field.TYPE_FLOAT:
60         case Field.TYPE_DOUBLE:
61         case Field.TYPE_LONG:
62         case Field.TYPE_NODE:
63             ok = value instanceof Number JavaDoc;
64             break;
65
66             // String types.
67
case Field.TYPE_XML:
68             
69         case Field.TYPE_STRING:
70             ok = value instanceof String JavaDoc;
71             break;
72         case Field.TYPE_BOOLEAN:
73             ok = value instanceof Boolean JavaDoc;
74             break;
75         case Field.TYPE_DATETIME:
76             ok = value instanceof java.util.Date JavaDoc || value instanceof Number JavaDoc;
77             break;
78         case Field.TYPE_LIST:
79             ok = value instanceof java.util.List JavaDoc;
80             break;
81
82
83         default: // Unknown field type, should not occur.
84
throw new IllegalStateException JavaDoc("Unknown field type: " + type);
85         }
86
87         if (!ok) {
88             throw new IllegalArgumentException JavaDoc("Invalid value for " + org.mmbase.core.util.Fields.getTypeDescription(type) + " field: "
89                                                + value + ", of type " + value.getClass().getName());
90         }
91     }
92
93     /**
94      * Compares two field values for equality.
95      * Numerical fields are compared based on their numerical value, as they
96      * may be of different type.
97      *
98      * @param value1 The first value, either a <code>String</code>
99      * or a <code>Number</code>
100      * @param value2 The second value, either a <code>String</code>
101      * or a <code>Number</code>
102      * @return True if both values represent the same string or numerical value.
103      */

104     // package visibility!
105
static boolean equalFieldValues(Object JavaDoc value1, Object JavaDoc value2) {
106         if (value1 instanceof Number JavaDoc) {
107             if (value2 instanceof Number JavaDoc) {
108                 Number JavaDoc number1 = (Number JavaDoc) value1;
109                 Number JavaDoc number2 = (Number JavaDoc) value2;
110                 return Double.doubleToLongBits(number1.doubleValue()) == Double.doubleToLongBits(number2.doubleValue());
111             } else {
112                 return false;
113             }
114         } else {
115             return (value1 == null? value2 == null: value1.equals(value2));
116         }
117     }
118
119     /**
120      * Constructor.
121      *
122      * @param step The associated step.
123      * @param field The associated field.
124      * @throws IllegalArgumentException when an invalid argument is supplied.
125      */

126     public BasicStepField(Step step, CoreField field) {
127         if (step == null) {
128             throw new IllegalArgumentException JavaDoc(
129             "Invalid step value: " + step);
130         }
131         this.step = step;
132
133         if (field == null) {
134             throw new IllegalArgumentException JavaDoc(
135             "Invalid field value: " + field);
136         }
137         // Check field belongs to step
138
if (!step.getTableName().equals(field.getParent().getTableName())) {
139             throw new IllegalArgumentException JavaDoc(
140             "Invalid field value, belongs to step " + field.getParent().getTableName()
141             + " instead of step " + step.getTableName() + ": "
142             + field);
143         }
144         this.field = field;
145     }
146
147     /**
148      * Sets alias property.
149      *
150      * @param alias The alias property.
151      * @return This <code>BasicStepField</code> instance.
152      * @throws IllegalArgumentException when an invalid argument is supplied.
153      */

154     public BasicStepField setAlias(String JavaDoc alias) {
155         if (alias != null && alias.trim().length() == 0) {
156             throw new IllegalArgumentException JavaDoc("Invalid alias value: " + alias);
157         }
158         this.alias = alias;
159         return this;
160     }
161
162     /**
163      * Gets the associated field.
164      *
165      * @return The field.
166      */

167     public CoreField getField() {
168         return field;
169     }
170
171     // javadoc is inherited
172
public String JavaDoc getFieldName() {
173         return field.getName();
174     }
175
176     // javadoc is inherited
177
public String JavaDoc getAlias() {
178         return alias;
179     }
180
181     // javadoc is inherited
182
public Step getStep() {
183         return step;
184     }
185
186     // javadoc in inherited
187
public int getType() {
188         return field.getType();
189     }
190
191     // javadoc is inherited
192
public boolean equals(Object JavaDoc obj) {
193         if (obj instanceof StepField) {
194             StepField field = (StepField) obj;
195             return BasicStepField.compareSteps(getStep(), field.getStep())
196                 && getFieldName().equals(field.getFieldName())
197                 && (alias == null? field.getAlias() == null : alias.equals(field.getAlias()));
198         } else {
199             return false;
200         }
201     }
202
203     // javadoc is inherited
204
public int hashCode() {
205         return (getStep().getAlias() == null?
206             47 * getStep().getTableName().hashCode():
207             51 * getStep().getAlias().hashCode())
208         + 53 * getFieldName().hashCode()
209         + (alias == null? 0: 59 * alias.hashCode());
210     }
211
212     /**
213      * Utility method, compares steps by their alias or table name.
214      * Steps are considered equal if their aliases are equal.
215      * When their aliases are <code>null</code>, the steps are considered
216      * equal if their tablenames are equal as well.
217      * <p>
218      * This can be used to verify that both steps refer to the same step
219      * in a <code>SearchQuery</code> object.
220      * Note that this differs from the equality defined by their
221      * {@link org.mmbase.storage.search.Step#equals() equals()} method.
222      *
223      * @param step1 The first step.
224      * @param step2 The second step.
225      * @return <code>true</code> when the steps are considered equal,
226      * <code>false</code> otherwise.
227      */

228     // package visibility!
229
static boolean compareSteps(Step step1, Step step2) {
230         String JavaDoc alias1 = step1.getAlias();
231         if (alias1 == null) {
232             return step2.getAlias() == null
233             && step1.getTableName().equals(step2.getTableName());
234         } else {
235             return alias1.equals(step2.getAlias());
236         }
237     }
238
239     /**
240      * Returns the field's fieldname, possibly extended with the step's name if known.
241      * May return null or partial fieldnames if not all data is available (for use in debugging).
242      * @param field the fieldname whose name to return
243      */

244     static public String JavaDoc getFieldName(StepField field) {
245         String JavaDoc fieldName = null;
246         if (field != null) {
247             fieldName = field.getAlias();
248             if (fieldName == null) {
249                 fieldName = field.getFieldName();
250             }
251             Step step = field.getStep();
252             if (step != null) {
253                 if (step.getAlias() != null) {
254                     fieldName = step.getAlias() + "." + fieldName;
255                 } else {
256                     fieldName = step.getTableName() + "." + fieldName;
257                 }
258             }
259         }
260         return fieldName;
261     }
262
263
264     // javadoc is inherited
265
public String JavaDoc toString() {
266         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
267         Step step = getStep();
268         if (step == null) {
269             sb.append("null");
270         } else {
271             String JavaDoc stepAlias = step.getAlias();
272             if (stepAlias == null) {
273                 sb.append(getStep().getTableName());
274             } else {
275                 sb.append(stepAlias);
276             }
277         }
278         sb.append(".").append(getFieldName());
279         String JavaDoc alias = getAlias();
280         if (alias != null) {
281             sb.append(" as ").append(alias);
282         }
283         return sb.toString();
284     }
285
286 }
287
Popular Tags