| 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.in; |
| 19 | |
| 20 | import org.wso2.siddhi.core.event.AtomicEvent; |
| 21 | import org.wso2.siddhi.core.event.ComplexEvent; |
| 22 | import org.wso2.siddhi.core.event.ListEvent; |
| 23 | import org.wso2.siddhi.core.event.StreamEvent; |
| 24 | |
| 25 | import java.util.Arrays; |
| 26 | |
| 27 | /** |
| 28 | * Event with state |
| 29 | */ |
| 30 | public class StateEvent implements ComplexEvent, InStream, AtomicEvent { |
| 31 | |
| 32 | private int eventState = -1; |
| 33 | protected StreamEvent[] streamEvents; |
| 34 | |
| 35 | public StateEvent(StreamEvent[] streamEvents) { |
| 36 | this.streamEvents = streamEvents; |
| 37 | } |
| 38 | |
| 39 | public StreamEvent[] getStreamEvents() { |
| 40 | return streamEvents; |
| 41 | } |
| 42 | |
| 43 | public StreamEvent getStreamEvent(int i) { |
| 44 | return streamEvents[i]; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public long getTimeStamp() { |
| 49 | for (int i = streamEvents.length - 1; i >= 0; i--) { |
| 50 | StreamEvent streamEvent = streamEvents[i]; |
| 51 | if (streamEvent != null) { |
| 52 | return streamEvent.getTimeStamp(); |
| 53 | |
| 54 | } |
| 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String toString() { |
| 61 | return "StateEvent{" + |
| 62 | "eventState=" + eventState + |
| 63 | ", streamEvents=" + (streamEvents == null ? null : Arrays.asList(streamEvents)) + |
| 64 | '}'; |
| 65 | } |
| 66 | |
| 67 | public void setStreamEvent(int i, StreamEvent streamEvent) { |
| 68 | this.streamEvents[i] = streamEvent; |
| 69 | } |
| 70 | |
| 71 | protected StateEvent createCloneEvent(StreamEvent[] newEventStream, |
| 72 | int eventState) { |
| 73 | StateEvent stateEvent = new StateEvent(newEventStream); |
| 74 | stateEvent.setEventState(eventState); |
| 75 | return stateEvent; |
| 76 | } |
| 77 | |
| 78 | public StateEvent cloneEvent(int stateNumber) { |
| 79 | int length = streamEvents.length; |
| 80 | StreamEvent[] newEventStream = new StreamEvent[length]; |
| 81 | for (int i = 0; i < stateNumber; i++) { |
| 82 | StreamEvent streamEvent = streamEvents[i]; |
| 83 | if (streamEvent != null) { |
| 84 | if (streamEvent instanceof ListEvent) { |
| 85 | ((ListEvent) streamEvent).cloneEvent(); |
| 86 | } else { |
| 87 | newEventStream[i] = streamEvent; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | System.arraycopy(streamEvents, 0, newEventStream, 0, stateNumber); |
| 92 | return createCloneEvent(newEventStream, eventState); |
| 93 | } |
| 94 | |
| 95 | public int getEventState() { |
| 96 | return eventState; |
| 97 | } |
| 98 | |
| 99 | public void setEventState(int eventState) { |
| 100 | this.eventState = eventState; |
| 101 | } |
| 102 | } |