aboutsummaryrefslogtreecommitdiffstats
path: root/rba.tool.core/src/rba/tool/core/sort/ValueSortedList.java
blob: 25ab560d1e3d6b132a234a7cb8167da0d0645574 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package rba.tool.core.sort;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import rba.core.ComparisonAnd;
import rba.core.ComparisonOperator;
import rba.core.EqualToOperator;
import rba.core.Expression;
import rba.core.GreaterThanOperator;
import rba.core.MuchGreaterThanOperator;
import rba.core.RuleObject;
import rba.core.ValueExpression;

abstract public class ValueSortedList<T extends RuleObject> {
    private List<ValueGroup<T>> list;

    private Collection<T> unsorted;

    public ValueSortedList(Collection<T> originalList) {
        list = new ArrayList<ValueGroup<T>>();
        list.add(new ValueGroup<T>("MIN"));
        list.add(new ValueGroup<T>("1"));
        list.add(new ValueGroup<T>("MAX"));
        unsorted = new ArrayList<T>(originalList);
        sort();
    }

    public void add(T o) {
        unsorted.add(o);
        sort();
    }

    public void addAll(Collection<T> list) {
        unsorted.addAll(list);
        sort();
    }

    public void sort() {
        list.get(0).getMember().addAll(unsorted);
        unsorted.clear();
        boolean trysort = true;
        int retry = 0;
        while (trysort) {
            trysort = false;
            retry = retry + 1;
            if (retry > 100) {
                throw new RuntimeException("Could not sort value [" + list + "]");
            }
            for (T i : sortedList()) {
                int currentIndex = this.getGroupIndexOf(i);
                Expression e = this.getValueExpression(i);
                if (e == null)
                    continue;
                if (e instanceof ValueExpression) {
                    int index = this.getGroupIndexOf(e.getExpression());
                    if (index == -1) {
                        index = ((ValueExpression) e).getExpressionValue();

                        if (index == -1) {
                            index = 0;
                        }
                        if (index >= list.size() - 1) {
                            list.get(currentIndex).remove(i);
                            addGroupAt(index, i);
                            trysort = true;
                            continue;
                        }
                    }
                    if (index != currentIndex) {
                        list.get(currentIndex).remove(i);
                        list.get(index).add(i);
                        trysort = true;
                    } else {
                    }
                } else if (e instanceof ComparisonOperator) {
                    ComparisonOperatorEvaluator<T> evaluator = createEvaluator((ComparisonOperator) e);
                    int index = evaluator.suggestIndex(this);
                    if (index < 0)
                        continue;
                    if (index >= list.size() - 1) {
                        list.get(currentIndex).remove(i);
                        addGroupAt(index, i);
                        trysort = true;
                    } else if (index != currentIndex) {
                        list.get(currentIndex).remove(i);
                        list.get(index).add(i);
                        trysort = true;
                    } else {
                    }
                } else {
                    throw new RuntimeException("Unsupported expression [" + e.getExpression() + "]");
                }
            }
        }
    }

    private void addGroupAt(int index, T e) {

        if (index >= list.size() - 1) {
            for (int i = list.size() - 1; i <= index; ++i) {
                ValueGroup<T> newGroup = new ValueGroup<T>(Integer.toString(i));
                list.add(i, newGroup);
            }
        }

        list.get(index).add(e);

    }

    public List<T> sortedList() {
        List<T> result = new ArrayList<T>();
        for (ValueGroup<T> i : list) {
            result.addAll(i.getMember());
        }
        return result;
    }

    public List<ValueGroup<T>> getGroupList() {
        return list;
    }

    public List<T> sortedListByBig() {
        List<T> result = new ArrayList<T>();
        for (int index = list.size() - 1; index >= 0; index--) {
            ValueGroup<T> i = list.get(index);
            result.addAll(i.getMember());
        }
        return result;
    }

    abstract protected Expression getValueExpression(T e);

    public int getGroupIndexOf(T e) {
        for (ValueGroup<T> i : list) {
            if (i.getMember().contains(e)) {
                return list.indexOf(i);
            }
        }
        return -1;
    }

    public int getGroupIndexOf(String groupName) {
        for (ValueGroup<T> i : list) {
            if (i.getName().contentEquals(groupName)) {
                return list.indexOf(i);
            }
        }
        return -1;
    }

    public ComparisonOperatorEvaluator<T> createEvaluator(ComparisonOperator e) {
        ComparisonOperatorEvaluator<T> evaluator;
        if (e instanceof GreaterThanOperator) {
            evaluator = new GreaterThanOperatorEvaluator<T>((GreaterThanOperator) e);
        } else if (e instanceof MuchGreaterThanOperator) {
            evaluator = new MuchGreaterThanOperatorEvaluator<T>((MuchGreaterThanOperator) e);
        } else if (e instanceof ComparisonAnd) {
            evaluator = new ComparisonAndEvaluator<T>((ComparisonAnd) e);
        } else if (e instanceof EqualToOperator) {
            evaluator = new EqualToOperatorEvaluator<T>((EqualToOperator) e);
        } else {
            throw new RuntimeException("Unsupported expression [" + e.getExpression() + "]");
        }
        return evaluator;
    }
}