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.event; |
19 | |
20 | import org.wso2.siddhi.query.api.utils.SiddhiConstants; |
21 | |
22 | import java.util.Arrays; |
23 | |
24 | public abstract class ListEvent implements StreamEvent { |
25 | |
26 | Event[] events; |
27 | private int activeEvents = 0; |
28 | private int currentMaxSize = 0; |
29 | private boolean unlimited = false; |
30 | |
31 | public ListEvent(int maxEvents) { |
32 | if (maxEvents != SiddhiConstants.UNLIMITED) { |
33 | this.events = new Event[maxEvents]; |
34 | currentMaxSize = maxEvents; |
35 | } else { |
36 | this.events = new Event[10]; |
37 | currentMaxSize = 10; |
38 | this.unlimited = true; |
39 | } |
40 | } |
41 | |
42 | protected ListEvent(Event[] events, int activeEvents, boolean unlimited) { |
43 | this.events = events; |
44 | this.activeEvents = activeEvents; |
45 | this.unlimited = unlimited; |
46 | } |
47 | |
48 | public ListEvent(Event[] events) { |
49 | this.events = events; |
50 | this.activeEvents = events.length; |
51 | this.unlimited = false; |
52 | } |
53 | |
54 | public Event[] getEvents() { |
55 | return events; |
56 | } |
57 | |
58 | public Event getEvent(int i) { |
59 | return events[i]; |
60 | } |
61 | |
62 | @Override |
63 | public String toString() { |
64 | return "SingleEventList{" + |
65 | "events=" + (events == null ? null : Arrays.asList(events)) + |
66 | '}'; |
67 | } |
68 | |
69 | @Override |
70 | public long getTimeStamp() { |
71 | return events[activeEvents - 1].getTimeStamp(); |
72 | } |
73 | |
74 | public boolean addEvent(Event event) { |
75 | if (currentMaxSize == activeEvents) { |
76 | if (unlimited) { |
77 | Event[] newEvents = new Event[activeEvents + 10]; |
78 | currentMaxSize+=10; |
79 | System.arraycopy(events, 0, newEvents, 0, events.length); |
80 | events = newEvents; |
81 | } else { |
82 | return false; |
83 | } |
84 | } |
85 | this.events[activeEvents] = event; |
86 | activeEvents++; |
87 | return true; |
88 | } |
89 | |
90 | public int getActiveEvents() { |
91 | return activeEvents; |
92 | } |
93 | |
94 | |
95 | public void removeLast() { |
96 | activeEvents--; |
97 | events[activeEvents] = null; |
98 | } |
99 | |
100 | public StreamEvent cloneEvent() { |
101 | int length = events.length; |
102 | Event[] newEvents = new Event[length]; |
103 | System.arraycopy(newEvents, 0, newEvents, 0, activeEvents); |
104 | return createEventClone(newEvents,activeEvents,unlimited); |
105 | |
106 | |
107 | } |
108 | |
109 | protected abstract ListEvent createEventClone(Event[] newEvents, int activeEvents, |
110 | boolean unlimited) ; |
111 | |
112 | public void setEvents(Event[] events) { |
113 | this.events = events; |
114 | this.activeEvents = events.length; |
115 | this.unlimited = false; |
116 | } |
117 | } |