| 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.max; |
| 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 MaxAggregatorLong implements Aggregator { |
| 28 | |
| 29 | private Deque<Long> maxDeque = new LinkedList<Long>(); |
| 30 | private volatile Long maxValue = null; |
| 31 | private Attribute.Type type = Attribute.Type.LONG; |
| 32 | |
| 33 | public Object getValue() { |
| 34 | return maxValue; |
| 35 | } |
| 36 | |
| 37 | public Attribute.Type getType() { |
| 38 | return this.type; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public synchronized Object add(Object obj) { |
| 43 | Long value = ((Long) obj); |
| 44 | for (Iterator<Long> iterator = maxDeque.descendingIterator(); iterator.hasNext(); ) { |
| 45 | if (iterator.next() < value) { |
| 46 | iterator.remove(); |
| 47 | } else { |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | maxDeque.addLast(value); |
| 52 | if (maxValue < value) { |
| 53 | maxValue = value; |
| 54 | } |
| 55 | return maxValue; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public synchronized Object remove(Object obj) { |
| 60 | maxDeque.removeFirstOccurrence(obj); |
| 61 | maxValue = maxDeque.peekFirst(); |
| 62 | return maxValue; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Aggregator createNewInstance() { |
| 67 | return new MaxAggregatorLong(); |
| 68 | } |
| 69 | } |