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