KickJava   Java API By Example, From Geeks To Geeks.

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


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.storage.search.*;
14
15 /**
16  * Basic implementation.
17  * The tested operation is equality, unless it is explicitly set.
18  *
19  * @author Rob van Maris
20  * @version $Id: BasicFieldCompareConstraint.java,v 1.11 2005/10/02 16:18:15 michiel Exp $
21  * @since MMBase-1.7
22  */

23 public class BasicFieldCompareConstraint extends BasicFieldConstraint implements FieldCompareConstraint {
24
25     /** The operator. */
26     private int operator = FieldCompareConstraint.EQUAL;
27
28     /**
29      * Constructor.
30      * Protected, so only subclasses can be instantiated.
31      *
32      * @param field The associated field.
33      * @throws IllegalArgumentException when an invalid argument is supplied.
34      */

35     protected BasicFieldCompareConstraint(StepField field) {
36         super(field);
37     }
38
39     /**
40      * Sets operator.
41      *
42      * @param operator the operator value
43      * @return This <code>BasicFieldCompareConstraint</code> instance.
44      * @throws IllegalArgumentException when an invalid argument is supplied.
45      */

46     public BasicFieldCompareConstraint setOperator(int operator) {
47
48         // Test for defined operator value.
49
if (operator < FieldCompareConstraint.LESS
50         || operator > FieldCompareConstraint.LIKE) {
51             throw new IllegalArgumentException JavaDoc(
52             "Invalid operator value: " + operator );
53         }
54
55         // Test "LIKE" operator only used with string type field.
56
if (operator == FieldCompareConstraint.LIKE
57             && getField().getType() != Field.TYPE_STRING
58             && getField().getType() != Field.TYPE_XML) {
59             throw new IllegalArgumentException JavaDoc("LIKE operator not allowed for this field type: " + getField().getType());
60         }
61
62         this.operator = operator;
63         return this;
64     }
65
66     // javadoc is inherited
67
public int getOperator() {
68         return operator;
69     }
70
71     /**
72      * Returns a description of the operator
73      */

74     public String JavaDoc getOperatorDescription() {
75         try {
76             return FieldCompareConstraint.OPERATOR_DESCRIPTIONS[operator];
77         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
78             return null;
79         }
80     }
81
82     // javadoc is inherited
83
public boolean equals(Object JavaDoc obj) {
84         // Must be same class (subclasses should override this)!
85
if (obj != null && obj.getClass() == getClass()) {
86             BasicFieldCompareConstraint constraint
87                 = (BasicFieldCompareConstraint) obj;
88             return isInverse() == constraint.isInverse()
89                 && isCaseSensitive() == constraint.isCaseSensitive()
90                 && getField().getFieldName().equals(constraint.getField().getFieldName())
91                 && BasicStepField.compareSteps(getField().getStep(),
92                     constraint.getField().getStep())
93                 && operator == constraint.operator;
94         } else {
95             return false;
96         }
97     }
98
99     // javadoc is inherited
100
public int hashCode() {
101         return super.hashCode()
102         + 113 * operator;
103     }
104
105     // javadoc is inherited
106
public String JavaDoc toString() {
107         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("BasicFieldCompareConstraint(inverse:").
108         append(isInverse()).
109         append(", field:").append(getFieldName()).
110         append(", casesensitive:").append(isCaseSensitive()).
111         append(", operator:").append(getOperatorDescription()).
112         append(")");
113         return sb.toString();
114     }
115
116 }
117
Popular Tags