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
.internal
.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
31 public class TmfXmlPatternSegment
implements ISegment
{
34 * The serial version UID
36 private static final long serialVersionUID
= 3556323761465412078L;
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;
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
;
51 * Constructs an XML pattern segment
54 * Start time of the pattern segment
56 * End time of the pattern segment
58 * Scale of the pattern segment
60 * Name of the pattern segment
62 * Fields of the pattern segment
64 public TmfXmlPatternSegment(long start
, long end
, int scale
, String segmentName
, @NonNull Map
<@NonNull String
, @NonNull ITmfStateValue
> fields
) {
68 fSegmentName
= segmentName
;
69 fContent
= Collections
.unmodifiableMap(fields
);
73 * Get the start timestamp of the segment
75 * @return The start timestamp
77 public @NonNull ITmfTimestamp
getTimestampStart() {
78 return TmfTimestamp
.create(fStart
, fScale
);
82 * Get the end timestamp of this segment
84 * @return The end timestamp
86 public @NonNull ITmfTimestamp
getTimestampEnd() {
87 return TmfTimestamp
.create(fEnd
, fScale
);
91 * Get the content of the pattern segment
94 public Map
<@NonNull String
, @NonNull ITmfStateValue
> getContent() {
99 * Get the name of pattern segment
102 public String
getName() {
107 * Get the timestamp scale of the pattern segment
108 * @return The timestamp scale
110 public int getScale() {
115 public int compareTo(@NonNull ISegment o
) {
116 int ret
= ISegment
.super.compareTo(o
);
120 return toString().compareTo(o
.toString());
124 public long getStart() {
129 public long getEnd() {
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$
144 private void writeObject(ObjectOutputStream out
) throws IOException
{
145 out
.defaultWriteObject();
147 // Write the number of fields
148 out
.writeInt(fContent
.size());
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());
161 out
.writeInt(value
.unboxInt());
164 out
.writeLong(value
.unboxLong());
167 final @NonNull String string
= value
.unboxStr();
168 out
.writeInt(string
.length());
169 out
.writeBytes(string
);
172 throw new IOException("Write object failed : Invalid data"); //$NON-NLS-1$
177 private void readObject(ObjectInputStream in
) throws IOException
, ClassNotFoundException
{
178 in
.defaultReadObject();
179 int contentSize
= in
.readInt();
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();
188 Byte type
= in
.readByte();
189 ITmfStateValue value
;
192 value
= TmfStateValue
.nullValue();
195 value
= TmfStateValue
.newValueInt(in
.readInt());
198 value
= TmfStateValue
.newValueLong(in
.readLong());
201 length
= in
.readInt();
202 bytes
= new byte[length
];
203 in
.read(bytes
, 0, length
);
204 value
= TmfStateValue
.newValueString(new String(bytes
).intern());
207 throw new IOException("Read object failed : Invalid data"); //$NON-NLS-1$
209 content
.put(name
, value
);
215 * Here we determine how state values "types" are written in the 8-bit field
216 * that indicates the value type in the file.
218 private static byte getByteFromType(ITmfStateValue
.Type type
) {
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$
This page took 0.036075 seconds and 5 git commands to generate.