Remove unneeded checkNotNull() calls
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.ui / src / org / eclipse / tracecompass / internal / analysis / timing / ui / views / segmentstore / statistics / AbstractSegmentStoreStatisticsViewer.java
CommitLineData
ce8319b6 1/*******************************************************************************
658401c8 2 * Copyright (c) 2015, 2016 Ericsson
ce8319b6
BH
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
658401c8 12package org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore.statistics;
ce8319b6
BH
13
14import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
15
91deb4e1 16import java.text.Format;
ce8319b6
BH
17import java.util.ArrayList;
18import java.util.List;
ce8319b6
BH
19
20import org.eclipse.jdt.annotation.Nullable;
21import org.eclipse.jface.viewers.Viewer;
22import org.eclipse.jface.viewers.ViewerComparator;
a0acb044 23import org.eclipse.swt.SWT;
ce8319b6 24import org.eclipse.swt.widgets.Composite;
91deb4e1 25import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.SubSecondTimeWithUnitFormat;
658401c8
BH
26import org.eclipse.tracecompass.internal.analysis.timing.core.segmentstore.statistics.SegmentStoreStatistics;
27import org.eclipse.tracecompass.internal.analysis.timing.ui.Activator;
ce8319b6
BH
28import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
29import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
30import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
31import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer;
32import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider;
33import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData;
34import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
35
36/**
a0acb044
MK
37 * An abstract tree viewer implementation for displaying segment store
38 * statistics
ce8319b6
BH
39 *
40 * @author Bernd Hufmann
41 *
42 */
3507ca88 43public abstract class AbstractSegmentStoreStatisticsViewer extends AbstractTmfTreeViewer {
ce8319b6 44
91deb4e1 45 private static final Format FORMATTER = new SubSecondTimeWithUnitFormat();
ce8319b6 46
a0acb044
MK
47 @Nullable
48 private TmfAbstractAnalysisModule fModule;
ce8319b6
BH
49
50 private static final String[] COLUMN_NAMES = new String[] {
53f46dc0
BH
51 checkNotNull(Messages.SegmentStoreStatistics_LevelLabel),
52 checkNotNull(Messages.SegmentStoreStatistics_Statistics_MinLabel),
53 checkNotNull(Messages.SegmentStoreStatistics_MaxLabel),
a0acb044
MK
54 checkNotNull(Messages.SegmentStoreStatistics_AverageLabel),
55 checkNotNull(Messages.SegmentStoreStatisticsViewer_StandardDeviation)
ce8319b6
BH
56 };
57
58 /**
59 * Constructor
60 *
61 * @param parent
62 * the parent composite
63 */
3507ca88 64 public AbstractSegmentStoreStatisticsViewer(Composite parent) {
ce8319b6 65 super(parent, false);
53f46dc0 66 setLabelProvider(new SegmentStoreStatisticsLabelProvider());
ce8319b6
BH
67 }
68
53f46dc0
BH
69 /** Provides label for the Segment Store tree viewer cells */
70 protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider {
ce8319b6
BH
71
72 @Override
73 public String getColumnText(@Nullable Object element, int columnIndex) {
a0acb044 74 String value = ""; //$NON-NLS-1$
ce8319b6
BH
75 if (element instanceof HiddenTreeViewerEntry) {
76 if (columnIndex == 0) {
77 value = ((HiddenTreeViewerEntry) element).getName();
78 }
53f46dc0
BH
79 } else if (element instanceof SegmentStoreStatisticsEntry) {
80 SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element;
1198ba6e 81 if (columnIndex == 0) {
0e4f957e 82 return String.valueOf(entry.getName());
1198ba6e
BH
83 }
84 if (entry.getEntry().getNbSegments() > 0) {
85 if (columnIndex == 1) {
86 value = toFormattedString(entry.getEntry().getMin());
ce8319b6 87 } else if (columnIndex == 2) {
1198ba6e 88 value = String.valueOf(toFormattedString(entry.getEntry().getMax()));
ce8319b6 89 } else if (columnIndex == 3) {
1198ba6e 90 value = String.valueOf(toFormattedString(entry.getEntry().getAverage()));
a0acb044
MK
91 } else if (columnIndex == 4) {
92 value = String.valueOf(toFormattedString(entry.getEntry().getStdDev()));
ce8319b6
BH
93 }
94 }
95 }
96 return checkNotNull(value);
97 }
98 }
99
100 /**
101 * Creates the statistics analysis module
102 *
103 * @return the statistics analysis module
104 */
a0acb044
MK
105 @Nullable
106 protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule();
ce8319b6
BH
107
108 /**
109 * Gets the statistics analysis module
a0acb044 110 *
ce8319b6
BH
111 * @return the statistics analysis module
112 */
a0acb044
MK
113 @Nullable
114 public TmfAbstractAnalysisModule getStatisticsAnalysisModule() {
ce8319b6
BH
115 return fModule;
116 }
117
118 @Override
119 protected ITmfTreeColumnDataProvider getColumnDataProvider() {
120 return new ITmfTreeColumnDataProvider() {
121
122 @Override
4c4e2816 123 public List<@Nullable TmfTreeColumnData> getColumnData() {
ce8319b6 124 /* All columns are sortable */
4c4e2816 125 List<@Nullable TmfTreeColumnData> columns = new ArrayList<>();
ce8319b6 126 TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]);
a0acb044 127 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
128 column.setComparator(new ViewerComparator() {
129 @Override
130 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
131 if ((e1 == null) || (e2 == null)) {
132 return 0;
133 }
134
53f46dc0
BH
135 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
136 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
137
138 return n1.getName().compareTo(n2.getName());
139
140 }
141 });
142 columns.add(column);
143 column = new TmfTreeColumnData(COLUMN_NAMES[1]);
a0acb044 144 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
145 column.setComparator(new ViewerComparator() {
146 @Override
147 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
148 if ((e1 == null) || (e2 == null)) {
149 return 0;
150 }
151
53f46dc0
BH
152 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
153 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
154
155 return Long.compare(n1.getEntry().getMin(), n2.getEntry().getMin());
156
157 }
158 });
159 columns.add(column);
160 column = new TmfTreeColumnData(COLUMN_NAMES[2]);
a0acb044 161 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
162 column.setComparator(new ViewerComparator() {
163 @Override
164 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
165 if ((e1 == null) || (e2 == null)) {
166 return 0;
167 }
168
53f46dc0
BH
169 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
170 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
171
172 return Long.compare(n1.getEntry().getMax(), n2.getEntry().getMax());
173
174 }
175 });
176 columns.add(column);
177 column = new TmfTreeColumnData(COLUMN_NAMES[3]);
a0acb044 178 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
179 column.setComparator(new ViewerComparator() {
180 @Override
181 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
182 if ((e1 == null) || (e2 == null)) {
183 return 0;
184 }
185
53f46dc0
BH
186 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
187 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
188
189 return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage());
190
191 }
192 });
193 columns.add(column);
a0acb044
MK
194 column = new TmfTreeColumnData(COLUMN_NAMES[4]);
195 column.setAlignment(SWT.RIGHT);
196 column.setComparator(new ViewerComparator() {
197 @Override
198 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
199 if ((e1 == null) || (e2 == null)) {
200 return 0;
201 }
202
203 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
204 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6 205
a0acb044
MK
206 return Double.compare(n1.getEntry().getStdDev(), n2.getEntry().getStdDev());
207
208 }
209 });
210 columns.add(column);
211 column = new TmfTreeColumnData(""); //$NON-NLS-1$
212 columns.add(column);
ce8319b6
BH
213 return columns;
214 }
215
216 };
217 }
218
ce8319b6
BH
219 @Override
220 public void initializeDataSource() {
221 /* Should not be called while trace is still null */
222 ITmfTrace trace = checkNotNull(getTrace());
223 TmfAbstractAnalysisModule module = createStatisticsAnalysiModule();
224 if (module == null) {
225 return;
226 }
227 try {
228 module.setTrace(trace);
229 module.schedule();
230 fModule = module;
231 } catch (TmfAnalysisException e) {
232 Activator.getDefault().logError("Error initializing statistics analysis module", e); //$NON-NLS-1$
233 }
234 }
235
236 /**
237 * Formats a double value string
238 *
239 * @param value
240 * a value to format
241 * @return formatted value
242 */
243 protected static String toFormattedString(double value) {
a0acb044
MK
244 // The cast to long is needed because the formatter cannot truncate the
245 // number.
0e4f957e 246 String percentageString = String.format("%s", FORMATTER.format(value)); //$NON-NLS-1$
ce8319b6
BH
247 return percentageString;
248 }
249
250 /**
251 * Class for defining an entry in the statistics tree.
252 */
53f46dc0 253 protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry {
3507ca88 254
53f46dc0 255 private SegmentStoreStatistics fEntry;
ce8319b6
BH
256
257 /**
258 * Constructor
259 *
260 * @param name
261 * name of entry
262 *
263 * @param entry
53f46dc0 264 * segment store statistics object
ce8319b6 265 */
53f46dc0 266 public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) {
ce8319b6
BH
267 super(name);
268 fEntry = entry;
269 }
270
271 /**
272 * Gets the statistics object
273 *
274 * @return statistics object
275 */
53f46dc0 276 public SegmentStoreStatistics getEntry() {
0e4f957e 277 return fEntry;
ce8319b6
BH
278 }
279
280 }
281
282 /**
283 * Class to define a level in the tree that doesn't have any values.
284 */
53f46dc0 285 protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry {
ce8319b6
BH
286 /**
287 * Constructor
288 *
289 * @param name
290 * the name of the level
291 */
292 public HiddenTreeViewerEntry(String name) {
53f46dc0 293 super(name, new SegmentStoreStatistics());
ce8319b6
BH
294 }
295 }
296
297}
This page took 0.04447 seconds and 5 git commands to generate.