package org.jetbrains.java.decompiler.util; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; public class SortUtil { public static interface Indexed { public int getSortIndex(); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static Iterator sortIndexed(Iterator itr) { List list = new ArrayList(); List def_dec = new ArrayList(); int first = -1; while(itr.hasNext()) { Object i = itr.next(); //Split off any default variable declarations and sort them. if (i instanceof Indexed && ((Indexed)i).getSortIndex() >= 0) { if (first == -1) first = list.size(); def_dec.add((Indexed)i); } else { list.add(i); } } if (def_dec.size() > 0) { Collections.sort(def_dec, new Comparator() { @Override public int compare(Indexed o1, Indexed o2) { return o1.getSortIndex() - o2.getSortIndex(); } }); list.addAll(first, def_dec); } return list.iterator(); } @SuppressWarnings({ "unchecked", "rawtypes" }) public static Iterator sortComparable(Iterator itr) { List list = new ArrayList(); while (itr.hasNext()) list.add(itr.next()); Collections.sort(list); return list.iterator(); } }