View Javadoc

1   package org.apache.helix.alerts;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Iterator;
23  import java.util.List;
24  
25  public abstract class Operator {
26  	
27  	public int minInputTupleLists;
28  	public int maxInputTupleLists;
29  	public int numOutputTupleLists = -1;
30  	public boolean inputOutputTupleListsCountsEqual = false;
31  	
32  	public Operator()
33  	{
34  		
35  	}
36  	
37  	public Tuple<String> multiplyTuples(Tuple<String> tup1, Tuple<String> tup2)
38  	{
39  		if (tup1 == null) {
40  			return tup2;
41  		}
42  		if (tup2 == null) {
43  			return tup1;
44  		}
45  		Tuple<String>outputTup = new Tuple<String>();
46  		
47  
48  		//sum staggers if the tuples are same length
49  		//e.g. 1,2,3 + 4,5 = 1,6,8
50  		//so this is a bit tricky
51  		Tuple<String>largerTup;
52  		Tuple<String>smallerTup;
53  		if (tup1.size() >= tup2.size()) {
54  			largerTup = tup1;
55  			smallerTup = tup2;
56  		}
57  		else {
58  			largerTup = tup2;
59  			smallerTup = tup1;
60  		}		
61  		int gap = largerTup.size() - smallerTup.size();
62  		
63  		for (int i=0; i< largerTup.size();i++) {
64  			if (i < gap) {
65  				outputTup.add(largerTup.getElement(i));
66  			}
67  			else {
68  				double elementProduct = 0;
69  				elementProduct = Double.parseDouble(largerTup.getElement(i)) *
70  						Double.parseDouble(smallerTup.getElement(i-gap));
71  				outputTup.add(String.valueOf(elementProduct));
72  			}
73  		}
74  		return outputTup;
75  	}
76  	
77  	public Tuple<String> sumTuples(Tuple<String> tup1, Tuple<String> tup2)
78  	{
79  		if (tup1 == null) {
80  			return tup2;
81  		}
82  		if (tup2 == null) {
83  			return tup1;
84  		}
85  		Tuple<String>outputTup = new Tuple<String>();
86  		
87  
88  		//sum staggers if the tuples are same length
89  		//e.g. 1,2,3 + 4,5 = 1,6,8
90  		//so this is a bit tricky
91  		Tuple<String>largerTup;
92  		Tuple<String>smallerTup;
93  		if (tup1.size() >= tup2.size()) {
94  			largerTup = tup1;
95  			smallerTup = tup2;
96  		}
97  		else {
98  			largerTup = tup2;
99  			smallerTup = tup1;
100 		}		
101 		int gap = largerTup.size() - smallerTup.size();
102 		
103 		for (int i=0; i< largerTup.size();i++) {
104 			if (i < gap) {
105 				outputTup.add(largerTup.getElement(i));
106 			}
107 			else {
108 				double elementSum = 0;
109 				elementSum = Double.parseDouble(largerTup.getElement(i)) +
110 						Double.parseDouble(smallerTup.getElement(i-gap));
111 				outputTup.add(String.valueOf(elementSum));
112 			}
113 		}
114 		return outputTup;
115 	}
116 	
117 	public abstract List<Iterator<Tuple<String>>> execute(List<Iterator<Tuple<String>>> input);
118 }
119