KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
13 import org.mmbase.storage.search.*;
14 import org.mmbase.util.logging.*;
15
16 /**
17  * Basic implementation.
18  *
19  * @author Rob van Maris
20  * @version $Id: BasicCompositeConstraint.java,v 1.7 2005/05/02 13:02:09 michiel Exp $
21  * @since MMBase-1.7
22  */

23 public class BasicCompositeConstraint extends BasicConstraint implements CompositeConstraint {
24     private static final Logger log = Logging.getLoggerInstance(BasicCompositeConstraint.class);
25
26     /** The child constraints. */
27     private List childs = new ArrayList();
28
29     /** The logical operator. */
30     private int logicalOperator = 0;
31
32     /**
33      * Constructor.
34      *
35      * @param logicalOperator The logical operator.
36      * @throws IllegalArgumentException when an invalid argument is supplied.
37      */

38     public BasicCompositeConstraint(int logicalOperator) {
39         // Invalid argument, must be either LOGICAL_AND or LOGICAL_OR.
40
if (logicalOperator != CompositeConstraint.LOGICAL_AND
41         && logicalOperator != CompositeConstraint.LOGICAL_OR) {
42             throw new IllegalArgumentException JavaDoc(
43             ("Invalid argument: " + logicalOperator + ", must be either "
44             + CompositeConstraint.LOGICAL_AND + " or "
45             + CompositeConstraint.LOGICAL_OR));
46         }
47         this.logicalOperator = logicalOperator;
48     }
49
50     /**
51      * Adds new child constraint.
52      *
53      * @param child The child constraint.
54      * @return This <code>BasicCompositeConstraint</code> instance.
55      * @throws IllegalArgumentException when an invalid argument is supplied.
56      */

57     public BasicCompositeConstraint addChild(Constraint child) {
58         if (child == null) {
59             throw new IllegalArgumentException JavaDoc(
60             "Invalid child argument: " + child);
61         }
62         // Check constraint not added to itself.
63
if (child == this) {
64             throw new IllegalArgumentException JavaDoc(
65             "Trying to add constraint as child to itself: " + child);
66         }
67         childs.add(child);
68         return this;
69     }
70
71     public BasicCompositeConstraint removeChild(Constraint child) {
72         if (! childs.remove(child)) {
73             log.info("Tried to remove non existing child");
74         }
75         return this;
76     }
77
78
79     // javadoc is inherited
80
public List getChilds() {
81         // return a unmodifiable list
82
return Collections.unmodifiableList(childs);
83     }
84
85     // javadoc is inherited
86
public int getLogicalOperator() {
87         return logicalOperator;
88     }
89
90     /**
91      * Returns a description of the logical operator
92      */

93     public String JavaDoc getLogicalOperatorDescription() {
94         try {
95             return CompositeConstraint.LOGICAL_OPERATOR_DESCRIPTIONS[logicalOperator];
96         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
97             return null;
98         }
99     }
100
101     // javadoc is inherited
102
public int getBasicSupportLevel() {
103         // Calculate support as lowest value among childs.
104
int result = SearchQueryHandler.SUPPORT_OPTIMAL;
105         Iterator iChilds = childs.iterator();
106         while (iChilds.hasNext()) {
107             Constraint constraint = (Constraint) iChilds.next();
108             int support = constraint.getBasicSupportLevel();
109             if (support < result) {
110                 result = support;
111                 // Stop iteration when a not supported child constraint is found.
112
if (result == SearchQueryHandler.SUPPORT_NONE) {
113                     break;
114                 }
115             }
116         }
117         return result;
118     }
119
120     // javadoc is inherited
121
public boolean equals(Object JavaDoc obj) {
122         if (obj == this) {
123             return true;
124         }
125         if (obj instanceof CompositeConstraint) {
126             CompositeConstraint constraint = (CompositeConstraint) obj;
127             return isInverse() == constraint.isInverse()
128                 && logicalOperator == constraint.getLogicalOperator()
129                 && childs.equals(constraint.getChilds());
130             // XXX should order of childs matter (it does now)?
131
} else {
132             return false;
133         }
134     }
135
136     // javadoc is inherited
137
public int hashCode() {
138         return super.hashCode()
139         + 109 * logicalOperator
140         + 71 * childs.hashCode();
141     }
142
143     // javadoc is inherited
144
public String JavaDoc toString() {
145         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("CompositeConstraint(inverse:").
146         append(isInverse()).
147         append(", operator:").append(getLogicalOperatorDescription()).
148         append(", childs:").append(getChilds()).
149         append(")");
150         return sb.toString();
151     }
152 }
153
Popular Tags