1 | /* |
2 | * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. |
3 | * |
4 | * WSO2 Inc. licenses this file to you under the Apache License, |
5 | * Version 2.0 (the "License"); you may not use this file except |
6 | * in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, |
12 | * software distributed under the License is distributed on an |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | * KIND, either express or implied. See the License for the |
15 | * specific language governing permissions and limitations |
16 | * under the License. |
17 | */ |
18 | package org.wso2.siddhi.core.projector.attibute.generator; |
19 | |
20 | import org.wso2.siddhi.core.event.AtomicEvent; |
21 | import org.wso2.siddhi.core.event.remove.RemoveStream; |
22 | import org.wso2.siddhi.core.executor.expression.ExpressionExecutor; |
23 | import org.wso2.siddhi.core.projector.attibute.aggregator.Aggregator; |
24 | import org.wso2.siddhi.core.util.parser.AggregatorParser; |
25 | import org.wso2.siddhi.core.util.parser.ExecutorParser; |
26 | import org.wso2.siddhi.query.api.definition.Attribute; |
27 | import org.wso2.siddhi.query.api.expression.Expression; |
28 | import org.wso2.siddhi.query.api.query.QueryEventStream; |
29 | |
30 | import java.util.List; |
31 | |
32 | public class MaxAggregateAttributeGenerator extends AbstractAggregateAttributeGenerator { |
33 | private ExpressionExecutor expressionExecutor; |
34 | private Aggregator aggregator; |
35 | |
36 | public MaxAggregateAttributeGenerator() { |
37 | } |
38 | |
39 | private MaxAggregateAttributeGenerator(Aggregator aggregator, |
40 | ExpressionExecutor expressionExecutor) { |
41 | this.aggregator = aggregator; |
42 | this.expressionExecutor = expressionExecutor; |
43 | } |
44 | |
45 | @Override |
46 | public void assignExpressions(Expression[] expressions, |
47 | List<QueryEventStream> queryEventStreamList) { |
48 | expressionExecutor = ExecutorParser.parseExpression(expressions[0], queryEventStreamList, null); |
49 | aggregator = AggregatorParser.createMaxAggregator(expressionExecutor.getType()); |
50 | } |
51 | |
52 | @Override |
53 | public AbstractAggregateAttributeGenerator createNewInstance() { |
54 | return new MaxAggregateAttributeGenerator(aggregator.createNewInstance(),expressionExecutor); |
55 | } |
56 | |
57 | @Override |
58 | public Attribute.Type getType() { |
59 | return aggregator.getType(); |
60 | } |
61 | |
62 | @Override |
63 | public Object process(AtomicEvent event) { |
64 | if (event instanceof RemoveStream) { |
65 | return aggregator.remove(expressionExecutor.execute(event)); |
66 | } else { |
67 | return aggregator.add(expressionExecutor.execute(event)); |
68 | } |
69 | } |
70 | } |