EMMA Coverage Report (generated Tue Jul 10 07:50:22 IST 2012)
[all classes][org.wso2.siddhi.core.stream.handler.window]

COVERAGE SUMMARY FOR SOURCE FILE [LengthWindowHandler.java]

nameclass, %method, %block, %line, %
LengthWindowHandler.java100% (1/1)100% (3/3)31%  (54/176)38%  (13.2/35)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class LengthWindowHandler100% (1/1)100% (3/3)31%  (54/176)38%  (13.2/35)
process (ComplexEvent): void 100% (1/1)25%  (40/162)30%  (9.2/31)
LengthWindowHandler (): void 100% (1/1)100% (6/6)100% (2/2)
setParameters (Object []): void 100% (1/1)100% (8/8)100% (2/2)

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*/
18package org.wso2.siddhi.core.stream.handler.window;
19 
20import org.wso2.siddhi.core.event.ComplexEvent;
21import org.wso2.siddhi.core.event.Event;
22import org.wso2.siddhi.core.event.ListEvent;
23import org.wso2.siddhi.core.event.StreamEvent;
24import org.wso2.siddhi.core.event.remove.RemoveEvent;
25import org.wso2.siddhi.core.event.remove.RemoveListEvent;
26import org.wso2.siddhi.core.util.SchedulerQueue;
27 
28public class LengthWindowHandler extends WindowHandler {
29 
30    int lengthToKeep;
31    private volatile int currentLength = 0;
32 
33    @Override
34    public void setParameters(Object[] parameters) {
35        lengthToKeep = (Integer) parameters[0];
36    }
37 
38    @Override
39    public void process(ComplexEvent complexEvent) {
40        try {
41            if (complexEvent instanceof Event) {
42                SchedulerQueue<StreamEvent> queue = getWindow();
43                queue.put((StreamEvent) complexEvent);
44                getNextPreStreamFlowProcessor().process(complexEvent);
45                if (currentLength == lengthToKeep) {
46                    getNextPreStreamFlowProcessor().process(new RemoveEvent((Event) queue.take(),System.currentTimeMillis()));
47                }else {
48                    currentLength++;
49                }
50            } else if (complexEvent instanceof ListEvent) {
51                SchedulerQueue<StreamEvent> queue = getWindow();
52                Event[] newEvents = ((ListEvent) complexEvent).getEvents();
53                int oldEventLength = newEvents.length - (lengthToKeep - currentLength);
54                if (oldEventLength > 0) {
55                    Event[] oldEvents = new Event[newEvents.length - (lengthToKeep - currentLength)];
56                    int oldEventIndex = 0;
57                    for (Event event : newEvents) {
58                        queue.put(event);
59                        getNextPreStreamFlowProcessor().process(event);
60                        if (currentLength == lengthToKeep) {
61                            oldEvents[oldEventIndex] = new RemoveEvent((Event) queue.take(),System.currentTimeMillis());
62                            oldEventIndex++;
63                        } else {
64                            currentLength++;
65                        }
66                    }
67                    getNextPreStreamFlowProcessor().process(new RemoveListEvent(oldEvents,System.currentTimeMillis()));
68                } else {
69                    for (Event event : newEvents) {
70                        queue.put(event);
71                        getNextPreStreamFlowProcessor().process(event);
72                        currentLength++;
73                    }
74                }
75            }
76        } catch (InterruptedException e) {
77            e.printStackTrace();
78        }
79    }
80 
81}

[all classes][org.wso2.siddhi.core.stream.handler.window]
EMMA 2.1.5320 (stable) (C) Vladimir Roubtsov