summaryrefslogtreecommitdiffstats
path: root/helper/ListBTS.java
blob: dac45b0c065d801a1411516bac9bc2f16681b7d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package helper;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import DataStructure.SingleBTS;

public class ListBTS {

	/**
	 * 
	 * @param list
	 *            Where to search
	 * @param arfcn
	 *            which BTS to search
	 * @return false if BTS not found or list or arfcn is null or empty. True if
	 *         BTS is found
	 */
	public static boolean contains(List<SingleBTS> list, SingleBTS arfcn) {
		return contains(list, arfcn.ARFCN);
	}

	public static boolean contains(List<SingleBTS> list, int arfcn) {
		if (list == null || list.isEmpty()) {
			return false;
		}
		if (list instanceof LinkedList<?>) {
			ListIterator<SingleBTS> itr = list.listIterator();
			while (itr.hasNext()) {
				SingleBTS element = itr.next();
				if (element.ARFCN == arfcn)
					return true;
			}
		} else {
			for (int i = list.size() - 1; i >= 0; i--) {
				if (list.get(i).ARFCN == arfcn) {
					return true;
				}
			}
		}

		return false;
	}

	public static boolean containsUL(List<SingleBTS> list, SingleBTS arfcn) {
		for (SingleBTS current : list) {
			if (current.ARFCN == arfcn.ARFCN && current.fullBTS)
				return true;
		}
		return false;
	}

	public static int countDL(LinkedList<SingleBTS> list, SingleBTS arfcn) {
		int count = 0;
		for (SingleBTS current : list) {
			if (current != null && current.ARFCN == arfcn.ARFCN)
				count++;
		}
		return count;
	}

	public static int countUL(LinkedList<SingleBTS> list, SingleBTS arfcn) {
		int count = 0;
		for (SingleBTS current : list) {
			if (current != null && current.fullBTS
					&& current.ARFCN == arfcn.ARFCN)
				count++;
		}
		return count;
	}

	/**
	 * Searches the list for the _first_ BTS with same arfcn. Removes it from
	 * that list and returns the element
	 * 
	 * @param list
	 *            from which to search
	 * @param arfcn
	 *            MR with ARFCN you search for
	 * @return Element from list that got deleted. Null if element is not
	 *         present in list
	 */
	public static SingleBTS removeARFCN(List<SingleBTS> list, SingleBTS arfcn) {
		return removeARFCN(list, arfcn.ARFCN);
	}

	public static SingleBTS removeARFCN(List<SingleBTS> list, int arfcn) {
		ListIterator<SingleBTS> itr = list.listIterator();
		while (itr.hasNext()) {
			SingleBTS element = itr.next();
			if (element.ARFCN == arfcn) {
				itr.remove();
				return element;
			}
		}
		return null;
	}

