Commit | Line | Data |
---|---|---|
a4cddcbc BH |
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 | ||
a14d7bd5 BH |
31 | /** |
32 | * Default constructor | |
33 | */ | |
34 | private ControlFlowColumnComparators() {} | |
35 | ||
a4cddcbc BH |
36 | /** |
37 | * Process Name comparator. This compares first the trace, then the process name, then the | |
38 | * birth time, then the TID finally the parent TID. | |
39 | */ | |
a14d7bd5 | 40 | public static final ITimeGraphEntryComparator PROCESS_NAME_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() { |
a4cddcbc BH |
41 | private final List<Comparator<ITimeGraphEntry>> SECONDARY_COMPARATORS = init(); |
42 | private int fDirection = SWT.DOWN; | |
43 | ||
44 | @Override | |
45 | public int compare(@Nullable ITimeGraphEntry o1, @Nullable ITimeGraphEntry o2) { | |
3553c912 PT |
46 | /* First sort by process name */ |
47 | int result = IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR.compare(o1, o2); | |
a4cddcbc BH |
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 | */ | |
a14d7bd5 | 69 | public static final ITimeGraphEntryComparator TID_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() { |
a4cddcbc BH |
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) { | |
3553c912 PT |
76 | /* First sort by TID */ |
77 | int result = IControlFlowEntryComparator.TID_COMPARATOR.compare(o1, o2); | |
a4cddcbc BH |
78 | return compareList(result, fDirection, SECONDARY_COMPARATORS, o1, o2); |
79 | } | |
80 | ||
81 | @Override | |
82 | public void setDirection(int direction) { | |
83 | fDirection = direction; | |
84 | } | |
85 | ||
86 | private List<Comparator<ITimeGraphEntry>> init() { | |
87 | ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder(); | |
88 | builder.add(IControlFlowEntryComparator.BIRTH_TIME_COMPARATOR) | |
89 | .add(IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR) | |
90 | .add(IControlFlowEntryComparator.PTID_COMPARATOR); | |
91 | return builder.build(); | |
92 | } | |
93 | ||
94 | }; | |
95 | ||
96 | /** | |
97 | * Process PTID comparator. This compares first the trace, then the process | |
98 | * parent TID, then the birth time, then the process name finally the TID. | |
99 | */ | |
a14d7bd5 | 100 | public static final ITimeGraphEntryComparator PTID_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() { |
a4cddcbc BH |
101 | |
102 | private final List<Comparator<ITimeGraphEntry>> SECONDARY_COMPARATORS = init(); | |
103 | private int fDirection = SWT.DOWN; | |
104 | ||
105 | @Override | |
106 | public int compare(@Nullable ITimeGraphEntry o1, @Nullable ITimeGraphEntry o2) { | |
3553c912 PT |
107 | /* First sort by PTID */ |
108 | int result = IControlFlowEntryComparator.PTID_COMPARATOR.compare(o1, o2); | |
a4cddcbc BH |
109 | return compareList(result, fDirection, SECONDARY_COMPARATORS, o1, o2); |
110 | } | |
111 | ||
112 | @Override | |
113 | public void setDirection(int direction) { | |
114 | fDirection = direction; | |
115 | } | |
116 | ||
117 | private List<Comparator<ITimeGraphEntry>> init() { | |
118 | ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder(); | |
119 | builder.add(IControlFlowEntryComparator.BIRTH_TIME_COMPARATOR) | |
120 | .add(IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR) | |
121 | .add(IControlFlowEntryComparator.TID_COMPARATOR); | |
122 | return builder.build(); | |
123 | } | |
124 | }; | |
125 | ||
126 | /** | |
127 | * Process birth time comparator. This compares first the trace, then the | |
128 | * birth time, then the process name, then the TID finally the parent TID. | |
129 | */ | |
a14d7bd5 | 130 | public static final ITimeGraphEntryComparator BIRTH_TIME_COLUMN_COMPARATOR = new ITimeGraphEntryComparator() { |
a4cddcbc BH |
131 | private final List<Comparator<ITimeGraphEntry>> SECONDARY_COMPARATORS = init(); |
132 | private int fDirection = SWT.DOWN; | |
133 | ||
134 | @Override | |
135 | public int compare(@Nullable ITimeGraphEntry o1, @Nullable ITimeGraphEntry o2) { | |
136 | ||
3553c912 PT |
137 | /* Sort all child processes according to birth time. */ |
138 | int result = IControlFlowEntryComparator.BIRTH_TIME_COMPARATOR.compare(o1, o2); | |
a4cddcbc BH |
139 | return compareList(result, fDirection, SECONDARY_COMPARATORS, o1, o2); |
140 | } | |
141 | ||
142 | @Override | |
143 | public void setDirection(int direction) { | |
144 | fDirection = direction; | |
145 | } | |
146 | ||
147 | private List<Comparator<ITimeGraphEntry>> init() { | |
148 | ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder(); | |
149 | builder.add(IControlFlowEntryComparator.PROCESS_NAME_COMPARATOR) | |
150 | .add(IControlFlowEntryComparator.TID_COMPARATOR) | |
151 | .add(IControlFlowEntryComparator.PTID_COMPARATOR); | |
152 | return builder.build(); | |
153 | } | |
154 | }; | |
155 | ||
a4cddcbc BH |
156 | private static int compareList(int prevResult, int direction, List<Comparator<ITimeGraphEntry>> comps, ITimeGraphEntry o1, ITimeGraphEntry o2) { |
157 | int result = prevResult; | |
158 | for (Comparator<ITimeGraphEntry> comparator : comps) { | |
159 | if (result == 0) { | |
160 | result = comparator.compare(o1, o2); | |
161 | if (direction == SWT.UP) { | |
162 | result = -result; | |
163 | } | |
164 | } | |
165 | } | |
166 | return result; | |
167 | } | |
168 | ||
169 | } |