KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > exp > parser > ASTList


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.exp.parser;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cayenne.exp.Expression;
29
30 /**
31  * A leaf expression representing an immutable collection of values.
32  *
33  * @since 1.1
34  * @author Andrus Adamchik
35  */

36 public class ASTList extends SimpleNode {
37     protected Object JavaDoc[] values;
38
39     ASTList(int id) {
40         super(id);
41     }
42
43     public ASTList() {
44         super(ExpressionParserTreeConstants.JJTLIST);
45     }
46
47     /**
48      * Initializes a list expression with an Object[].
49      */

50     public ASTList(Object JavaDoc[] objects) {
51         super(ExpressionParserTreeConstants.JJTLIST);
52         setValues(objects);
53     }
54
55     /**
56      * Initializes a list expression with a Java Collection
57      */

58     public ASTList(Collection JavaDoc objects) {
59         super(ExpressionParserTreeConstants.JJTLIST);
60         setValues(objects);
61     }
62
63     /**
64      * Initializes a list expression with a Java Iterator.
65      */

66     public ASTList(Iterator JavaDoc objects) {
67         super(ExpressionParserTreeConstants.JJTLIST);
68         setValues(objects);
69     }
70
71     /**
72      * Creates a copy of this expression node, without copying children.
73      */

74     public Expression shallowCopy() {
75         return new ASTList(id);
76     }
77
78     protected Object JavaDoc evaluateNode(Object JavaDoc o) throws Exception JavaDoc {
79         return values;
80     }
81
82     public int getType() {
83         return Expression.LIST;
84     }
85
86     protected String JavaDoc getExpressionOperator(int index) {
87         return ",";
88     }
89
90     public void encodeAsString(PrintWriter JavaDoc 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 JavaDoc getOperand(int index) {
117         if (index == 0) {
118             return values;
119         }
120
121         throw new ArrayIndexOutOfBoundsException JavaDoc(index);
122     }
123
124     public void setOperand(int index, Object JavaDoc value) {
125         if (index != 0) {
126             throw new ArrayIndexOutOfBoundsException JavaDoc(index);
127         }
128
129         setValues(value);
130     }
131
132     /**
133      * Sets an internal collection of values. Value argument
134      * can be an Object[], a Collection or an iterator.
135      */

136     protected void setValues(Object JavaDoc value) {
137         if (value == null) {
138             this.values = null;
139         }
140         else if (value instanceof Object JavaDoc[]) {
141             this.values = (Object JavaDoc[]) value;
142         }
143         else if (value instanceof Collection JavaDoc) {
144             this.values = ((Collection JavaDoc) value).toArray();
145         }
146         else if (value instanceof Iterator JavaDoc) {
147             List JavaDoc values = new ArrayList JavaDoc();
148             Iterator JavaDoc it = (Iterator JavaDoc) value;
149             while (it.hasNext()) {
150                 values.add(it.next());
151             }
152
153             this.values = values.toArray();
154         }
155         else {
156             throw new IllegalArgumentException JavaDoc(
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         // For backwards compatibility set a List value wrapping the nodes.
167
// or maybe we should rewrite the parser spec to insert children
168
// directly into internal collection?
169
int size = jjtGetNumChildren();
170         Object JavaDoc[] listValue = new Object JavaDoc[size];
171         for (int i = 0; i < size; i++) {
172             listValue[i] = unwrapChild(jjtGetChild(i));
173         }
174
175         setValues(listValue);
176
177         // clean children - we are not supposed to use them anymore
178
children = null;
179     }
180 }
181
Popular Tags