1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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
;
11 import java
.util
.Comparator
;
12 import java
.util
.List
;
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
;
19 import com
.google
.common
.collect
.ImmutableList
;
23 * Class with comparators used for sorting the ControlFlowEntries based based on
24 * the column selection.
26 * @author Bernd Hufmann
29 public final class ControlFlowColumnComparators
{
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.
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
;
40 public int compare(@Nullable ITimeGraphEntry o1
, @Nullable ITimeGraphEntry o2
) {
41 /* First sort by trace */
42 int result
= compareTrace(fDirection
, o1
, o2
);
45 /* Then sort by process name */
46 result
= IControlFlowEntryComparator
.PROCESS_NAME_COMPARATOR
.compare(o1
, o2
);
48 return compareList(result
, fDirection
, SECONDARY_COMPARATORS
, o1
, o2
);
52 public void setDirection(int direction
) {
53 fDirection
= direction
;
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();
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.
69 public static ITimeGraphEntryComparator TID_COLUMN_COMPARATOR
= new ITimeGraphEntryComparator() {
71 private final List
<Comparator
<ITimeGraphEntry
>> SECONDARY_COMPARATORS
= init();
72 private int fDirection
= SWT
.DOWN
;
75 public int compare(@Nullable ITimeGraphEntry o1
, @Nullable ITimeGraphEntry o2
) {
76 /* First sort by trace */
77 int result
= compareTrace(fDirection
, o1
, o2
);
80 /* Then sort by TID */
81 result
= IControlFlowEntryComparator
.TID_COMPARATOR
.compare(o1
, o2
);
83 return compareList(result
, fDirection
, SECONDARY_COMPARATORS
, o1
, o2
);
87 public void setDirection(int direction
) {
88 fDirection
= direction
;
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();
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.
105 public static ITimeGraphEntryComparator PTID_COLUMN_COMPARATOR
= new ITimeGraphEntryComparator() {
107 private final List
<Comparator
<ITimeGraphEntry
>> SECONDARY_COMPARATORS
= init();
108 private int fDirection
= SWT
.DOWN
;
111 public int compare(@Nullable ITimeGraphEntry o1
, @Nullable ITimeGraphEntry o2
) {
112 /* First sort by trace */
113 int result
= compareTrace(fDirection
, o1
, o2
);
115 /* Then sort by PTID */
116 result
= IControlFlowEntryComparator
.PTID_COMPARATOR
.compare(o1
, o2
);
118 return compareList(result
, fDirection
, SECONDARY_COMPARATORS
, o1
, o2
);
122 public void setDirection(int direction
) {
123 fDirection
= direction
;
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();
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.
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
;
144 public int compare(@Nullable ITimeGraphEntry o1
, @Nullable ITimeGraphEntry o2
) {
146 int result
= compareTrace(fDirection
, o1
, o2
);
149 /* Sort all child processes according to birth time. */
150 result
= IControlFlowEntryComparator
.BIRTH_TIME_COMPARATOR
.compare(o1
, o2
);
152 return compareList(result
, fDirection
, SECONDARY_COMPARATORS
, o1
, o2
);
156 public void setDirection(int direction
) {
157 fDirection
= direction
;
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();
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
174 public static ITimeGraphEntryComparator TRACE_COLUMN_COMPARATOR
= new ITimeGraphEntryComparator() {
175 private final List
<Comparator
<ITimeGraphEntry
>> SECONDARY_COMPARATORS
= init();
176 private int fDirection
= SWT
.DOWN
;
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
);
185 public void setDirection(int direction
) {
186 fDirection
= direction
;
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();
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
) {
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
) {
211 result
= comparator
.compare(o1
, o2
);
212 if (direction
== SWT
.UP
) {