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