1 19 20 package org.apache.cayenne.exp.parser; 21 22 import java.io.PrintWriter ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.cayenne.exp.Expression; 29 30 36 public class ASTList extends SimpleNode { 37 protected Object [] values; 38 39 ASTList(int id) { 40 super(id); 41 } 42 43 public ASTList() { 44 super(ExpressionParserTreeConstants.JJTLIST); 45 } 46 47 50 public ASTList(Object [] objects) { 51 super(ExpressionParserTreeConstants.JJTLIST); 52 setValues(objects); 53 } 54 55 58 public ASTList(Collection objects) { 59 super(ExpressionParserTreeConstants.JJTLIST); 60 setValues(objects); 61 } 62 63 66 public ASTList(Iterator objects) { 67 super(ExpressionParserTreeConstants.JJTLIST); 68 setValues(objects); 69 } 70 71 74 public Expression shallowCopy() { 75 return new ASTList(id); 76 } 77 78 protected Object evaluateNode(Object o) throws Exception { 79 return values; 80 } 81 82 public int getType() { 83 return Expression.LIST; 84 } 85 86 protected String getExpressionOperator(int index) { 87 return ","; 88 } 89 90 public void encodeAsString(PrintWriter pw) { 91 pw.print('('); 92 93 if ((values != null) && (values.length > 0)) { 94 for (int i = 0; i < values.length; ++i) { 95 if (i > 0) { 96 pw.print(getExpressionOperator(i)); 97 pw.print(' '); 98 } 99 100 if (values[i] instanceof Expression) { 101 ((Expression) values[i]).encodeAsString(pw); 102 } 103 else { 104 encodeScalarAsString(pw, values[i]); 105 } 106 } 107 } 108 109 pw.print(')'); 110 } 111 112 public int getOperandCount() { 113 return 1; 114 } 115 116 public Object getOperand(int index) { 117 if (index == 0) { 118 return values; 119 } 120 121 throw new ArrayIndexOutOfBoundsException (index); 122 } 123 124 public void setOperand(int index, Object value) { 125 if (index != 0) { 126 throw new ArrayIndexOutOfBoundsException (index); 127 } 128 129 setValues(value); 130 } 131 132 136 protected void setValues(Object value) { 137 if (value == null) { 138 this.values = null; 139 } 140 else if (value instanceof Object []) { 141 this.values = (Object []) value; 142 } 143 else if (value instanceof Collection ) { 144 this.values = ((Collection ) value).toArray(); 145 } 146 else if (value instanceof Iterator ) { 147 List values = new ArrayList (); 148 Iterator it = (Iterator ) value; 149 while (it.hasNext()) { 150 values.add(it.next()); 151 } 152 153 this.values = values.toArray(); 154 } 155 else { 156 throw new IllegalArgumentException ( 157 "Invalid value class '" 158 + value.getClass().getName() 159 + "', expected null, Object[], Collection, Iterator"); 160 } 161 } 162 163 public void jjtClose() { 164 super.jjtClose(); 165 166 int size = jjtGetNumChildren(); 170 Object [] listValue = new Object [size]; 171 for (int i = 0; i < size; i++) { 172 listValue[i] = unwrapChild(jjtGetChild(i)); 173 } 174 175 setValues(listValue); 176 177 children = null; 179 } 180 } 181 | Popular Tags |