Commit | Line | Data |
---|---|---|
4208b510 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015, 2016 EfficiOS Inc., Alexandre Montplaisir | |
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 | ||
10 | package org.eclipse.tracecompass.internal.provisional.analysis.lami.core.aspect; | |
11 | ||
12 | import java.util.Comparator; | |
13 | ||
14 | import org.eclipse.jdt.annotation.NonNull; | |
15 | import org.eclipse.jdt.annotation.Nullable; | |
16 | import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module.LamiTableEntry; | |
17 | ||
18 | /** | |
19 | * Base class for LAMI table aspects. | |
20 | * | |
21 | * @author Alexandre Montplaisir | |
22 | */ | |
23 | public class LamiGenericAspect extends LamiTableEntryAspect { | |
24 | ||
25 | private final int fColIndex; | |
26 | private final boolean fIsContinuous; | |
27 | private final boolean fIsTimeStamp; | |
28 | ||
29 | /** | |
30 | * Constructor | |
31 | * | |
32 | * @param aspectName | |
33 | * Name of the aspect (name of the column in the UI) | |
34 | * @param units | |
35 | * The units of this column | |
36 | * @param colIndex | |
37 | * Index of this column | |
38 | * @param isContinuous | |
39 | * If the contents of this column are numbers or not | |
40 | * @param isTimeStamp | |
41 | * If the contents of this column are numerical timestamp or not | |
42 | */ | |
43 | public LamiGenericAspect(String aspectName, @Nullable String units, int colIndex, boolean isContinuous, boolean isTimeStamp) { | |
44 | super(aspectName, units); | |
45 | fColIndex = colIndex; | |
46 | fIsContinuous = isContinuous; | |
47 | fIsTimeStamp = isTimeStamp; | |
48 | } | |
49 | ||
50 | @Override | |
51 | public boolean isContinuous() { | |
52 | return fIsContinuous; | |
53 | } | |
54 | ||
55 | @Override | |
56 | public boolean isTimeStamp() { | |
57 | return fIsTimeStamp; | |
58 | } | |
59 | ||
60 | @Override | |
61 | public @Nullable String resolveString(@NonNull LamiTableEntry entry) { | |
62 | return entry.getValue(fColIndex).toString(); | |
63 | } | |
64 | ||
65 | @Override | |
66 | public @Nullable Double resolveDouble(@NonNull LamiTableEntry entry) { | |
67 | if (fIsContinuous) { | |
68 | try { | |
69 | if (entry.getValue(fColIndex).toString() != null) { | |
70 | return Double.parseDouble(entry.getValue(fColIndex).toString()); | |
71 | } | |
72 | } catch (NumberFormatException e) { | |
73 | // Fallback to default value below | |
74 | } | |
75 | } | |
76 | return null; | |
77 | } | |
78 | ||
79 | @Override | |
80 | public Comparator<LamiTableEntry> getComparator() { | |
81 | if (isContinuous()) { | |
82 | return (o1, o2) -> { | |
83 | Double dO1 = resolveDouble(o1); | |
84 | Double dO2 = resolveDouble(o2); | |
85 | if (dO1 == null || dO2 == null) { | |
86 | return 0; | |
87 | } | |
88 | ||
89 | return dO1.compareTo(dO2); | |
90 | }; | |
91 | } | |
92 | ||
93 | /* Use regular string comparison */ | |
94 | return (o1, o2) -> { | |
95 | String s1 = resolveString(o1); | |
96 | String s2 = resolveString(o2); | |
97 | ||
98 | if (s1 == null || s2 == null) { | |
99 | return 0; | |
100 | } | |
101 | ||
102 | return s1.compareTo(s2); | |
103 | }; | |
104 | } | |
105 | ||
106 | } |