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 | |
cbd4ad82 | 22 | // ------------------------------------------------------------------------ |
8c8bf09f | 23 | // Attributes |
cbd4ad82 | 24 | // ------------------------------------------------------------------------ |
8c8bf09f | 25 | |
cbd4ad82 FC |
26 | protected TmfEvent fParentEvent; |
27 | protected Object fRawContent; | |
28 | protected Object[] fFields; | |
8c8bf09f | 29 | |
cbd4ad82 | 30 | // ------------------------------------------------------------------------ |
8c8bf09f | 31 | // Constructors |
cbd4ad82 | 32 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
33 | |
34 | /** | |
cbd4ad82 FC |
35 | * @param parent the parent event (owner) |
36 | * @param content the raw content | |
8c8bf09f | 37 | */ |
28b94d61 FC |
38 | public TmfEventContent(TmfEvent parent, Object content) { |
39 | fParentEvent = parent; | |
40 | fRawContent = content; | |
8c8bf09f ASL |
41 | } |
42 | ||
28b94d61 | 43 | /** |
cbd4ad82 | 44 | * @param other the original event content |
28b94d61 FC |
45 | */ |
46 | public TmfEventContent(TmfEventContent other) { | |
cbd4ad82 FC |
47 | if (other == null) |
48 | throw new IllegalArgumentException(); | |
28b94d61 FC |
49 | fParentEvent = other.fParentEvent; |
50 | fRawContent = other.fRawContent; | |
51 | fFields = other.fFields; | |
52 | } | |
53 | ||
54 | @SuppressWarnings("unused") | |
55 | private TmfEventContent() { | |
cbd4ad82 | 56 | throw new AssertionError(); |
28b94d61 FC |
57 | } |
58 | ||
cbd4ad82 | 59 | // ------------------------------------------------------------------------ |
8c8bf09f | 60 | // Accessors |
cbd4ad82 | 61 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
62 | |
63 | /** | |
28b94d61 | 64 | * @return the parent (containing) event |
8c8bf09f | 65 | */ |
28b94d61 FC |
66 | public TmfEvent getEvent() { |
67 | return fParentEvent; | |
8c8bf09f ASL |
68 | } |
69 | ||
70 | /** | |
28b94d61 | 71 | * @return the event type |
8c8bf09f | 72 | */ |
28b94d61 FC |
73 | public TmfEventType getType() { |
74 | return fParentEvent.getType(); | |
8c8bf09f ASL |
75 | } |
76 | ||
28b94d61 FC |
77 | /** |
78 | * @return the raw content | |
79 | */ | |
80 | public Object getContent() { | |
81 | return fRawContent; | |
82 | } | |
8c8bf09f ASL |
83 | |
84 | /** | |
28b94d61 FC |
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) | |
8c8bf09f | 88 | */ |
28b94d61 FC |
89 | public Object[] getFields() { |
90 | if (fFields == null) { | |
91 | parseContent(); | |
92 | } | |
8c8bf09f ASL |
93 | return fFields; |
94 | } | |
95 | ||
96 | /** | |
cbd4ad82 FC |
97 | * @param id the field id |
98 | * @return the corresponding field | |
99 | * @throws TmfNoSuchFieldException | |
8c8bf09f | 100 | */ |
28b94d61 FC |
101 | public Object getField(String id) throws TmfNoSuchFieldException { |
102 | if (fFields == null) { | |
103 | parseContent(); | |
104 | } | |
105 | return fFields[getType().getFieldIndex(id)]; | |
8c8bf09f ASL |
106 | } |
107 | ||
146a887c | 108 | /** |
cbd4ad82 FC |
109 | * @param n the field index as per TmfEventType.getLabels() |
110 | * @return the corresponding field (null if non-existing) | |
146a887c | 111 | */ |
28b94d61 FC |
112 | public Object getField(int n) { |
113 | if (fFields == null) { | |
114 | parseContent(); | |
115 | } | |
116 | if (n >= 0 && n < fFields.length) | |
117 | return fFields[n]; | |
cbd4ad82 | 118 | |
28b94d61 FC |
119 | return null; |
120 | } | |
121 | ||
cbd4ad82 | 122 | // ------------------------------------------------------------------------ |
28b94d61 | 123 | // Operators |
cbd4ad82 | 124 | // ------------------------------------------------------------------------ |
28b94d61 | 125 | |
023761c4 FC |
126 | /** |
127 | * @param event | |
128 | */ | |
129 | public void setEvent(TmfEvent event) { | |
130 | fParentEvent = event; | |
131 | } | |
132 | ||
28b94d61 | 133 | /** |
cbd4ad82 FC |
134 | * Parse the content into fields. By default, a single field (the raw |
135 | * content) is returned. | |
136 | * Should be overridden. | |
28b94d61 FC |
137 | */ |
138 | protected void parseContent() { | |
139 | fFields = new Object[1]; | |
140 | fFields[0] = fRawContent; | |
146a887c | 141 | } |
28b94d61 | 142 | |
cbd4ad82 FC |
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); | |
28b94d61 FC |
161 | } |
162 | ||
163 | @Override | |
3b38ea61 | 164 | @SuppressWarnings("nls") |
28b94d61 FC |
165 | public String toString() { |
166 | Object[] fields = getFields(); | |
cbd4ad82 | 167 | StringBuilder result = new StringBuilder("[TmfEventContent("); |
28b94d61 | 168 | for (int i = 0; i < fields.length; i++) { |
cbd4ad82 FC |
169 | if (i > 0) result.append(","); |
170 | result.append(fields[i]); | |
28b94d61 | 171 | } |
cbd4ad82 FC |
172 | result.append(")]"); |
173 | return result.toString(); | |
28b94d61 | 174 | } |
146a887c | 175 | |
1a971e96 FC |
176 | @Override |
177 | public TmfEventContent clone() { | |
178 | TmfEventContent clone = null; | |
179 | try { | |
180 | clone = (TmfEventContent) super.clone(); | |
181 | clone.fParentEvent = fParentEvent; | |
c87f4702 FC |
182 | clone.fRawContent = fRawContent; |
183 | clone.fFields = fFields; | |
1a971e96 FC |
184 | } |
185 | catch (CloneNotSupportedException e) { | |
186 | e.printStackTrace(); | |
187 | } | |
188 | return clone; | |
189 | } | |
8c8bf09f | 190 | } |