Some cleanups related to investigating redundant null checks
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / internal / tmf / core / parsers / custom / CustomEventAspects.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 * Alexandre Montplaisir - Update for TmfEventTableColumn
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.tmf.core.parsers.custom;
15
16 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.tracecompass.common.core.NonNullUtils;
22 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
23 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
24 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomEvent;
25 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition;
26 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn;
27
28 import com.google.common.collect.ImmutableList;
29
30 /**
31 * Event aspects for Custom {Text|XML} traces.
32 *
33 * Since this definition will be different for every single custom trace, we
34 * cannot define specific {@link ITmfEventAspect} in advance.
35 *
36 * Instead, one has to call {@link #generateAspects(CustomTraceDefinition)}
37 * with the CustomTraceDefinition of the the particular trace to display.
38 *
39 * @author Alexandre Montplaisir
40 */
41 public class CustomEventAspects {
42
43 /**
44 * Aspects for custom events, which use an integer ID to represent each
45 * field.
46 */
47 private static final class CustomEventFieldAspect implements ITmfEventAspect {
48
49 private final @NonNull String fName;
50 private final int fIndex;
51
52 /**
53 * Constructor
54 *
55 * @param name
56 * The name of this aspect
57 * @param idx
58 * The index of this field in the event's content to display
59 */
60 public CustomEventFieldAspect(@NonNull String name, int idx) {
61 fName = name;
62 fIndex = idx;
63 }
64
65 @Override
66 public String getName() {
67 return fName;
68 }
69
70 @Override
71 public String getHelpText() {
72 return EMPTY_STRING;
73 }
74
75 @Override
76 public String resolve(ITmfEvent event) {
77 if (event instanceof CustomEvent) {
78 return NonNullUtils.nullToEmptyString(((CustomEvent) event).getEventString(fIndex));
79 }
80 return EMPTY_STRING;
81 }
82 }
83
84 /**
85 * Build a set of event aspects for a given trace definition
86 *
87 * @param definition
88 * The {@link CustomTraceDefinition} of the trace for which you
89 * want the aspects
90 * @return The set of event aspects for the given trace
91 */
92 public static @NonNull Iterable<ITmfEventAspect> generateAspects(CustomTraceDefinition definition) {
93 ImmutableList.Builder<ITmfEventAspect> builder = new ImmutableList.Builder<>();
94 List<OutputColumn> outputs = definition.outputs;
95 for (int i = 0; i < outputs.size(); i++) {
96 String name = outputs.get(i).name;
97 builder.add(new CustomEventFieldAspect(name, i));
98 }
99 return checkNotNull(builder.build());
100 }
101 }
This page took 0.059503 seconds and 6 git commands to generate.