	/**
	 * Gets the first occurence of the specified arfcn from the list. Element
	 * stays in list
	 * 
	 * @param list
	 *            the list from where to extract the BTS
	 * @param arfcn
	 *            arfcn to extract
	 * @return first SingleBTS that matches from the list. Null if Element is
	 *         not present
	 */
	public static SingleBTS getARFCN(List<SingleBTS> list, SingleBTS arfcn) {
		if (list == null || arfcn == null) {
			return null;
		}
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).ARFCN == arfcn.ARFCN) {
				return list.get(i);
			}
		}
		return null;
	}

	public static ArrayList<SingleBTS> getAllARFCN(ArrayList<SingleBTS> list,
			int arfcn) {
		if (list == null || list.isEmpty()) {
			return null;
		}
		boolean aHitWasFound = false;
		ArrayList<SingleBTS> result = new ArrayList<SingleBTS>();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).ARFCN == arfcn) {
				result.add(list.get(i));
				aHitWasFound = true;
			}
		}
		if (aHitWasFound)
			return result;
		else
			return null;
	}

	/**
	 * Gets the first occurence of the specified arfcn from the list. Element
	 * stays in list
	 * 
	 * @param list
	 *            the list from where to extract the BTS
	 * @param arfcn
	 *            arfcn to extract
	 * @return first SingleBTS that matches from the list. Null if Element is
	 *         not present
	 */
	public static SingleBTS getARFCN(List<SingleBTS> list, int arfcn) {
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).ARFCN == arfcn) {
				return list.get(i);
			}
		}
		return null;
	}

	/**
	 * Takes a list. Returns a copy of that list without null-elements
	 * 
	 * @param list
	 *            ListBTS where all elements with null will be filtered out
	 * @return a copy of this list without null-elements. Null if the whole list
	 *         is null
	 */
	public static ArrayList<SingleBTS> removeNullElements(List<SingleBTS> list) {
		if (list == null)
			return null;
		ArrayList<SingleBTS> list2 = new ArrayList<SingleBTS>();
		boolean element_present = false;
		for (SingleBTS element : list) {
			if (element != null) {
				element_present = true;
				list2.add(element);
			}
		}
		if (element_present)
			return list2;
		else
			return null;
	}

	/**
	 * ListBTS all the unique SingleBTS's that occur in this list
	 * 
	 * @param list
	 *            ListBTS with SingleBTS's
	 * @return Array with the unique SingleBTS's. That is, every BTS in this
	 *         Array occurs also in the list. Null if the list was empty
	 */
	public static SingleBTS[] content(List<SingleBTS> list) {
		// get first element of list.
		// traverse list and delete every occurence of first element
		// get next element
		// copy list to destroy reference
		LinkedList<SingleBTS> list2 = new LinkedList<SingleBTS>(list);
		if (list2.isEmpty())
			return null;
		// int count = 0;
		LinkedList<SingleBTS> output = new LinkedList<SingleBTS>();
		while (!list2.isEmpty()) {
			SingleBTS bts = list2.getFirst();
			list2.removeFirst();
			output.add(bts);
			// remove the just added bts from the BTS-list
			// speedup with listiterator
			/*
			 * for (int i = 0; i < list2.size(); i++) { if (list2.get(i).ARFCN
			 * == bts.ARFCN) { list2.remove(i); i--; }
			 */
			ListIterator<SingleBTS> itr = list2.listIterator();
			SingleBTS testelement;
			while (itr.hasNext()) {
				testelement = itr.next();
				if (testelement.ARFCN == bts.ARFCN) {
					// same element found. Delete it
					itr.remove();
					// if we are on the first element, itr.previous will throw
					// an exception. Just ignore it and go on
					try {
						itr.previous();
					} catch (Exception e) {

					}
				}

			}

		}
		return output.toArray(new SingleBTS[1]);
	}

	/**
	 * Takes two lists. Merges missing parameters. Interpolated BTS will be
	 * overwritten by real measurements. NOT IMPLEMENTED!!!!
	 * 
	 * @param list1
	 * @param list2
	 * @return
	 */
	public static LinkedList<SingleBTS> addMissing(LinkedList<SingleBTS> list1,
			LinkedList<SingleBTS> list2) {
		// LinkedList<SingleBTS> result = new LinkedList<SingleBTS>();
		if ((list1 == null && list2 != null)) {
			return list2;
		}
		if (list1 != null && list2 == null) {
			return list1;
		}
		if (list1.isEmpty() && !list2.isEmpty()) {
			return list2;
		}
		if (!list1.isEmpty() && list2.isEmpty()) {
			return list1;
		}

		// for (SingleBTS element1 : list1) {
		// for (SingleBTS element2 : list2) {
		// TODO: Code here!
		// }
		// }
		return null;
	}

	/**
	 * Takes all BTS with same arfcn and adds the Measurements together
	 * 
	 * @param List
	 * @param content
	 * @return
	 */
	public static ArrayList<SingleBTS> generateAveragedList(
			ArrayList<SingleBTS> List, SingleBTS[] content) {
		ArrayList<SingleBTS> output = new ArrayList<SingleBTS>();
		// traverse every unique BTS
		for (SingleBTS singleBTS : content) {
			// initialize with ARFCN and Name
			SingleBTS current = new SingleBTS(singleBTS.ARFCN, singleBTS.name);
			// add everything from list. SingleBTS-Class takes only correct BTSs
			for (SingleBTS measure : List) {
				current.addBTSMeasure(measure);
			}
			output.add(current);
		}

		return output;
	}

	public static ArrayList<SingleBTS> generateAveragedList(
			ArrayList<SingleBTS> List) {
		SingleBTS[] content = content(List);
		return generateAveragedList(List, content);
	}

}