| 1 | /* |
| 2 | * Copyright (c) 2005-2012, 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.groupby; |
| 19 | |
| 20 | import org.wso2.siddhi.core.event.AtomicEvent; |
| 21 | import org.wso2.siddhi.core.executor.expression.ExpressionExecutor; |
| 22 | import org.wso2.siddhi.core.projector.attibute.generator.AbstractAggregateAttributeGenerator; |
| 23 | |
| 24 | import java.util.HashMap; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | public class GroupByAttributeGeneratorMap { |
| 28 | |
| 29 | private Map<String, AbstractAggregateAttributeGenerator> attributeGeneratorMap = new HashMap<String, AbstractAggregateAttributeGenerator>(); |
| 30 | private ExpressionExecutor[] expressionExecutors; |
| 31 | private AbstractAggregateAttributeGenerator attributeGenerator; |
| 32 | |
| 33 | public GroupByAttributeGeneratorMap(ExpressionExecutor[] expressionExecutors, |
| 34 | AbstractAggregateAttributeGenerator attributeGenerator) { |
| 35 | this.expressionExecutors = expressionExecutors; |
| 36 | this.attributeGenerator = attributeGenerator; |
| 37 | } |
| 38 | |
| 39 | public AbstractAggregateAttributeGenerator get(AtomicEvent event) { |
| 40 | String key = getKey(event); |
| 41 | AbstractAggregateAttributeGenerator generator = attributeGeneratorMap.get(key); |
| 42 | if (generator != null) { |
| 43 | return generator; |
| 44 | } else { |
| 45 | generator = attributeGenerator.createNewInstance(); |
| 46 | attributeGeneratorMap.put(key, generator); |
| 47 | return generator; |
| 48 | |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private String getKey(AtomicEvent event) { |
| 53 | StringBuilder sb = new StringBuilder(); |
| 54 | for (ExpressionExecutor executor : expressionExecutors) { |
| 55 | sb.append(executor.execute(event)).append("::"); |
| 56 | } |
| 57 | return sb.toString(); |
| 58 | } |
| 59 | } |