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 list, SingleBTS arfcn) { return contains(list, arfcn.ARFCN); } public static boolean contains(List list, int arfcn) { if (list == null || list.isEmpty()) { return false; } if (list instanceof LinkedList) { ListIterator 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 list, SingleBTS arfcn) { for (SingleBTS current : list) { if (current.ARFCN == arfcn.ARFCN && current.fullBTS) return true; } return false; } public static int countDL(LinkedList 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 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 list, SingleBTS arfcn) { return removeARFCN(list, arfcn.ARFCN); } public static SingleBTS removeARFCN(List list, int arfcn) { ListIterator 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 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 getAllARFCN(ArrayList list, int arfcn) { if (list == null || list.isEmpty()) { return null; } boolean aHitWasFound = false; ArrayList result = new ArrayList(); 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 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 removeNullElements(List list) { if (list == null) return null; ArrayList list2 = new ArrayList(); 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 list) { // get first element of list. // traverse list and delete every occurence of first element // get next element // copy list to destroy reference LinkedList list2 = new LinkedList(list); if (list2.isEmpty()) return null; // int count = 0; LinkedList output = new LinkedList(); 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 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 addMissing(LinkedList list1, LinkedList list2) { // LinkedList result = new LinkedList(); 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 generateAveragedList( ArrayList List, SingleBTS[] content) { ArrayList output = new ArrayList(); // 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 generateAveragedList( ArrayList List) { SingleBTS[] content = content(List); return generateAveragedList(List, content); } }