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 package org.perfidix.element;
28
29 import java.util.ArrayList;
30 import java.util.Comparator;
31 import java.util.Hashtable;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Set;
36 import java.util.TreeSet;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public final class SequentialMethodArrangement extends AbstractMethodArrangement {
57
58
59
60
61
62
63 protected SequentialMethodArrangement(final List<BenchmarkElement> elements) {
64 super(elements);
65 }
66
67
68 @Override
69 protected List<BenchmarkElement> arrangeList(final List<BenchmarkElement> elements) {
70 final Map<BenchmarkMethod, ArrayList<BenchmarkElement>> table =
71 new Hashtable<BenchmarkMethod, ArrayList<BenchmarkElement>>();
72 final List<BenchmarkElement> returnVal = new ArrayList<BenchmarkElement>();
73
74
75 for (final BenchmarkElement elem : elements) {
76 if (!table.containsKey(elem.getMeth())) {
77 table.put(elem.getMeth(), new ArrayList<BenchmarkElement>());
78 }
79 table.get(elem.getMeth()).add(elem);
80 }
81
82
83 int numberOfElements = 0;
84 for (final BenchmarkMethod meth : table.keySet()) {
85 numberOfElements = numberOfElements + table.get(meth).size();
86 }
87
88
89 final Set<Entry<BenchmarkMethod, ArrayList<BenchmarkElement>>> compareMethods =
90 new TreeSet<Entry<BenchmarkMethod, ArrayList<BenchmarkElement>>>(new BenchmarkElementComparator());
91 for (final Entry<BenchmarkMethod, ArrayList<BenchmarkElement>> entry : table.entrySet()) {
92 compareMethods.add(entry);
93 }
94
95 final ArrayList<BenchmarkMethod> methods = new ArrayList<BenchmarkMethod>();
96 for (Entry<BenchmarkMethod, ArrayList<BenchmarkElement>> entry : compareMethods) {
97 methods.add(entry.getKey());
98 }
99
100 for (int i = 0; i < numberOfElements; i++) {
101 BenchmarkElement elem = null;
102 int indexPart = 0;
103 while (elem == null) {
104 final int index = (i + indexPart) % methods.size();
105 final BenchmarkMethod methodToInclude = methods.get(index);
106 if (table.containsKey(methodToInclude)) {
107 elem = table.get(methodToInclude).remove(0);
108 if (table.get(methodToInclude).size() == 0) {
109 table.remove(methodToInclude);
110 }
111 }
112 indexPart++;
113 }
114 returnVal.add(elem);
115 }
116 return returnVal;
117 }
118
119
120
121
122
123
124
125 class BenchmarkElementComparator implements
126 Comparator<Entry<BenchmarkMethod, ArrayList<BenchmarkElement>>> {
127
128
129 public int compare(final Entry<BenchmarkMethod, ArrayList<BenchmarkElement>> object1,
130 final Entry<BenchmarkMethod, ArrayList<BenchmarkElement>> object2) {
131 int returnVal = 0;
132 if (object1.getValue().size() > object2.getValue().size()) {
133 returnVal = -1;
134 } else {
135 returnVal = 1;
136 }
137 return returnVal;
138 }
139
140 }
141
142 }