/* A wrapper for some common sorting/searching/filtering methods etc, mostly using java.util.Collections */ public static class TransactionUtils { public static final int ASCENDING = 1; public static final int DESCENDING = -1; //pass in a List of Transactions and it returns a List of Map.Entrys mapping the unique subject name --> num times found in Transactions public static ArrayList countMajorCategories(ArrayList transactions) { Integer[] categories = new Integer[10]; Arrays.fill(categories, 0); for (int i = 0; i < transactions.size(); i++) { Transaction t = (Transaction) transactions.get(i); if(t.deweyClass.length() > 1) { Integer deweyClass = Integer.parseInt(t.deweyClass.substring(0, 1)); Integer cnt = (Integer)categories[deweyClass]; if (cnt == null) { categories[deweyClass] = new Integer(1); } else { categories[deweyClass] = new Integer(cnt + 1); } } } return new ArrayList(Arrays.asList(categories)); } public static ArrayList countMinorCategories(ArrayList transactions) { Map[] uniqueCategoriesToCount = new HashMap[10]; for(int i = 0; i < uniqueCategoriesToCount.length; i++) { uniqueCategoriesToCount[i] = new HashMap(); } for (int i = 0; i < transactions.size(); i++) { Transaction t = (Transaction) transactions.get(i); if(t.deweyClass.length() > 1) { Integer majorDeweyClass = Integer.parseInt(t.deweyClass.substring(0, 1)); String minorDeweyClass = t.deweyClass.substring(0, 3); Integer cnt = (Integer) uniqueCategoriesToCount[majorDeweyClass].get(minorDeweyClass); if (cnt == null) { uniqueCategoriesToCount[majorDeweyClass].put(minorDeweyClass, new Integer(1)); } else { uniqueCategoriesToCount[majorDeweyClass].put(minorDeweyClass, new Integer(cnt + 1)); } } } ArrayList c = new ArrayList(); for(int i = 0; i < uniqueCategoriesToCount.length; i++) { c.add(i, uniqueCategoriesToCount[i].entrySet()); } return c; } public static String getStringKeyForEntry(Object o) { Map.Entry me = (Map.Entry) o; return (String) me.getKey(); } public static int getIntValueForEntry(Object o) { Map.Entry me = (Map.Entry) o; return ((Integer) me.getValue()).intValue(); } //sort a list of Map.Entrys by their value. This assumes that the value is an Integer! //order = 1 for Ascending, order = -1 for descending. public static void sortUniqueEntriesByCount(ArrayList entryList, final int order) { Collections.sort(entryList, new Comparator() { public int compare(Object o1, Object o2) { Map.Entry me1 = (Map.Entry) o1; Map.Entry me2 = (Map.Entry) o2; return (((Integer)me1.getValue()).intValue() - ((Integer)me2.getValue()).intValue()) * order; } } ); } //sort a List of transactions by the title public static void sortTransactionsByTitle(ArrayList transactions) { Collections.sort(transactions, new Comparator() { public int compare(Object o1, Object o2) { Transaction t1 = (Transaction) o1; Transaction t2 = (Transaction) o2; return t1.title.compareToIgnoreCase(t2.title); } } ); } //reverse order of List public static void reverse(ArrayList transactions) { Collections.reverse(transactions); } public static void filterOutNonAlphabeticalTitles(ArrayList transactions) { for (int i = transactions.size() - 1; i >= 0; i--) { Transaction t = (Transaction) transactions.get(i); if (t.title.length() < 1) { transactions.remove(t); continue; } char firstLetter = t.title.charAt(0); if (! ((firstLetter >= 'a' && firstLetter <= 'z') || (firstLetter >= 'A' && firstLetter <= 'Z')) ) { transactions.remove(t); continue; } } } public static String getOrdinal(int value) { switch (value) { case 1: return "st"; case 2: return "nd"; case 3: return "rd"; default: return "th"; } } }