Commit | Line | Data |
---|---|---|
8c8bf09f ASL |
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: | |
1f506a43 | 10 | * Francois Chouinard - Initial API and implementation |
8c8bf09f ASL |
11 | *******************************************************************************/ |
12 | ||
13 | package org.eclipse.linuxtools.tmf.event; | |
14 | ||
8c8bf09f ASL |
15 | /** |
16 | * <b><u>TmfEventContent</u></b> | |
17 | * <p> | |
18 | * The event content. | |
19 | */ | |
1a971e96 | 20 | public class TmfEventContent implements Cloneable { |
8c8bf09f | 21 | |
e2561baf FC |
22 | // Default field ids |
23 | public static final String FIELD_ID_TIMESTAMP = "$time$"; //$NON-NLS-1$ | |
24 | public static final String FIELD_ID_SOURCE = "$sour$"; //$NON-NLS-1$ | |
25 | public static final String FIELD_ID_TYPE = "$type$"; //$NON-NLS-1$ | |
26 | public static final String FIELD_ID_REFERENCE = "$refe$"; //$NON-NLS-1$ | |
27 | public static final String FIELD_ID_CONTENT = "$cont$"; //$NON-NLS-1$ | |
28 | ||
cbd4ad82 | 29 | // ------------------------------------------------------------------------ |
8c8bf09f | 30 | // Attributes |
cbd4ad82 | 31 | // ------------------------------------------------------------------------ |
8c8bf09f | 32 | |
cbd4ad82 FC |
33 | protected TmfEvent fParentEvent; |
34 | protected Object fRawContent; | |
35 | protected Object[] fFields; | |
8c8bf09f | 36 | |
cbd4ad82 | 37 | // ------------------------------------------------------------------------ |
8c8bf09f | 38 | // Constructors |
cbd4ad82 | 39 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
40 | |
41 | /** | |
cbd4ad82 FC |
42 | * @param parent the parent event (owner) |
43 | * @param content the raw content | |
8c8bf09f | 44 | */ |
28b94d61 FC |
45 | public TmfEventContent(TmfEvent parent, Object content) { |
46 | fParentEvent = parent; | |
47 | fRawContent = content; | |
8c8bf09f ASL |
48 | } |
49 | ||
28b94d61 | 50 | /** |
cbd4ad82 | 51 | * @param other the original event content |
28b94d61 FC |
52 | */ |
53 | public TmfEventContent(TmfEventContent other) { | |
cbd4ad82 FC |
54 | if (other == null) |
55 | throw new IllegalArgumentException(); | |
28b94d61 FC |
56 | fParentEvent = other.fParentEvent; |
57 | fRawContent = other.fRawContent; | |
58 | fFields = other.fFields; | |
59 | } | |
60 | ||
61 | @SuppressWarnings("unused") | |
62 | private TmfEventContent() { | |
cbd4ad82 | 63 | throw new AssertionError(); |
28b94d61 FC |
64 | } |
65 | ||
cbd4ad82 | 66 | // ------------------------------------------------------------------------ |
8c8bf09f | 67 | // Accessors |
cbd4ad82 | 68 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
69 | |
70 | /** | |
28b94d61 | 71 | * @return the parent (containing) event |
8c8bf09f | 72 | */ |
28b94d61 FC |
73 | public TmfEvent getEvent() { |
74 | return fParentEvent; | |
8c8bf09f ASL |
75 | } |
76 | ||
77 | /** | |
28b94d61 | 78 | * @return the event type |
8c8bf09f | 79 | */ |
28b94d61 FC |
80 | public TmfEventType getType() { |
81 | return fParentEvent.getType(); | |
8c8bf09f ASL |
82 | } |
83 | ||
28b94d61 FC |
84 | /** |
85 | * @return the raw content | |
86 | */ | |
87 | public Object getContent() { | |
88 | return fRawContent; | |
89 | } | |
8c8bf09f ASL |
90 | |
91 | /** | |
28b94d61 FC |
92 | * Returns the list of fields in the same order as TmfEventType.getLabels() |
93 | * | |
94 | * @return the ordered set of fields (optional fields might be null) | |
8c8bf09f | 95 | */ |
28b94d61 FC |
96 | public Object[] getFields() { |
97 | if (fFields == null) { | |
98 | parseContent(); | |
99 | } | |
8c8bf09f ASL |
100 | return fFields; |
101 | } | |
102 | ||
103 | /** | |
cbd4ad82 FC |
104 | * @param id the field id |
105 | * @return the corresponding field | |
106 | * @throws TmfNoSuchFieldException | |
8c8bf09f | 107 | */ |
28b94d61 FC |
108 | public Object getField(String id) throws TmfNoSuchFieldException { |
109 | if (fFields == null) { | |
110 | parseContent(); | |
111 | } | |
e2561baf FC |
112 | try { |
113 | return fFields[getType().getFieldIndex(id)]; | |
114 | } catch (TmfNoSuchFieldException e) { | |
115 | // Required for filtering from default TmfEventsTable columns | |
116 | if (id.equals(FIELD_ID_TIMESTAMP)) { | |
117 | return new Long(fParentEvent.getTimestamp().getValue()).toString(); | |
118 | } else if (id.equals(FIELD_ID_SOURCE)) { | |
119 | return fParentEvent.getSource().getSourceId().toString(); | |
120 | } else if (id.equals(FIELD_ID_TYPE)) { | |
121 | return fParentEvent.getType().getTypeId().toString(); | |
122 | } else if (id.equals(FIELD_ID_REFERENCE)) { | |
123 | return fParentEvent.getReference().getReference().toString(); | |
124 | } else if (id.equals(FIELD_ID_CONTENT)) { | |
125 | return fParentEvent.getContent().toString(); | |
126 | } | |
127 | throw e; | |
128 | } | |
8c8bf09f ASL |
129 | } |
130 | ||
146a887c | 131 | /** |
cbd4ad82 FC |
132 | * @param n the field index as per TmfEventType.getLabels() |
133 | * @return the corresponding field (null if non-existing) | |
146a887c | 134 | */ |
28b94d61 FC |
135 | public Object getField(int n) { |
136 | if (fFields == null) { | |
137 | parseContent(); | |
138 | } | |
139 | if (n >= 0 && n < fFields.length) | |
140 | return fFields[n]; | |
cbd4ad82 | 141 | |
28b94d61 FC |
142 | return null; |
143 | } | |
144 | ||
cbd4ad82 | 145 | // ------------------------------------------------------------------------ |
28b94d61 | 146 | // Operators |
cbd4ad82 | 147 | // ------------------------------------------------------------------------ |
28b94d61 | 148 | |
023761c4 FC |
149 | /** |
150 | * @param event | |
151 | */ | |
152 | public void setEvent(TmfEvent event) { | |
153 | fParentEvent = event; | |
154 | } | |
155 | ||
28b94d61 | 156 | /** |
cbd4ad82 FC |
157 | * Parse the content into fields. By default, a single field (the raw |
158 | * content) is returned. | |
159 | * Should be overridden. | |
28b94d61 FC |
160 | */ |
161 | protected void parseContent() { | |
162 | fFields = new Object[1]; | |
163 | fFields[0] = fRawContent; | |
146a887c | 164 | } |
28b94d61 | 165 | |
cbd4ad82 FC |
166 | // ------------------------------------------------------------------------ |
167 | // Object | |
168 | // ------------------------------------------------------------------------ | |
169 | ||
170 | @Override | |
171 | public int hashCode() { | |
172 | int result = 17; | |
173 | result = 37 * result + ((fParentEvent != null) ? fParentEvent.hashCode() : 0); | |
174 | result = 37 * result + ((fRawContent != null) ? fRawContent.hashCode() : 0); | |
175 | return result; | |
176 | } | |
177 | ||
178 | @Override | |
179 | public boolean equals(Object other) { | |
180 | if (!(other instanceof TmfEventContent)) | |
181 | return false; | |
182 | TmfEventContent o = (TmfEventContent) other; | |
183 | return fRawContent.equals(o.fRawContent); | |
28b94d61 FC |
184 | } |
185 | ||
186 | @Override | |
3b38ea61 | 187 | @SuppressWarnings("nls") |
28b94d61 FC |
188 | public String toString() { |
189 | Object[] fields = getFields(); | |
cbd4ad82 | 190 | StringBuilder result = new StringBuilder("[TmfEventContent("); |
28b94d61 | 191 | for (int i = 0; i < fields.length; i++) { |
cbd4ad82 FC |
192 | if (i > 0) result.append(","); |
193 | result.append(fields[i]); | |
28b94d61 | 194 | } |
cbd4ad82 FC |
195 | result.append(")]"); |
196 | return result.toString(); | |
28b94d61 | 197 | } |
146a887c | 198 | |
1a971e96 FC |
199 | @Override |
200 | public TmfEventContent clone() { | |
201 | TmfEventContent clone = null; | |
202 | try { | |
203 | clone = (TmfEventContent) super.clone(); | |
204 | clone.fParentEvent = fParentEvent; | |
c87f4702 FC |
205 | clone.fRawContent = fRawContent; |
206 | clone.fFields = fFields; | |
1a971e96 FC |
207 | } |
208 | catch (CloneNotSupportedException e) { | |
209 | e.printStackTrace(); | |
210 | } | |
211 | return clone; | |
212 | } | |
8c8bf09f | 213 | } |