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 | |
5b973e7c | 66 | public @Nullable Number resolveNumber(@NonNull LamiTableEntry entry) { |
4208b510 AM |
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) -> { | |
5b973e7c JR |
83 | Number d1 = resolveNumber(o1); |
84 | Number d2 = resolveNumber(o2); | |
85 | ||
86 | if (d1 == null && d2 == null) { | |
4208b510 AM |
87 | return 0; |
88 | } | |
5b973e7c JR |
89 | if (d1 == null) { |
90 | return 1; | |
91 | } | |
92 | ||
93 | if (d2 == null) { | |
94 | return -1; | |
95 | } | |
4208b510 | 96 | |
5b973e7c | 97 | return Double.compare(d1.doubleValue(), d2.doubleValue()); |
4208b510 AM |
98 | }; |
99 | } | |
100 | ||
101 | /* Use regular string comparison */ | |
102 | return (o1, o2) -> { | |
103 | String s1 = resolveString(o1); | |
104 | String s2 = resolveString(o2); | |
105 | ||
5b973e7c | 106 | if (s1 == null && s2 == null) { |
4208b510 AM |
107 | return 0; |
108 | } | |
5b973e7c JR |
109 | if (s1 == null) { |
110 | return 1; | |
111 | } | |
112 | ||
113 | if (s2 == null) { | |
114 | return -1; | |
115 | } | |
4208b510 AM |
116 | |
117 | return s1.compareTo(s2); | |
118 | }; | |
119 | } | |
120 | ||
121 | } |