analysis: support for sorting of control flow view columns
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / internal / analysis / os / linux / ui / views / controlflow / ControlFlowColumnComparators.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow;
10
11 import java.util.Comparator;
12 import java.util.List;
13
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.tracecompass.tmf.ui.views.timegraph.ITimeGraphEntryComparator;
17 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
18
19 import com.google.common.collect.ImmutableList;
20
21 /**
22 *
23 * Class with comparators used for sorting the ControlFlowEntries based based on
24 * the column selection.
25 *
26 * @author Bernd Hufmann
27 *
28 */
29 public final class ControlFlowColumnComparators {
30
31 /**
32 * Process Name comparator. This compares first the trace, then the process name, then the
33 * birth time, then the TID finally the parent TID.
34 */
35 public static ITimeGraphEntryComparator PROCESS_NAME_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() {
36 private final List<Comparator<ITimeGraphEntry>> SECONDARY_COMPARATORS = init();
37 private int fDirection = SWT.DOWN;
38
39 @Override
40 public int compare(@Nullable ITimeGraphEntry o1, @Nullable ITimeGraphEntry o2) {
41 /* First sort by trace */
42 int result = compareTrace(fDirection, o1, o2);
43
44 if (result == 0) {
45 /* Then sort by process name */
46 result = IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR.compare(o1, o2);
47 }
48 return compareList(result, fDirection, SECONDARY_COMPARATORS, o1, o2);
49 }
50
51 @Override
52 public void setDirection(int direction) {
53 fDirection = direction;
54 }
55
56 private List<Comparator<ITimeGraphEntry>> init() {
57 ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder();
58 builder.add(IControlFlowEntryComparator.BIRTH_TIME_COMPARATOR)
59 .add(IControlFlowEntryComparator.TID_COMPARATOR)
60 .add(IControlFlowEntryComparator.PTID_COMPARATOR);
61 return builder.build();
62 }
63 };
64
65 /**
66 * Process TID comparator. This compares first the trace, then the process TID, then the
67 * birth time, then the process name finally the parent TID.
68 */
69 public static ITimeGraphEntryComparator TID_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() {
70
71 private final List<Comparator<ITimeGraphEntry>> SECONDARY_COMPARATORS = init();
72 private int fDirection = SWT.DOWN;
73
74 @Override
75 public int compare(@Nullable ITimeGraphEntry o1, @Nullable ITimeGraphEntry o2) {
76 /* First sort by trace */
77 int result = compareTrace(fDirection, o1, o2);
78
79 if (result == 0) {
80 /* Then sort by TID */
81 result = IControlFlowEntryComparator.TID_COMPARATOR.compare(o1, o2);
82 }
83 return compareList(result, fDirection, SECONDARY_COMPARATORS, o1, o2);
84 }
85
86 @Override
87 public void setDirection(int direction) {
88 fDirection = direction;
89 }
90
91 private List<Comparator<ITimeGraphEntry>> init() {
92 ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder();
93 builder.add(IControlFlowEntryComparator.BIRTH_TIME_COMPARATOR)
94 .add(IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR)
95 .add(IControlFlowEntryComparator.PTID_COMPARATOR);
96 return builder.build();
97 }
98
99 };
100
101 /**
102 * Process PTID comparator. This compares first the trace, then the process
103 * parent TID, then the birth time, then the process name finally the TID.
104 */
105 public static ITimeGraphEntryComparator PTID_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() {
106
107 private final List<Comparator<ITimeGraphEntry>> SECONDARY_COMPARATORS = init();
108 private int fDirection = SWT.DOWN;
109
110 @Override
111 public int compare(@Nullable ITimeGraphEntry o1, @Nullable ITimeGraphEntry o2) {
112 /* First sort by trace */
113 int result = compareTrace(fDirection, o1, o2);
114 if (result == 0) {
115 /* Then sort by PTID */
116 result = IControlFlowEntryComparator.PTID_COMPARATOR.compare(o1, o2);
117 }
118 return compareList(result, fDirection, SECONDARY_COMPARATORS, o1, o2);
119 }
120
121 @Override
122 public void setDirection(int direction) {
123 fDirection = direction;
124 }
125
126 private List<Comparator<ITimeGraphEntry>> init() {
127 ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder();
128 builder.add(IControlFlowEntryComparator.BIRTH_TIME_COMPARATOR)
129 .add(IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR)
130 .add(IControlFlowEntryComparator.TID_COMPARATOR);
131 return builder.build();
132 }
133 };
134
135 /**
136 * Process birth time comparator. This compares first the trace, then the
137 * birth time, then the process name, then the TID finally the parent TID.
138 */
139 public static ITimeGraphEntryComparator BIRTH_TIME_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() {
140 private final List<Comparator<ITimeGraphEntry>> SECONDARY_COMPARATORS = init();
141 private int fDirection = SWT.DOWN;
142
143 @Override
144 public int compare(@Nullable ITimeGraphEntry o1, @Nullable ITimeGraphEntry o2) {
145
146 int result = compareTrace(fDirection, o1, o2);
147
148 if (result == 0) {
149 /* Sort all child processes according to birth time. */
150 result = IControlFlowEntryComparator.BIRTH_TIME_COMPARATOR.compare(o1, o2);
151 }
152 return compareList(result, fDirection, SECONDARY_COMPARATORS, o1, o2);
153 }
154
155 @Override
156 public void setDirection(int direction) {
157 fDirection = direction;
158 }
159
160 private List<Comparator<ITimeGraphEntry>> init() {
161 ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder();
162 builder.add(IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR)
163 .add(IControlFlowEntryComparator.TID_COMPARATOR)
164 .add(IControlFlowEntryComparator.PTID_COMPARATOR);
165 return builder.build();
166 }
167 };
168
169 /**
170 * Trace comparator. This compares first the trace, then the process birth
171 * time, the process name, the process TID and finally the process's parent
172 * TID.
173 */
174 public static ITimeGraphEntryComparator TRACE_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() {
175 private final List<Comparator<ITimeGraphEntry>> SECONDARY_COMPARATORS = init();
176 private int fDirection = SWT.DOWN;
177
178 @Override
179 public int compare(@Nullable ITimeGraphEntry o1, @Nullable ITimeGraphEntry o2) {
180 int result = IControlFlowEntryComparator.TRACE_COMPARATOR.compare(o1, o2);
181 return compareList(result, fDirection, SECONDARY_COMPARATORS, o1, o2);
182 }
183
184 @Override
185 public void setDirection(int direction) {
186 fDirection = direction;
187 }
188
189 private List<Comparator<ITimeGraphEntry>> init() {
190 ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder();
191 builder.add(IControlFlowEntryComparator.BIRTH_TIME_COMPARATOR)
192 .add(IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR)
193 .add(IControlFlowEntryComparator.TID_COMPARATOR)
194 .add(IControlFlowEntryComparator.PTID_COMPARATOR);
195 return builder.build();
196 }
197 };
198
199 private static int compareTrace(int direction, ITimeGraphEntry o1, ITimeGraphEntry o2) {
200 int result = IControlFlowEntryComparator.TRACE_COMPARATOR.compare(o1, o2);
201 if (direction == SWT.UP) {
202 result = -result;
203 }
204 return result;
205 }
206
207 private static int compareList(int prevResult, int direction, List<Comparator<ITimeGraphEntry>> comps, ITimeGraphEntry o1, ITimeGraphEntry o2) {
208 int result = prevResult;
209 for (Comparator<ITimeGraphEntry> comparator : comps) {
210 if (result == 0) {
211 result = comparator.compare(o1, o2);
212 if (direction == SWT.UP) {
213 result = -result;
214 }
215 }
216 }
217 return result;
218 }
219
220 }
This page took 0.046388 seconds and 5 git commands to generate.