1 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 23 public class BasicCompositeConstraint extends BasicConstraint implements CompositeConstraint { 24 private static final Logger log = Logging.getLoggerInstance(BasicCompositeConstraint.class); 25 26 27 private List childs = new ArrayList(); 28 29 30 private int logicalOperator = 0; 31 32 38 public BasicCompositeConstraint(int logicalOperator) { 39 if (logicalOperator != CompositeConstraint.LOGICAL_AND 41 && logicalOperator != CompositeConstraint.LOGICAL_OR) { 42 throw new IllegalArgumentException ( 43 ("Invalid argument: " + logicalOperator + ", must be either " 44 + CompositeConstraint.LOGICAL_AND + " or " 45 + CompositeConstraint.LOGICAL_OR)); 46 } 47 this.logicalOperator = logicalOperator; 48 } 49 50 57 public BasicCompositeConstraint addChild(Constraint child) { 58 if (child == null) { 59 throw new IllegalArgumentException ( 60 "Invalid child argument: " + child); 61 } 62 if (child == this) { 64 throw new IllegalArgumentException ( 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 public List getChilds() { 81 return Collections.unmodifiableList(childs); 83 } 84 85 public int getLogicalOperator() { 87 return logicalOperator; 88 } 89 90 93 public String getLogicalOperatorDescription() { 94 try { 95 return CompositeConstraint.LOGICAL_OPERATOR_DESCRIPTIONS[logicalOperator]; 96 } catch (IndexOutOfBoundsException ioobe) { 97 return null; 98 } 99 } 100 101 public int getBasicSupportLevel() { 103 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 if (result == SearchQueryHandler.SUPPORT_NONE) { 113 break; 114 } 115 } 116 } 117 return result; 118 } 119 120 public boolean equals(Object 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 } else { 132 return false; 133 } 134 } 135 136 public int hashCode() { 138 return super.hashCode() 139 + 109 * logicalOperator 140 + 71 * childs.hashCode(); 141 } 142 143 public String toString() { 145 StringBuffer sb = new StringBuffer ("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 |