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.query.api.definition; |
19 | |
20 | import org.wso2.siddhi.query.api.ExecutionPlan; |
21 | import org.wso2.siddhi.query.api.exception.AttributeAlreadyExist; |
22 | |
23 | import java.util.ArrayList; |
24 | import java.util.List; |
25 | |
26 | public class StreamDefinition implements ExecutionPlan { |
27 | |
28 | String streamId; |
29 | List<Attribute> attributeList = new ArrayList<Attribute>(); |
30 | |
31 | public StreamDefinition name(String streamId) { |
32 | this.streamId = streamId; |
33 | return this; |
34 | } |
35 | |
36 | public StreamDefinition attribute(String attributeName, Attribute.Type type) { |
37 | checkAttribute(attributeName); |
38 | this.attributeList.add(new Attribute(attributeName, type)); |
39 | return this; |
40 | } |
41 | |
42 | private void checkAttribute(String attributeName) { |
43 | for (Attribute attribute : attributeList) { |
44 | if (attribute.getName().equals(attributeName)) { |
45 | throw new AttributeAlreadyExist(attributeName + " is already defined for with type " + attribute.getType() + " for " + streamId); |
46 | } |
47 | } |
48 | } |
49 | |
50 | public String getStreamId() { |
51 | return streamId; |
52 | } |
53 | |
54 | public List<Attribute> getAttributeList() { |
55 | return attributeList; |
56 | } |
57 | |
58 | public Attribute.Type getAttributeType(String attributeName) { |
59 | for (Attribute attribute : attributeList) { |
60 | if (attribute.getName().equals(attributeName)) { |
61 | return attribute.getType(); |
62 | } |
63 | } |
64 | return null; //todo through exception |
65 | } |
66 | |
67 | public int getAttributePosition(String attributeName) { |
68 | for (int i = 0, attributeListSize = attributeList.size(); i < attributeListSize; i++) { |
69 | Attribute attribute = attributeList.get(i); |
70 | if (attribute.getName().equals(attributeName)) { |
71 | return i; |
72 | } |
73 | } |
74 | return 0; //todo through exception |
75 | } |
76 | |
77 | @Override |
78 | public String toString() { |
79 | return "StreamDefinition{" + |
80 | "streamId='" + streamId + '\'' + |
81 | ", attributeList=" + attributeList + |
82 | '}'; |
83 | } |
84 | } |