1 56 package org.objectstyle.cayenne.exp.parser; 57 58 import java.io.PrintWriter ; 59 import java.util.ArrayList ; 60 import java.util.Collection ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import org.objectstyle.cayenne.exp.Expression; 65 66 72 public class ASTList extends SimpleNode { 73 protected Object [] values; 74 75 ASTList(int id) { 76 super(id); 77 } 78 79 public ASTList() { 80 super(ExpressionParserTreeConstants.JJTLIST); 81 } 82 83 86 public ASTList(Object [] objects) { 87 super(ExpressionParserTreeConstants.JJTLIST); 88 setValues(objects); 89 } 90 91 94 public ASTList(Collection objects) { 95 super(ExpressionParserTreeConstants.JJTLIST); 96 setValues(objects); 97 } 98 99 102 public ASTList(Iterator objects) { 103 super(ExpressionParserTreeConstants.JJTLIST); 104 setValues(objects); 105 } 106 107 110 public Expression shallowCopy() { 111 return new ASTList(id); 112 } 113 114 protected Object evaluateNode(Object o) throws Exception { 115 return values; 116 } 117 118 public int getType() { 119 return Expression.LIST; 120 } 121 122 protected String getExpressionOperator(int index) { 123 return ","; 124 } 125 126 public void encodeAsString(PrintWriter pw) { 127 pw.print('('); 128 129 if ((values != null) && (values.length > 0)) { 130 for (int i = 0; i < values.length; ++i) { 131 if (i > 0) { 132 pw.print(getExpressionOperator(i)); 133 pw.print(' '); 134 } 135 136 if (values[i] instanceof Expression) { 137 ((Expression) values[i]).encodeAsString(pw); 138 } 139 else { 140 encodeScalarAsString(pw, values[i]); 141 } 142 } 143 } 144 145 pw.print(')'); 146 } 147 148 public int getOperandCount() { 149 return 1; 150 } 151 152 public Object getOperand(int index) { 153 if (index == 0) { 154 return values; 155 } 156 157 throw new ArrayIndexOutOfBoundsException (index); 158 } 159 160 public void setOperand(int index, Object value) { 161 if (index != 0) { 162 throw new ArrayIndexOutOfBoundsException (index); 163 } 164 165 setValues(value); 166 } 167 168 172 protected void setValues(Object value) { 173 if (value == null) { 174 this.values = null; 175 } 176 else if (value instanceof Object []) { 177 this.values = (Object []) value; 178 } 179 else if (value instanceof Collection ) { 180 this.values = ((Collection ) value).toArray(); 181 } 182 else if (value instanceof Iterator ) { 183 List values = new ArrayList (); 184 Iterator it = (Iterator ) value; 185 while (it.hasNext()) { 186 values.add(it.next()); 187 } 188 189 this.values = values.toArray(); 190 } 191 else { 192 throw new IllegalArgumentException ( 193 "Invalid value class '" 194 + value.getClass().getName() 195 + "', expected null, Object[], Collection, Iterator"); 196 } 197 } 198 199 public void jjtClose() { 200 super.jjtClose(); 201 202 int size = jjtGetNumChildren(); 206 Object [] listValue = new Object [size]; 207 for (int i = 0; i < size; i++) { 208 listValue[i] = unwrapChild(jjtGetChild(i)); 209 } 210 211 setValues(listValue); 212 213 children = null; 215 } 216 } 217 | Popular Tags |