1 56 57 package org.objectstyle.cayenne.exp; 58 59 import java.io.PrintWriter ; 60 import java.util.ArrayList ; 61 import java.util.Collection ; 62 import java.util.List ; 63 64 import org.apache.commons.collections.Transformer; 65 66 72 public class ListExpression extends Expression { 73 protected List operands = new ArrayList (); 74 75 public ListExpression() { 76 } 77 78 public ListExpression(int type) { 79 this.type = type; 80 } 81 82 protected void flattenTree() { 83 84 } 85 86 protected boolean pruneNodeForPrunedChild(Object prunedChild) { 87 return false; 88 } 89 90 public Expression transform(Transformer transformer) { 91 Expression copy = super.transform(transformer); 92 93 if (!(copy instanceof ListExpression)) { 94 return copy; 95 } 96 97 switch (copy.getOperandCount()) { 100 case 1 : 101 return (Expression) copy.getOperand(0); 102 case 0 : 103 return null; 104 default : 105 return copy; 106 } 107 } 108 109 114 public Expression shallowCopy() { 115 return new ListExpression(type); 116 } 117 118 121 public int getOperandCount() { 122 return operands.size(); 123 } 124 125 128 public Object getOperand(int index) { 129 if (operands.size() <= index) { 130 throw new IllegalArgumentException ( 131 "Attempt to retrieve operand " 132 + index 133 + ", while current number of operands is " 134 + operands.size()); 135 } 136 137 return operands.get(index); 138 } 139 140 143 public void setOperand(int index, Object value) { 144 if (operands.size() == index) { 145 appendOperand(value); 146 } 147 else if (operands.size() > index) { 148 operands.set(index, value); 149 } 150 else { 151 throw new IllegalArgumentException ( 152 "Attempt to set operand " 153 + index 154 + ", while current number of operands is " 155 + operands.size()); 156 } 157 } 158 159 public void appendOperand(Object value) { 160 operands.add(value); 161 } 162 163 public void appendOperands(Collection operands) { 164 this.operands.addAll(operands); 165 } 166 167 public void removeOperand(Object value) { 168 operands.remove(value); 169 } 170 171 177 public Expression joinExp(int type, Expression exp) { 178 if (type != this.type) { 179 return super.joinExp(type, exp); 180 } 181 182 ListExpression copy = new ListExpression(); 184 copy.setType(type); 185 copy.appendOperands(operands); 186 copy.appendOperand(exp); 187 188 return copy; 189 } 190 191 194 public void encodeAsString(PrintWriter pw) { 195 StringBuffer buffer = new StringBuffer (); 198 toStringBuffer(buffer); 199 pw.print(buffer.toString()); 200 } 201 } 202 | Popular Tags |