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.aggregator.min; |
19 | |
20 | import org.wso2.siddhi.core.projector.attibute.aggregator.Aggregator; |
21 | import org.wso2.siddhi.query.api.definition.Attribute; |
22 | |
23 | import java.util.Deque; |
24 | import java.util.Iterator; |
25 | import java.util.LinkedList; |
26 | |
27 | public class MinAggregatorInt implements Aggregator { |
28 | |
29 | private Deque<Integer> minDeque = new LinkedList<Integer>(); |
30 | private volatile Integer minValue = null; |
31 | private Attribute.Type type = Attribute.Type.INT; |
32 | |
33 | public Object getValue() { |
34 | return minValue; |
35 | } |
36 | |
37 | public Attribute.Type getType() { |
38 | return this.type; |
39 | } |
40 | |
41 | @Override |
42 | public synchronized Object add(Object obj) { |
43 | Integer value = ((Integer) obj); |
44 | for (Iterator<Integer> iterator = minDeque.descendingIterator(); iterator.hasNext(); ) { |
45 | |
46 | if (iterator.next() > value) { |
47 | iterator.remove(); |
48 | } |
49 | } |
50 | minDeque.addLast(value); |
51 | if (minValue > value) { |
52 | minValue = value; |
53 | } |
54 | return minValue; |
55 | } |
56 | |
57 | @Override |
58 | public synchronized Object remove(Object obj) { |
59 | minDeque.removeFirstOccurrence(obj); |
60 | minValue = minDeque.peekFirst(); |
61 | return minValue; |
62 | } |
63 | |
64 | @Override |
65 | public Aggregator createNewInstance() { |
66 | return new MinAggregatorInt(); |
67 | } |
68 | } |