(no commit message)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / event / TmfEventContent.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.event;
14
15
16 /**
17 * <b><u>TmfEventContent</u></b>
18 * <p>
19 * The event content.
20 */
21 public class TmfEventContent {
22
23 // ------------------------------------------------------------------------
24 // Attributes
25 // ------------------------------------------------------------------------
26
27 protected TmfEvent fParentEvent;
28 protected Object fRawContent;
29 protected Object[] fFields;
30
31 // ------------------------------------------------------------------------
32 // Constructors
33 // ------------------------------------------------------------------------
34
35 /**
36 * @param parent the parent event (owner)
37 * @param content the raw content
38 */
39 public TmfEventContent(TmfEvent parent, Object content) {
40 fParentEvent = parent;
41 fRawContent = content;
42 }
43
44 /**
45 * @param other the original event content
46 */
47 public TmfEventContent(TmfEventContent other) {
48 if (other == null)
49 throw new IllegalArgumentException();
50 fParentEvent = other.fParentEvent;
51 fRawContent = other.fRawContent;
52 fFields = other.fFields;
53 }
54
55 @SuppressWarnings("unused")
56 private TmfEventContent() {
57 throw new AssertionError();
58 }
59
60 // ------------------------------------------------------------------------
61 // Accessors
62 // ------------------------------------------------------------------------
63
64 /**
65 * @return the parent (containing) event
66 */
67 public TmfEvent getEvent() {
68 return fParentEvent;
69 }
70
71 /**
72 * @return the event type
73 */
74 public TmfEventType getType() {
75 return fParentEvent.getType();
76 }
77
78 /**
79 * @return the raw content
80 */
81 public Object getContent() {
82 return fRawContent;
83 }
84
85 /**
86 * Returns the list of fields in the same order as TmfEventType.getLabels()
87 *
88 * @return the ordered set of fields (optional fields might be null)
89 */
90 public Object[] getFields() {
91 if (fFields == null) {
92 parseContent();
93 }
94 return fFields;
95 }
96
97 /**
98 * @param id the field id
99 * @return the corresponding field
100 * @throws TmfNoSuchFieldException
101 */
102 public Object getField(String id) throws TmfNoSuchFieldException {
103 if (fFields == null) {
104 parseContent();
105 }
106 return fFields[getType().getFieldIndex(id)];
107 }
108
109 /**
110 * @param n the field index as per TmfEventType.getLabels()
111 * @return the corresponding field (null if non-existing)
112 */
113 public Object getField(int n) {
114 if (fFields == null) {
115 parseContent();
116 }
117 if (n >= 0 && n < fFields.length)
118 return fFields[n];
119
120 return null;
121 }
122
123 // ------------------------------------------------------------------------
124 // Operators
125 // ------------------------------------------------------------------------
126
127 /**
128 * Parse the content into fields. By default, a single field (the raw
129 * content) is returned.
130 * Should be overridden.
131 */
132 protected void parseContent() {
133 fFields = new Object[1];
134 fFields[0] = fRawContent;
135 }
136
137 // ------------------------------------------------------------------------
138 // Object
139 // ------------------------------------------------------------------------
140
141 @Override
142 public int hashCode() {
143 int result = 17;
144 result = 37 * result + ((fParentEvent != null) ? fParentEvent.hashCode() : 0);
145 result = 37 * result + ((fRawContent != null) ? fRawContent.hashCode() : 0);
146 return result;
147 }
148
149 @Override
150 public boolean equals(Object other) {
151 if (!(other instanceof TmfEventContent))
152 return false;
153 TmfEventContent o = (TmfEventContent) other;
154 return fRawContent.equals(o.fRawContent);
155 }
156
157 @Override
158 public String toString() {
159 Object[] fields = getFields();
160 StringBuilder result = new StringBuilder("[TmfEventContent(");
161 for (int i = 0; i < fields.length; i++) {
162 if (i > 0) result.append(",");
163 result.append(fields[i]);
164 }
165 result.append(")]");
166 return result.toString();
167 }
168
169 }
This page took 0.033616 seconds and 5 git commands to generate.