analysis.lami: Implementation of LAMI plugins
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.lami.core / src / org / eclipse / tracecompass / internal / provisional / analysis / lami / core / module / LamiTableEntry.java
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.module;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.stream.Collectors;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.types.LamiData;
21 import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.types.LamiTimeRange;
22 import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.types.LamiTimestamp;
23
24 import com.google.common.collect.ImmutableList;
25
26 /**
27 * Entry of a LAMI output. Usually corresponds to one row in a JSON LAMI table
28 * output.
29 *
30 * @author Alexandre Montplaisir
31 */
32 public class LamiTableEntry {
33
34 private final List<LamiData> fValues;
35
36 /**
37 * Constructor
38 *
39 * @param values Values contained in this row
40 */
41 public LamiTableEntry(List<LamiData> values) {
42 fValues = checkNotNull(ImmutableList.copyOf(values));
43 }
44
45 /**
46 * Get the value at a given index
47 *
48 * @param index
49 * Index to look at
50 * @return The value at this index
51 */
52 public LamiData getValue(int index) {
53 return fValues.get(index);
54 }
55
56 /**
57 * Get the time range represented by this row.
58 *
59 * If more than one exists, one of them (usually the first) is returned.
60 *
61 * If there are no time ranges in this row, null is returned.
62 *
63 * @return The time range of this row
64 */
65 public @Nullable LamiTimeRange getCorrespondingTimeRange() {
66 /*
67 * If there is one or more time range(s) in the values, return the first
68 * one we find directly.
69 */
70 Optional<LamiTimeRange> oTimerange = fValues.stream()
71 .filter(data -> (data instanceof LamiTimeRange))
72 .<@NonNull LamiTimeRange> map(data -> (LamiTimeRange) data)
73 .findFirst();
74 if (oTimerange.isPresent()) {
75 return oTimerange.get();
76 }
77
78 /* Look for individual timestamps instead */
79 List<LamiTimestamp> timestamps = fValues.stream()
80 .filter(data -> (data instanceof LamiTimestamp))
81 .<@NonNull LamiTimestamp> map(data -> (LamiTimestamp) data)
82 .collect(Collectors.toList());
83
84 if (timestamps.size() > 1) {
85 /* We can try using the first two timestamps to create a range (making sure it's valid) */
86 long first = timestamps.get(0).getValue();
87 long second = timestamps.get(1).getValue();
88 if (second >= first) {
89 return new LamiTimeRange(first, second);
90 }
91 }
92
93 if (!timestamps.isEmpty()) {
94 /* If there is only one timestamp, use it to create a punctual range */
95 long ts = timestamps.get(0).getValue();
96 return new LamiTimeRange(ts, ts);
97 }
98
99 /* Didn't find any timestamp we can't use */
100 return null;
101 }
102 }
This page took 0.035198 seconds and 5 git commands to generate.