001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.storage.search.implementation;
011:
012: import java.util.*;
013: import org.mmbase.storage.search.*;
014: import org.mmbase.util.logging.*;
015:
016: /**
017: * Basic implementation.
018: *
019: * @author Rob van Maris
020: * @version $Id: BasicCompositeConstraint.java,v 1.10 2007/02/11 14:46:13 nklasens Exp $
021: * @since MMBase-1.7
022: */
023: public class BasicCompositeConstraint extends BasicConstraint implements
024: CompositeConstraint {
025: private static final Logger log = Logging
026: .getLoggerInstance(BasicCompositeConstraint.class);
027:
028: /** The child constraints. */
029: private List<Constraint> childs = new ArrayList<Constraint>();
030:
031: /** The logical operator. */
032: private int logicalOperator = 0;
033:
034: /**
035: * Constructor.
036: *
037: * @param logicalOperator The logical operator.
038: * @throws IllegalArgumentException when an invalid argument is supplied.
039: */
040: public BasicCompositeConstraint(int logicalOperator) {
041: // Invalid argument, must be either LOGICAL_AND or LOGICAL_OR.
042: if (logicalOperator != CompositeConstraint.LOGICAL_AND
043: && logicalOperator != CompositeConstraint.LOGICAL_OR) {
044: throw new IllegalArgumentException(
045: ("Invalid argument: " + logicalOperator
046: + ", must be either "
047: + CompositeConstraint.LOGICAL_AND + " or " + CompositeConstraint.LOGICAL_OR));
048: }
049: this .logicalOperator = logicalOperator;
050: }
051:
052: /**
053: * Adds new child constraint.
054: *
055: * @param child The child constraint.
056: * @return This <code>BasicCompositeConstraint</code> instance.
057: * @throws IllegalArgumentException when an invalid argument is supplied.
058: */
059: public BasicCompositeConstraint addChild(Constraint child) {
060: if (child == null) {
061: throw new IllegalArgumentException(
062: "Invalid child argument: " + child);
063: }
064: // Check constraint not added to itself.
065: if (child == this ) {
066: throw new IllegalArgumentException(
067: "Trying to add constraint as child to itself: "
068: + child);
069: }
070: childs.add(child);
071: return this ;
072: }
073:
074: public BasicCompositeConstraint removeChild(Constraint child) {
075: if (!childs.remove(child)) {
076: log.info("Tried to remove non existing child");
077: }
078: return this ;
079: }
080:
081: // javadoc is inherited
082: public List<Constraint> getChilds() {
083: // return a unmodifiable list
084: return Collections.unmodifiableList(childs);
085: }
086:
087: // javadoc is inherited
088: public int getLogicalOperator() {
089: return logicalOperator;
090: }
091:
092: /**
093: * Returns a description of the logical operator
094: */
095: public String getLogicalOperatorDescription() {
096: try {
097: return CompositeConstraint.LOGICAL_OPERATOR_DESCRIPTIONS[logicalOperator];
098: } catch (IndexOutOfBoundsException ioobe) {
099: return null;
100: }
101: }
102:
103: // javadoc is inherited
104: public int getBasicSupportLevel() {
105: // Calculate support as lowest value among childs.
106: int result = SearchQueryHandler.SUPPORT_OPTIMAL;
107: Iterator<Constraint> iChilds = childs.iterator();
108: while (iChilds.hasNext()) {
109: Constraint constraint = iChilds.next();
110: int support = constraint.getBasicSupportLevel();
111: if (support < result) {
112: result = support;
113: // Stop iteration when a not supported child constraint is found.
114: if (result == SearchQueryHandler.SUPPORT_NONE) {
115: break;
116: }
117: }
118: }
119: return result;
120: }
121:
122: // javadoc is inherited
123: public boolean equals(Object obj) {
124: if (obj == this ) {
125: return true;
126: }
127: if (obj instanceof CompositeConstraint) {
128: CompositeConstraint constraint = (CompositeConstraint) obj;
129: return isInverse() == constraint.isInverse()
130: && logicalOperator == constraint
131: .getLogicalOperator()
132: && childs.equals(constraint.getChilds());
133: // XXX should order of childs matter (it does now)?
134: } else {
135: return false;
136: }
137: }
138:
139: // javadoc is inherited
140: public int hashCode() {
141: return super .hashCode() + 109 * logicalOperator + 71
142: * childs.hashCode();
143: }
144:
145: // javadoc is inherited
146: public String toString() {
147: StringBuilder sb = new StringBuilder(
148: "CompositeConstraint(inverse:").append(isInverse())
149: .append(", operator:").append(
150: getLogicalOperatorDescription()).append(
151: ", childs:").append(getChilds()).append(")");
152: return sb.toString();
153: }
154: }
|