tmf.xml: Move all .core and .ui packages to internal
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / segment / TmfXmlPatternSegment.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ecole Polytechnique de Montreal, 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 package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.segment;
10
11 import java.io.IOException;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.tracecompass.segmentstore.core.ISegment;
20 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
21 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
22 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
23 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
24
25 /**
26 * This class implements an XML Pattern Segment. This type of segment has
27 * content and a default timestamp, which is the start time of the segment.
28 *
29 * @author Jean-Christian Kouame
30 */
31 public class TmfXmlPatternSegment implements ISegment {
32
33 /**
34 * The serial version UID
35 */
36 private static final long serialVersionUID = 3556323761465412078L;
37
38 /* 'Byte' equivalent for state values types */
39 private static final byte TYPE_NULL = -1;
40 private static final byte TYPE_INTEGER = 0;
41 private static final byte TYPE_STRING = 1;
42 private static final byte TYPE_LONG = 2;
43
44 private final int fScale;
45 private final long fStart;
46 private final long fEnd;
47 private final String fSegmentName;
48 private transient Map<@NonNull String, @NonNull ITmfStateValue> fContent;
49
50 /**
51 * Constructs an XML pattern segment
52 *
53 * @param start
54 * Start time of the pattern segment
55 * @param end
56 * End time of the pattern segment
57 * @param scale
58 * Scale of the pattern segment
59 * @param segmentName
60 * Name of the pattern segment
61 * @param fields
62 * Fields of the pattern segment
63 */
64 public TmfXmlPatternSegment(long start, long end, int scale, String segmentName, @NonNull Map<@NonNull String, @NonNull ITmfStateValue> fields) {
65 fStart = start;
66 fEnd = end;
67 fScale = scale;
68 fSegmentName = segmentName;
69 fContent = Collections.unmodifiableMap(fields);
70 }
71
72 /**
73 * Get the start timestamp of the segment
74 *
75 * @return The start timestamp
76 */
77 public @NonNull ITmfTimestamp getTimestampStart() {
78 return TmfTimestamp.create(fStart, fScale);
79 }
80
81 /**
82 * Get the end timestamp of this segment
83 *
84 * @return The end timestamp
85 */
86 public @NonNull ITmfTimestamp getTimestampEnd() {
87 return TmfTimestamp.create(fEnd, fScale);
88 }
89
90 /**
91 * Get the content of the pattern segment
92 * @return The content
93 */
94 public Map<@NonNull String, @NonNull ITmfStateValue> getContent() {
95 return fContent;
96 }
97
98 /**
99 * Get the name of pattern segment
100 * @return The name
101 */
102 public String getName() {
103 return fSegmentName;
104 }
105
106 /**
107 * Get the timestamp scale of the pattern segment
108 * @return The timestamp scale
109 */
110 public int getScale() {
111 return fScale;
112 }
113
114 @Override
115 public int compareTo(@NonNull ISegment o) {
116 int ret = ISegment.super.compareTo(o);
117 if (ret != 0) {
118 return ret;
119 }
120 return toString().compareTo(o.toString());
121 }
122
123 @Override
124 public long getStart() {
125 return fStart;
126 }
127
128 @Override
129 public long getEnd() {
130 return fEnd;
131 }
132
133 @Override
134 public String toString() {
135 return new StringBuilder(getClass().getSimpleName())
136 .append(", [fTimestampStart=").append(getTimestampStart()) //$NON-NLS-1$
137 .append(", fTimestampEnd=").append(getTimestampEnd()) //$NON-NLS-1$
138 .append(", duration= ").append(getLength()) //$NON-NLS-1$
139 .append(", fName=").append(getName()) //$NON-NLS-1$
140 .append(", fContent=").append(getContent()) //$NON-NLS-1$
141 .append("]").toString(); //$NON-NLS-1$
142 }
143
144 private void writeObject(ObjectOutputStream out) throws IOException {
145 out.defaultWriteObject();
146
147 // Write the number of fields
148 out.writeInt(fContent.size());
149
150 // Write the fields
151 for (Map.Entry<String, ITmfStateValue> entry : fContent.entrySet()) {
152 out.writeInt(entry.getKey().length());
153 out.writeBytes(entry.getKey());
154 final ITmfStateValue value = entry.getValue();
155 final byte type = getByteFromType(value.getType());
156 out.writeByte(type);
157 switch (type) {
158 case TYPE_NULL:
159 break;
160 case TYPE_INTEGER:
161 out.writeInt(value.unboxInt());
162 break;
163 case TYPE_LONG:
164 out.writeLong(value.unboxLong());
165 break;
166 case TYPE_STRING:
167 final @NonNull String string = value.unboxStr();
168 out.writeInt(string.length());
169 out.writeBytes(string);
170 break;
171 default:
172 throw new IOException("Write object failed : Invalid data"); //$NON-NLS-1$
173 }
174 }
175 }
176
177 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
178 in.defaultReadObject();
179 int contentSize = in.readInt();
180
181 final Map<@NonNull String, @NonNull ITmfStateValue> content = new HashMap<>();
182 for (int i = 0; i < contentSize; i++) {
183 int length = in.readInt();
184 byte[] bytes = new byte[length];
185 in.read(bytes, 0, length);
186 String name = new String(bytes).intern();
187
188 Byte type = in.readByte();
189 ITmfStateValue value;
190 switch (type) {
191 case TYPE_NULL:
192 value = TmfStateValue.nullValue();
193 break;
194 case TYPE_INTEGER:
195 value = TmfStateValue.newValueInt(in.readInt());
196 break;
197 case TYPE_LONG:
198 value = TmfStateValue.newValueLong(in.readLong());
199 break;
200 case TYPE_STRING:
201 length = in.readInt();
202 bytes = new byte[length];
203 in.read(bytes, 0, length);
204 value = TmfStateValue.newValueString(new String(bytes).intern());
205 break;
206 default:
207 throw new IOException("Read object failed : Invalid data"); //$NON-NLS-1$
208 }
209 content.put(name, value);
210 }
211 fContent = content;
212 }
213
214 /**
215 * Here we determine how state values "types" are written in the 8-bit field
216 * that indicates the value type in the file.
217 */
218 private static byte getByteFromType(ITmfStateValue.Type type) {
219 switch (type) {
220 case NULL:
221 return TYPE_NULL;
222 case INTEGER:
223 return TYPE_INTEGER;
224 case STRING:
225 return TYPE_STRING;
226 case LONG:
227 return TYPE_LONG;
228 case DOUBLE:
229 default:
230 /* Should not happen if the switch is fully covered */
231 throw new IllegalStateException("Data type " + type + " not supported"); //$NON-NLS-1$ //$NON-NLS-2$
232 }
233 }
234 }
This page took 0.036075 seconds and 5 git commands to generate.