991112c308cccfd6f8b3cd14708c61bd015dd7a2
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / event / aspect / TmfContentFieldAspect.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
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 * Alexandre Montplaisir - Initial API and implementation
11 * Patrick Tasse - Renamed from TmfEventFieldAspect and support subfield array
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.core.event.aspect;
15
16 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
18 import java.util.Arrays;
19
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
22 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
23
24 /**
25 * Event aspect representing a single field of an event's content.
26 *
27 * @author Alexandre Montplaisir
28 */
29 public class TmfContentFieldAspect implements ITmfEventAspect {
30
31 private final String fAspectName;
32 private final String[] fFieldPath;
33 private final String fHelpText;
34
35 /**
36 * Constructor
37 *
38 * @param aspectName
39 * The name of the aspect. Should be localized.
40 * @param fieldPath
41 * The field name or absolute field path array to look for in the
42 * event content. Should *not* be localized!
43 */
44 public TmfContentFieldAspect(String aspectName, String... fieldPath) {
45 this(aspectName, EMPTY_STRING, fieldPath);
46 }
47
48 private TmfContentFieldAspect(String aspectName, String helpText, String... fieldPath) {
49 fAspectName = aspectName;
50 fFieldPath = checkNotNull(Arrays.copyOf(fieldPath, fieldPath.length));
51 fHelpText = helpText;
52 }
53
54 /**
55 * Creates a new instance of this aspect with the specified name, help text,
56 * and field path.
57 *
58 * @param aspectName
59 * The name of the aspect. Should be localized.
60 * @param helpText
61 * The help text.
62 * @param fieldPath
63 * The field name or absolute field path array to look for in the
64 * event content. Should *not* be localized!
65 * @return the new aspect
66 * @since 1.0
67 */
68 public static TmfContentFieldAspect create(String aspectName, String helpText, String... fieldPath) {
69 return new TmfContentFieldAspect(aspectName, helpText, fieldPath);
70 }
71
72 @Override
73 public String getName() {
74 return fAspectName;
75 }
76
77 @Override
78 public String getHelpText() {
79 return fHelpText;
80 }
81
82 @Override
83 public @Nullable Object resolve(ITmfEvent event) {
84 ITmfEventField field = event.getContent().getField(fFieldPath);
85 if (field == null) {
86 return null;
87 }
88 return field.getValue();
89 }
90
91 // ------------------------------------------------------------------------
92 // hashCode/equals
93 // Typically we want identical field aspects to be merged together.
94 // ------------------------------------------------------------------------
95
96 @Override
97 public int hashCode() {
98 final int prime = 31;
99 int result = 1;
100 result = prime * result + fAspectName.hashCode();
101 result = prime * result + Arrays.hashCode(fFieldPath);
102 result = prime * result + fHelpText.hashCode();
103 return result;
104 }
105
106 @Override
107 public boolean equals(@Nullable Object obj) {
108 if (this == obj) {
109 return true;
110 }
111 if (obj == null) {
112 return false;
113 }
114 if (!this.getClass().equals(obj.getClass())) {
115 return false;
116 }
117 TmfContentFieldAspect other = (TmfContentFieldAspect) obj;
118 if (!fAspectName.equals(other.fAspectName)) {
119 return false;
120 }
121 if (!Arrays.equals(fFieldPath, other.fFieldPath)) {
122 return false;
123 }
124 if (!fHelpText.equals(other.fHelpText)) {
125 return false;
126 }
127 return true;
128 }
129 }
This page took 0.058877 seconds and 4 git commands to generate.