summaryrefslogtreecommitdiffstats
path: root/src/org/jetbrains/java/decompiler/util/SortUtil.java
blob: e227e02cbf110fc29abdddae9e795386c70ccbf1 (plain)
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
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<Indexed> def_dec = new ArrayList<Indexed>();
        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<Indexed>()
            {
                @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 <T extends Comparable> Iterator<T> sortComparable(Iterator<T> itr)
    {
        List<T> list = new ArrayList<T>();
    
        while (itr.hasNext())
            list.add(itr.next());
    
        Collections.sort(list);
        return list.iterator();
    }
}