use of org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue in project tracecompass by tracecompass.
the class CustomStateValueTest method testNoFactoryAvailable.
/**
* Test serialization/unserialization if no factory is available
*/
@Test
public void testNoFactoryAvailable() {
CustomStateValueStub.unregisterFactory();
ByteBuffer buffer = ByteBuffer.allocate(1024);
ISafeByteBufferWriter safeBufferWriter = SafeByteBufferFactory.wrapWriter(buffer, VALUE.getSerializedSize());
VALUE.serialize(safeBufferWriter);
// Reset buffer
buffer.flip();
ISafeByteBufferReader safeBufferReader = SafeByteBufferFactory.wrapReader(buffer, VALUE.getSerializedSize());
TmfStateValue read = CustomStateValue.readSerializedValue(safeBufferReader);
assertEquals("Custom state value: no factory", TmfStateValue.nullValue(), read);
}
use of org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue in project tracecompass by tracecompass.
the class CustomStateValueTest method testSerialization.
/**
* Test serialization/unserialization with the factory present
*/
@Test
public void testSerialization() {
CustomStateValueStub.registerFactory();
ByteBuffer buffer = ByteBuffer.allocate(1024);
ISafeByteBufferWriter safeBufferWriter = SafeByteBufferFactory.wrapWriter(buffer, VALUE.getSerializedSize());
VALUE.serialize(safeBufferWriter);
// Reset buffer
buffer.flip();
ISafeByteBufferReader safeBufferReader = SafeByteBufferFactory.wrapReader(buffer, VALUE.getSerializedSize());
TmfStateValue read = CustomStateValue.readSerializedValue(safeBufferReader);
assertEquals("Custom state value: no factory", VALUE, read);
}
use of org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue in project tracecompass by tracecompass.
the class TmfIntervalDeserializer method deserialize.
@Override
public ITmfStateInterval deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
JsonObject object = json.getAsJsonObject();
long start = object.get(TmfIntervalStrings.START).getAsLong();
long end = object.get(TmfIntervalStrings.END).getAsLong();
int quark = object.get(TmfIntervalStrings.QUARK).getAsInt();
String type = object.get(TmfIntervalStrings.TYPE).getAsString();
if (type.equals(TmfIntervalStrings.NULL)) {
return new TmfStateInterval(start, end, quark, (Object) null);
}
JsonElement value = object.get(TmfIntervalStrings.VALUE);
try {
Class<?> typeClass = Class.forName(type);
if (typeClass.isAssignableFrom(CustomStateValue.class)) {
String encoded = value.getAsString();
byte[] serialized = Base64.getDecoder().decode(encoded);
ByteBuffer buffer = ByteBuffer.wrap(serialized);
ISafeByteBufferReader sbbr = SafeByteBufferFactory.wrapReader(buffer, serialized.length);
TmfStateValue sv = CustomStateValue.readSerializedValue(sbbr);
return new TmfStateInterval(start, end, quark, sv.unboxValue());
}
if (typeClass.isAssignableFrom(Integer.class)) {
return new TmfStateInterval(start, end, quark, value.getAsInt());
} else if (typeClass.isAssignableFrom(Long.class)) {
return new TmfStateInterval(start, end, quark, value.getAsLong());
} else if (typeClass.isAssignableFrom(Double.class)) {
return new TmfStateInterval(start, end, quark, value.getAsDouble());
} else if (typeClass.isAssignableFrom(String.class)) {
return new TmfStateInterval(start, end, quark, value.getAsString());
}
} catch (ClassNotFoundException e) {
// Fall through
}
// last ditch attempt
return new TmfStateInterval(start, end, quark, value.toString());
}
Aggregations