01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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: package org.apache.openjpa.kernel.exps;
20:
21: import java.util.Collection;
22: import java.util.Collections;
23:
24: import org.apache.openjpa.kernel.StoreContext;
25: import org.apache.openjpa.lib.util.Localizer;
26: import org.apache.openjpa.util.UserException;
27:
28: /**
29: * A value produced from evaluating a result aggregate.
30: *
31: * @author Abe White
32: */
33: class Aggregate extends Val {
34:
35: private static final Localizer _loc = Localizer
36: .forPackage(Aggregate.class);
37:
38: private final AggregateListener _listener;
39: private final Val _arg;
40:
41: /**
42: * Constructor. Supply aggregate listener and its argument value, if any.
43: */
44: public Aggregate(AggregateListener listener, Val arg) {
45: _listener = listener;
46: _arg = arg;
47: }
48:
49: public boolean isAggregate() {
50: return true;
51: }
52:
53: public Class getType() {
54: return _listener.getType(getArgTypes());
55: }
56:
57: public void setImplicitType(Class type) {
58: }
59:
60: protected Object eval(Object candidate, Object orig,
61: StoreContext ctx, Object[] params) {
62: if (candidate == null)
63: candidate = Collections.EMPTY_LIST;
64:
65: // allow aggregates to be used in filter expressions so long as a
66: // collection is passed in
67: if (candidate instanceof Collection)
68: return eval((Collection) candidate, orig, ctx, params)
69: .iterator().next();
70: throw new UserException(_loc.get("agg-in-filter"));
71: }
72:
73: protected Collection eval(Collection candidates, Object orig,
74: StoreContext ctx, Object[] params) {
75: Collection args = null;
76: if (_arg != null)
77: args = _arg.eval(candidates, orig, ctx, params);
78: Object agg = _listener.evaluate(args, getArgTypes(),
79: candidates, ctx);
80: return Collections.singleton(agg);
81: }
82:
83: private Class[] getArgTypes() {
84: if (_arg == null)
85: return null;
86: if (_arg instanceof Args)
87: return ((Args) _arg).getTypes();
88: return new Class[] { _arg.getType() };
89: }
90:
91: public void acceptVisit(ExpressionVisitor visitor) {
92: visitor.enter(this);
93: if (_arg != null)
94: _arg.acceptVisit(visitor);
95: visitor.exit(this);
96: }
97: }
|