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.util.parser; |
19 | |
20 | import org.wso2.siddhi.core.exception.OperationNotSupportedException; |
21 | import org.wso2.siddhi.core.projector.attibute.aggregator.Aggregator; |
22 | import org.wso2.siddhi.core.projector.attibute.aggregator.max.MaxAggregatorDouble; |
23 | import org.wso2.siddhi.core.projector.attibute.aggregator.max.MaxAggregatorFloat; |
24 | import org.wso2.siddhi.core.projector.attibute.aggregator.max.MaxAggregatorInt; |
25 | import org.wso2.siddhi.core.projector.attibute.aggregator.max.MaxAggregatorLong; |
26 | import org.wso2.siddhi.core.projector.attibute.aggregator.min.MinAggregatorDouble; |
27 | import org.wso2.siddhi.core.projector.attibute.aggregator.min.MinAggregatorFloat; |
28 | import org.wso2.siddhi.core.projector.attibute.aggregator.min.MinAggregatorInt; |
29 | import org.wso2.siddhi.core.projector.attibute.aggregator.min.MinAggregatorLong; |
30 | import org.wso2.siddhi.core.projector.attibute.aggregator.sum.SumAggregatorDouble; |
31 | import org.wso2.siddhi.core.projector.attibute.aggregator.sum.SumAggregatorFloat; |
32 | import org.wso2.siddhi.core.projector.attibute.aggregator.sum.SumAggregatorInt; |
33 | import org.wso2.siddhi.core.projector.attibute.aggregator.sum.SumAggregatorLong; |
34 | import org.wso2.siddhi.core.projector.attibute.generator.AbstractAggregateAttributeGenerator; |
35 | import org.wso2.siddhi.core.util.*; |
36 | import org.wso2.siddhi.query.api.definition.Attribute; |
37 | |
38 | public class AggregatorParser { |
39 | |
40 | |
41 | public static AbstractAggregateAttributeGenerator loadAggregatorClass(String aggregationName) { |
42 | return (AbstractAggregateAttributeGenerator) org.wso2.siddhi.core.util.ClassLoader.loadClass("org.wso2.siddhi.core.projector.attibute.generator." + aggregationName.substring(0, 1).toUpperCase() + aggregationName.substring(1) + "AggregateAttributeGenerator"); |
43 | } |
44 | |
45 | public static Aggregator createSumAggregator(Attribute.Type type) { |
46 | switch (type) { |
47 | case STRING: |
48 | throw new OperationNotSupportedException("Sum not supported for string"); |
49 | case INT: |
50 | return new SumAggregatorInt(); |
51 | case LONG: |
52 | return new SumAggregatorLong(); |
53 | case FLOAT: |
54 | return new SumAggregatorFloat(); |
55 | case DOUBLE: |
56 | return new SumAggregatorDouble(); |
57 | case BOOL: |
58 | throw new OperationNotSupportedException("Sum not supported for bool"); |
59 | } |
60 | throw new OperationNotSupportedException("Sum not supported for " + type); |
61 | } |
62 | |
63 | public static Aggregator createAvgAggregator(Attribute.Type type) { |
64 | switch (type) { |
65 | case STRING: |
66 | throw new OperationNotSupportedException("Avg not supported for string"); |
67 | case INT: |
68 | return new SumAggregatorInt(); |
69 | case LONG: |
70 | return new SumAggregatorLong(); |
71 | case FLOAT: |
72 | return new SumAggregatorFloat(); |
73 | case DOUBLE: |
74 | return new SumAggregatorDouble(); |
75 | case BOOL: |
76 | throw new OperationNotSupportedException("Avg not supported for bool"); |
77 | } |
78 | throw new OperationNotSupportedException("Avg not supported for " + type); |
79 | } |
80 | |
81 | public static Aggregator createMaxAggregator(Attribute.Type type) { |
82 | switch (type) { |
83 | case STRING: |
84 | throw new OperationNotSupportedException("Max not supported for string"); |
85 | case INT: |
86 | return new MaxAggregatorInt(); |
87 | case LONG: |
88 | return new MaxAggregatorLong(); |
89 | case FLOAT: |
90 | return new MaxAggregatorFloat(); |
91 | case DOUBLE: |
92 | return new MaxAggregatorDouble(); |
93 | case BOOL: |
94 | throw new OperationNotSupportedException("Max not supported for bool"); |
95 | } |
96 | throw new OperationNotSupportedException("Max not supported for " + type); |
97 | } |
98 | |
99 | |
100 | public static Aggregator createMinAggregator(Attribute.Type type) { |
101 | switch (type) { |
102 | case STRING: |
103 | throw new OperationNotSupportedException("Min not supported for string"); |
104 | case INT: |
105 | return new MinAggregatorInt(); |
106 | case LONG: |
107 | return new MinAggregatorLong(); |
108 | case FLOAT: |
109 | return new MinAggregatorFloat(); |
110 | case DOUBLE: |
111 | return new MinAggregatorDouble(); |
112 | case BOOL: |
113 | throw new OperationNotSupportedException("Min not supported for bool"); |
114 | } |
115 | throw new OperationNotSupportedException("Min not supported for " + type); |
116 | } |
117 | } |