Apart from merging the 2 get methods, what else can I do to optimise this? Both input and output cannot be changed. Is there a way to iterate a map while writing to it?
// String condition - "cust" for customer group, "item" for itemGroup
public HashMap<String, ArrayList<PriceTable>> getPromoForViewGroup(
String condition) {
String custGroup = "custType";
String itemGroup = "itemType";
HashMap<String, ArrayList<PriceTable>> valueMap = new HashMap<String, ArrayList<PriceTable>>();
if (condition.equals(CUST)) {
getPromoForViewGroupHelperCust(custGroup, condition, valueMap);
}
if (condition.equals(ITEM)) {
getPromoForViewGroupHelperItem(itemGroup, condition, valueMap);
}
return valueMap;
}
private Map<String, ArrayList<PriceTable>> getPromoForViewGroupHelperItem(
String searchGroup, String condition,
Map<String, ArrayList<PriceTable>> valueMap) {
List<PriceTable> allList = new ArrayList<PriceTable>();
Cursor cursor = database.rawQuery("Select * from PriceTable where "
+ searchGroup + " IS NOT NULL", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
PriceTable pt = cursorToPriceTable(cursor);
if (!pt.getItemType().equals(""))
allList.add(pt);
cursor.moveToNext();
}
for (PriceTable pt1 : allList) {
ArrayList<PriceTable> set = valueMap.get(pt1.getItemType());
if (set == null) {
set = new ArrayList<PriceTable>();
set.add(pt1);
} else {
set.add(pt1);
}
valueMap.put(pt1.getItemType(), set);
}
cursor.close();
return valueMap;
}
private Map<String, ArrayList<PriceTable>> getPromoForViewGroupHelperCust(
String searchGroup, String condition,
Map<String, ArrayList<PriceTable>> valueMap) {
List<PriceTable> allList = new ArrayList<PriceTable>();
Cursor cursor = database.rawQuery("Select * from PriceTable where "
+ searchGroup + " IS NOT NULL", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
PriceTable pt = cursorToPriceTable(cursor);
if (!pt.getCustType().equals(""))
allList.add(pt);
cursor.moveToNext();
}
for (PriceTable pt1 : allList) {
ArrayList<PriceTable> set = valueMap.get(pt1.getCustType());
if (set == null) {
set = new ArrayList<PriceTable>();
set.add(pt1);
} else {
set.add(pt1);
}
valueMap.put(pt1.getCustType(), set);
}
cursor.close();
return valueMap;
}
private PriceTable cursorToPriceTable(Cursor cursor) {
PriceTable priceTable = new PriceTable();
priceTable.setPromoNum(cursor.getString(0));
priceTable.setPromoType(cursor.getString(1));
priceTable.setPromoRank(cursor.getString(2));
priceTable.setPromoName(cursor.getString(3));
priceTable.setItemType(cursor.getString(4));
priceTable.setItemCode(cursor.getString(5));
priceTable.setCustType(cursor.getString(6));
priceTable.setCustCode(cursor.getString(7));
priceTable.setStartDate(cursor.getString(8));
priceTable.setEndDate(cursor.getString(9));
priceTable.setPercent(cursor.getString(10));
priceTable.setSalesPrice(cursor.getString(11));
return priceTable;
}