f32406ab594b09dbda6a852a8f29c1353134e53c
1 /*******************************************************************************
2 * Copyright (c) 2016 Ecole Polytechnique de Montreal, Ericsson
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
.tmf
.analysis
.xml
.core
.segment
;
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
;
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
;
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.
29 * @author Jean-Christian Kouame
33 public class TmfXmlPatternSegment
implements ISegment
{
36 * The serial version UID
38 private static final long serialVersionUID
= 3556323761465412078L;
40 /* 'Byte' equivalent for state values types */
41 private static final byte TYPE_NULL
= -1;
42 private static final byte TYPE_INTEGER
= 0;
43 private static final byte TYPE_STRING
= 1;
44 private static final byte TYPE_LONG
= 2;
46 private final int fScale
;
47 private final long fStart
;
48 private final long fEnd
;
49 private final String fSegmentName
;
50 private transient Map
<@NonNull String
, @NonNull ITmfStateValue
> fContent
;
53 * Constructs an XML pattern segment
56 * Start time of the pattern segment
58 * End time of the pattern segment
60 * Scale of the pattern segment
62 * Name of the pattern segment
64 * Fields of the pattern segment
66 public TmfXmlPatternSegment(long start
, long end
, int scale
, String segmentName
, @NonNull Map
<@NonNull String
, @NonNull ITmfStateValue
> fields
) {
70 fSegmentName
= segmentName
;
71 fContent
= Collections
.unmodifiableMap(fields
);
75 * Get the start timestamp of the segment
77 * @return The start timestamp
79 public @NonNull ITmfTimestamp
getTimestampStart() {
80 return TmfTimestamp
.create(fStart
, fScale
);
84 * Get the end timestamp of this segment
86 * @return The end timestamp
88 public @NonNull ITmfTimestamp
getTimestampEnd() {
89 return TmfTimestamp
.create(fEnd
, fScale
);
93 * Get the content of the pattern segment
96 public Map
<@NonNull String
, @NonNull ITmfStateValue
> getContent() {
101 * Get the name of pattern segment
104 public String
getName() {
109 * Get the timestamp scale of the pattern segment
110 * @return The timestamp scale
112 public int getScale() {
117 public int compareTo(@NonNull ISegment o
) {
118 int ret
= ISegment
.super.compareTo(o
);
122 return toString().compareTo(o
.toString());
126 public long getStart() {
131 public long getEnd() {
136 public String
toString() {
137 return new StringBuilder(getClass().getSimpleName())
138 .append(", [fTimestampStart=").append(getTimestampStart()) //$NON-NLS-1$
139 .append(", fTimestampEnd=").append(getTimestampEnd()) //$NON-NLS-1$
140 .append(", duration= ").append(getLength()) //$NON-NLS-1$
141 .append(", fName=").append(getName()) //$NON-NLS-1$
142 .append(", fContent=").append(getContent()) //$NON-NLS-1$
143 .append("]").toString(); //$NON-NLS-1$
146 private void writeObject(ObjectOutputStream out
) throws IOException
{
147 out
.defaultWriteObject();
149 // Write the number of fields
150 out
.writeInt(fContent
.size());
153 for (Map
.Entry
<String
, ITmfStateValue
> entry
: fContent
.entrySet()) {
154 out
.writeInt(entry
.getKey().length());
155 out
.writeBytes(entry
.getKey());
156 final ITmfStateValue value
= entry
.getValue();
157 final byte type
= getByteFromType(value
.getType());
163 out
.writeInt(value
.unboxInt());
166 out
.writeLong(value
.unboxLong());
169 final @NonNull String string
= value
.unboxStr();
170 out
.writeInt(string
.length());
171 out
.writeBytes(string
);
174 throw new IOException("Write object failed : Invalid data"); //$NON-NLS-1$
179 private void readObject(ObjectInputStream in
) throws IOException
, ClassNotFoundException
{
180 in
.defaultReadObject();
181 int contentSize
= in
.readInt();
183 final Map
<@NonNull String
, @NonNull ITmfStateValue
> content
= new HashMap
<>();
184 for (int i
= 0; i
< contentSize
; i
++) {
185 int length
= in
.readInt();
186 byte[] bytes
= new byte[length
];
187 in
.read(bytes
, 0, length
);
188 String name
= new String(bytes
).intern();
190 Byte type
= in
.readByte();
191 ITmfStateValue value
;
194 value
= TmfStateValue
.nullValue();
197 value
= TmfStateValue
.newValueInt(in
.readInt());
200 value
= TmfStateValue
.newValueLong(in
.readLong());
203 length
= in
.readInt();
204 bytes
= new byte[length
];
205 in
.read(bytes
, 0, length
);
206 value
= TmfStateValue
.newValueString(new String(bytes
).intern());
209 throw new IOException("Read object failed : Invalid data"); //$NON-NLS-1$
211 content
.put(name
, value
);
217 * Here we determine how state values "types" are written in the 8-bit field
218 * that indicates the value type in the file.
220 private static byte getByteFromType(ITmfStateValue
.Type type
) {
232 /* Should not happen if the switch is fully covered */
233 throw new IllegalStateException("Data type " + type
+ " not supported"); //$NON-NLS-1$ //$NON-NLS-2$
This page took 0.034639 seconds and 4 git commands to generate.