I have the following class ProtokollEvent
public class ProtokollEvent extends Event {
//variable holds all devices in given protokoll
private ObservableList<Device> devicesList;
//variable holds SaveResult
private SaveResult result;
//final ProtokollEvents
public static final EventType<ProtokollEvent> PROTOKOLL_SAVE = new EventType(ANY, "PROTOKOLL_SAVE");
public static final EventType<ProtokollEvent> PROTOKOLL_SAVE_DONE = new EventType(ANY, "PROTOKOLL_SAVE_DONE");
public static final EventType<ProtokollEvent> PROTOKOLL_UPDATED = new EventType(ANY, "PROTOKOLL_UPDATED");
public static final EventType<ProtokollEvent> PROTOKOLL_DELETED = new EventType(ANY, "PROTOKOLL_DELETED");
public ProtokollEvent() {
this(PROTOKOLL_SAVE);
}
public ProtokollEvent(EventType<? extends Event> arg0) {
super(arg0);
}
public ProtokollEvent(Object arg0, EventTarget arg1, EventType<? extends Event> arg2) {
super(arg0, arg1, arg2);
}
/**
* getDevices will return current {#link Device} as ObservableList
*
* #return {#link ObservableList} of type {#link Device}
*/
public ObservableList getDev() {
return devicesList;
}
/**
* setDevices will set devicesList
* #param devices ObservableList {#link Device}
*/
public void setDev(ObservableList devices) {
this.devicesList = devices;
}
/**
* get the result which is returned from calling saveProtokoll
* #return result {#link SaveResult}
*/
public SaveResult getResult() {
return result;
}
/**
* set the result which is returned after calling saveMessprotokoll in RestCall
* #param result {#link SaveResult}
*/
public void setResult(SaveResult result) {
this.result = result;
}
}
in the second class
public class SaveUtils {
private MainWindowController controller;
private ObservableList<RowContainerPruefvorschriftController> rows;
private Protokoll lastSavedProtokoll;
private Protokoll currentSavingProtokoll;
public SaveUtils(MainWindowController control){
this.controller = control;
}
private void startSaving(){
currentSavingProtokoll = createProtokoll();
boolean state = controller.networkOnline.get() ? saveOnline() :saveOffline();
}
public void setRows(ObservableList<RowContainerPruefvorschriftController> rows) {
this.rows = rows;
//if rows get set start saveing the data
startSaving();
}
private boolean saveOffline(){
return false;
}
private boolean saveOnline() {
RestCall call = controller.getCall();
//call saveMessprotokoll and look what SaveResult returns
SaveResult result = call.saveMessprotokoll(currentSavingProtokoll);
//create ProtokollEvent to tell all consumers if all was ok
ProtokollEvent save = new ProtokollEvent(ProtokollEvent.PROTOKOLL_SAVE_DONE);
save.setResult(result);
//HOW to fire/dispatch the ProtokollEvent here??????
//TODO: need to fire this event and listen for it in other classes
if(result.getResult()>0){
controller.setLoggerMessage("SavedOnline->Protokoll-Nr.:"+result.getProtokollnr());
}
else {
controller.setLoggerMessage("SavedOnline not successful->Error:"+result.getError_message());
}
return true;
}
}
in the saveOnline function i create a ProtokollEvent and pass it some values.
What i now want is to fire or dispatch this event so other code parts could listen to this.
I tried with fireEvent() but as i understood the Oracle-DOCs only NODEs, Windows and Scene are able to do so. But how could i solve it with my custom class?
Additional i ask myself whats is the difference between fireEvent and dispatchEvent?
Found the solution.
All events could be fired through the static method
Event.fireEvent(EventTarget eventTarget,Event event)
where eventTarget specifies the path through which the event will travel (taken from java docs).
So for my example i just added the following line
Event.fireEvent(controller.getMainWindowBorderPane(),save);
did it...
Related
I'm trying to convert a HashMap<String, Object> to an Avro record. I get this runtime exception when I do a
DataStream<AvroRecord> dsRpvSchema = filteredVlfRPV.flatMap(new MessageFlattener()) .name("ToAvroSchema").uid("ToAvroSchema").startNewChain();
Not sure what the issue is with Flink converting the valid avro record to Flink Avro.
java.lang.IllegalStateException: Expecting type to be a PojoTypeInfo
at org.apache.flink.formats.avro.typeutils.AvroTypeInfo.generateFieldsFromAvroSchema(AvroTypeInfo.java:71)
at org.apache.flink.formats.avro.typeutils.AvroTypeInfo.<init>(AvroTypeInfo.java:55)
at org.apache.flink.formats.avro.utils.AvroKryoSerializerUtils.createAvroTypeInfo(AvroKryoSerializerUtils.java:81)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateGetForClass(TypeExtractor.java:1653)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateGetForClass(TypeExtractor.java:1559)
at org.apache.flink.api.java.typeutils.TypeExtractor.createTypeInfoWithTypeHierarchy(TypeExtractor.java:866)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateCreateTypeInfo(TypeExtractor.java:747)
at org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:531)
at org.apache.flink.api.java.typeutils.TypeExtractor.getFlatMapReturnTypes(TypeExtractor.java:168)
at org.apache.flink.streaming.api.datastream.DataStream.flatMap(DataStream.java:637)
I do see two questions on this but no answers provided:
Flink Kafka : Expecting type to be a PojoTypeInfo
Deserialize Avro from kafka as SpecificRecord Failing. Expecting type to be a PojoTypeInfo
UPDATE: 02/01/2022. Below is the AvroRecord class definition.
import org.apache.avro.generic.GenericArray;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.Utf8;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
#org.apache.avro.specific.AvroGenerated
public class AvroRecord extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = 9071485731787422200L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"AvroRecord\",\"namespace\":\"com.sample.dataplatform.stream.avro\",\"fields\":[{\"name\":\"_id\",\"type\":\"string\",\"doc\":\"car_id\"}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<AvroRecord> ENCODER =
new BinaryMessageEncoder<AvroRecord>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<AvroRecord> DECODER =
new BinaryMessageDecoder<AvroRecord>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageEncoder instance used by this class.
* #return the message encoder used by this class
*/
public static BinaryMessageEncoder<AvroRecord> getEncoder() {
return ENCODER;
}
/**
* Return the BinaryMessageDecoder instance used by this class.
* #return the message decoder used by this class
*/
public static BinaryMessageDecoder<AvroRecord> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {#link SchemaStore}.
* #param resolver a {#link SchemaStore} used to find schemas by fingerprint
* #return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
*/
public static BinaryMessageDecoder<AvroRecord> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<AvroRecord>(MODEL$, SCHEMA$, resolver);
}
/**
* Serializes this AvroRecord to a ByteBuffer.
* #return a buffer holding the serialized data for this instance
* #throws java.io.IOException if this instance could not be serialized
*/
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/**
* Deserializes a AvroRecord from a ByteBuffer.
* #param b a byte buffer holding serialized data for an instance of this class
* #return a AvroRecord instance decoded from the given buffer
* #throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
*/
public static AvroRecord fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
/** car_id */
private java.lang.CharSequence _id;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public AvroRecord() {}
/**
* All-args constructor.
* #param _id car_id
*/
public AvroRecord(java.lang.CharSequence _id) {
this._id = _id;
}
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return _id;
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
}
}
// Used by DatumReader. Applications should not call.
#SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: _id = (java.lang.CharSequence)value$; break;
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
}
}
/**
* Gets the value of the '_id' field.
* #return car_id
*/
public java.lang.CharSequence getId$1() {
return _id;
}
/**
* Sets the value of the '_id' field.
* car_id
* #param value the value to set.
*/
public void setId$1(java.lang.CharSequence value) {
this._id = value;
}
/**
* Creates a new AvroRecord RecordBuilder.
* #return A new AvroRecord RecordBuilder
*/
public static com.sample.dataplatform.stream.avro.AvroRecord.Builder newBuilder() {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder();
}
/**
* Creates a new AvroRecord RecordBuilder by copying an existing Builder.
* #param other The existing builder to copy.
* #return A new AvroRecord RecordBuilder
*/
public static com.sample.dataplatform.stream.avro.AvroRecord.Builder newBuilder(com.sample.dataplatform.stream.avro.AvroRecord.Builder other) {
if (other == null) {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder();
} else {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder(other);
}
}
/**
* Creates a new AvroRecord RecordBuilder by copying an existing AvroRecord instance.
* #param other The existing instance to copy.
* #return A new AvroRecord RecordBuilder
*/
public static com.sample.dataplatform.stream.avro.AvroRecord.Builder newBuilder(com.sample.dataplatform.stream.avro.AvroRecord other) {
if (other == null) {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder();
} else {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder(other);
}
}
/**
* RecordBuilder for AvroRecord instances.
*/
#org.apache.avro.specific.AvroGenerated
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<AvroRecord>
implements org.apache.avro.data.RecordBuilder<AvroRecord> {
/** car_id */
private java.lang.CharSequence _id;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* #param other The existing Builder to copy.
*/
private Builder(com.sample.dataplatform.stream.avro.AvroRecord.Builder other) {
super(other);
if (isValidValue(fields()[0], other._id)) {
this._id = data().deepCopy(fields()[0].schema(), other._id);
fieldSetFlags()[0] = other.fieldSetFlags()[0];
}
}
/**
* Creates a Builder by copying an existing AvroRecord instance
* #param other The existing instance to copy.
*/
private Builder(com.sample.dataplatform.stream.avro.AvroRecord other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other._id)) {
this._id = data().deepCopy(fields()[0].schema(), other._id);
fieldSetFlags()[0] = true;
}
}
/**
* Gets the value of the '_id' field.
* car_id
* #return The value.
*/
public java.lang.CharSequence getId$1() {
return _id;
}
/**
* Sets the value of the '_id' field.
* car_id
* #param value The value of '_id'.
* #return This builder.
*/
public com.sample.dataplatform.stream.avro.AvroRecord.Builder setId$1(java.lang.CharSequence value) {
validate(fields()[0], value);
this._id = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the '_id' field has been set.
* car_id
* #return True if the '_id' field has been set, false otherwise.
*/
public boolean hasId$1() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the '_id' field.
* car_id
* #return This builder.
*/
public com.sample.dataplatform.stream.avro.AvroRecord.Builder clearId$1() {
_id = null;
fieldSetFlags()[0] = false;
return this;
}
#Override
#SuppressWarnings("unchecked")
public AvroRecord build() {
try {
AvroRecord record = new AvroRecord();
record._id = fieldSetFlags()[0] ? this._id : (java.lang.CharSequence) defaultValue(fields()[0]);
return record;
} catch (org.apache.avro.AvroMissingFieldException e) {
throw e;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<AvroRecord>
WRITER$ = (org.apache.avro.io.DatumWriter<AvroRecord>)MODEL$.createDatumWriter(SCHEMA$);
#Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<AvroRecord>
READER$ = (org.apache.avro.io.DatumReader<AvroRecord>)MODEL$.createDatumReader(SCHEMA$);
#Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
#Override protected boolean hasCustomCoders() { return true; }
#Override public void customEncode(org.apache.avro.io.Encoder out)
throws java.io.IOException
{
out.writeString(this._id);
}
#Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
throws java.io.IOException
{
org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
if (fieldOrder == null) {
this._id = in.readString(this._id instanceof Utf8 ? (Utf8)this._id : null);
} else {
for (int i = 0; i < 1; i++) {
switch (fieldOrder[i].pos()) {
case 0:
this._id = in.readString(this._id instanceof Utf8 ? (Utf8)this._id : null);
break;
default:
throw new java.io.IOException("Corrupt ResolvingDecoder.");
}
}
}
}
}
The AVRO schema (.avsc) file looks like this:
{
"namespace": "com.sample.dataplatform.stream.avro",
"type": "record",
"name": "AvroRecord",
"fields": [
{
"name": "_id",
"type": "string",
"doc": "car_id"
}
]
}
Any help is appreciated!
I'm having a strange issue with marshalling/unmarshalling an avro generated class. The error I'm getting is throwing a not an enum error - except there aren't any enum's in my class.
The error is specifically this:
com.fasterxml.jackson.databind.JsonMappingException: Not an enum: {"type":"record","name":"TimeUpdateTopic","namespace":"org.company.mmd.time","fields":[{"name":"time","type":"double"}]} (through reference chain: org.company.mmd.time.TimeUpdateTopic["schema"]->org.apache.avro.Schema$RecordSchema["enumDefault"])
Test Case
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.junit.Test
class TimeUpdateTopicTest {
val objectMapper = ObjectMapper().registerModule(JavaTimeModule())
#Test
fun decode() {
val t = TimeUpdateTopic(1.0)
objectMapper.writeValueAsString(t)
}
}
AVDL
#namespace("org.company.mmd.time")
protocol TimeMonitor {
record TimeUpdateTopic {
double time;
}
}
Java class generated by the avro task
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package org.company.mmd.time;
import org.apache.avro.generic.GenericArray;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.Utf8;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
#org.apache.avro.specific.AvroGenerated
public class TimeUpdateTopic extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = -4648318619505855037L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"TimeUpdateTopic\",\"namespace\":\"org.company.mmd.time\",\"fields\":[{\"name\":\"time\",\"type\":\"double\"}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<TimeUpdateTopic> ENCODER =
new BinaryMessageEncoder<TimeUpdateTopic>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<TimeUpdateTopic> DECODER =
new BinaryMessageDecoder<TimeUpdateTopic>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageEncoder instance used by this class.
* #return the message encoder used by this class
*/
public static BinaryMessageEncoder<TimeUpdateTopic> getEncoder() {
return ENCODER;
}
/**
* Return the BinaryMessageDecoder instance used by this class.
* #return the message decoder used by this class
*/
public static BinaryMessageDecoder<TimeUpdateTopic> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {#link SchemaStore}.
* #param resolver a {#link SchemaStore} used to find schemas by fingerprint
* #return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
*/
public static BinaryMessageDecoder<TimeUpdateTopic> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<TimeUpdateTopic>(MODEL$, SCHEMA$, resolver);
}
/**
* Serializes this TimeUpdateTopic to a ByteBuffer.
* #return a buffer holding the serialized data for this instance
* #throws java.io.IOException if this instance could not be serialized
*/
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/**
* Deserializes a TimeUpdateTopic from a ByteBuffer.
* #param b a byte buffer holding serialized data for an instance of this class
* #return a TimeUpdateTopic instance decoded from the given buffer
* #throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
*/
public static TimeUpdateTopic fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
#Deprecated public double time;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public TimeUpdateTopic() {}
/**
* All-args constructor.
* #param time The new value for time
*/
public TimeUpdateTopic(java.lang.Double time) {
this.time = time;
}
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return time;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
// Used by DatumReader. Applications should not call.
#SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: time = (java.lang.Double)value$; break;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
/**
* Gets the value of the 'time' field.
* #return The value of the 'time' field.
*/
public double getTime() {
return time;
}
/**
* Sets the value of the 'time' field.
* #param value the value to set.
*/
public void setTime(double value) {
this.time = value;
}
/**
* Creates a new TimeUpdateTopic RecordBuilder.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder() {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
}
/**
* Creates a new TimeUpdateTopic RecordBuilder by copying an existing Builder.
* #param other The existing builder to copy.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder(org.company.mmd.time.TimeUpdateTopic.Builder other) {
if (other == null) {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
} else {
return new org.company.mmd.time.TimeUpdateTopic.Builder(other);
}
}
/**
* Creates a new TimeUpdateTopic RecordBuilder by copying an existing TimeUpdateTopic instance.
* #param other The existing instance to copy.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder(org.company.mmd.time.TimeUpdateTopic other) {
if (other == null) {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
} else {
return new org.company.mmd.time.TimeUpdateTopic.Builder(other);
}
}
/**
* RecordBuilder for TimeUpdateTopic instances.
*/
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<TimeUpdateTopic>
implements org.apache.avro.data.RecordBuilder<TimeUpdateTopic> {
private double time;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* #param other The existing Builder to copy.
*/
private Builder(org.company.mmd.time.TimeUpdateTopic.Builder other) {
super(other);
if (isValidValue(fields()[0], other.time)) {
this.time = data().deepCopy(fields()[0].schema(), other.time);
fieldSetFlags()[0] = other.fieldSetFlags()[0];
}
}
/**
* Creates a Builder by copying an existing TimeUpdateTopic instance
* #param other The existing instance to copy.
*/
private Builder(org.company.mmd.time.TimeUpdateTopic other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other.time)) {
this.time = data().deepCopy(fields()[0].schema(), other.time);
fieldSetFlags()[0] = true;
}
}
/**
* Gets the value of the 'time' field.
* #return The value.
*/
public double getTime() {
return time;
}
/**
* Sets the value of the 'time' field.
* #param value The value of 'time'.
* #return This builder.
*/
public org.company.mmd.time.TimeUpdateTopic.Builder setTime(double value) {
validate(fields()[0], value);
this.time = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the 'time' field has been set.
* #return True if the 'time' field has been set, false otherwise.
*/
public boolean hasTime() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the 'time' field.
* #return This builder.
*/
public org.company.mmd.time.TimeUpdateTopic.Builder clearTime() {
fieldSetFlags()[0] = false;
return this;
}
#Override
#SuppressWarnings("unchecked")
public TimeUpdateTopic build() {
try {
TimeUpdateTopic record = new TimeUpdateTopic();
record.time = fieldSetFlags()[0] ? this.time : (java.lang.Double) defaultValue(fields()[0]);
return record;
} catch (org.apache.avro.AvroMissingFieldException e) {
throw e;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<TimeUpdateTopic>
WRITER$ = (org.apache.avro.io.DatumWriter<TimeUpdateTopic>)MODEL$.createDatumWriter(SCHEMA$);
#Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<TimeUpdateTopic>
READER$ = (org.apache.avro.io.DatumReader<TimeUpdateTopic>)MODEL$.createDatumReader(SCHEMA$);
#Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
#Override protected boolean hasCustomCoders() { return true; }
#Override public void customEncode(org.apache.avro.io.Encoder out)
throws java.io.IOException
{
out.writeDouble(this.time);
}
#Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
throws java.io.IOException
{
org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
if (fieldOrder == null) {
this.time = in.readDouble();
} else {
for (int i = 0; i < 1; i++) {
switch (fieldOrder[i].pos()) {
case 0:
this.time = in.readDouble();
break;
default:
throw new java.io.IOException("Corrupt ResolvingDecoder.");
}
}
}
}
}
Am I doing something stupid and/or wrong here? Or is this an actual bug
Updates
I'm able to get JSON out using this function:
inline fun <reified T: SpecificRecordBase> StringFromAvroGenerated(obj: T) : String {
val schema = obj.schema
val writer = SpecificDatumWriter(T::class.java)
val stream = ByteArrayOutputStream()
var jsonEncoder = EncoderFactory.get().jsonEncoder(schema, stream)
writer.write(obj, jsonEncoder)
jsonEncoder.flush()
return stream.toString("UTF-8")
}
but I was assuming this should be automatic with Jackson
So it appears there are two ways to solve my issues (thanks to JsonMappingException when serializing avro generated object to json)
Write a Jackson MixIn to handle the getSchema call
So the first option required me to create a Mixin such as this:
abstract class AvroMixIn {
#JsonIgnore
abstract fun getSchema(): org.apache.avro.Schema
#JsonIgnore
abstract fun getSpecificData() : org.apache.avro.specific.SpecificData
}
And then when i make an object mapper:
val objectMapper = ObjectMapper()
.registerModule(JavaTimeModule())
.addMixIn(Object::class.java, AvroMixIn::class.java)
I chose Object::class.java instead of the actual class because it should apply to all classes. Probably a better solution is to apply it to a shared base-class all the AvroGenerated stuff has.
Rewrite the Avro Velocity Templates to automatically add this
This is actually the 1st approach I took because it seemed more "seemless".
1) Check out the avro project
2) Copy enum.vm, fixed.vm, protocol.vm, record.vm into a /avro_templates directory off the main root of my project
3) Add the #com.fasterxml.jackson.annotation.JsonIgnore property to the template:
#end
#com.fasterxml.jackson.annotation.JsonIgnore
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
#com.fasterxml.jackson.annotation.JsonIgnore
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
4) Update the gradle task:
avro {
dateTimeLogicalType="JSR310"
templateDirectory = "avro_templates/"
}
5) re-build avro classes
(everything now works)
I have generated a sample Multi-page Editor through the Eclipse wizard. Then, I have modified the sample plugin in order to have two pages:
Text Editor
Master Details Block
For the Master Details Block, I have used this tutorial.
I can open the Master Details Block page and I am also able to view the initialized objects in the list and display the corresponding details page. Now, I want to replace the static object with entries from the loaded file. My problem is, that I don't know how I can parse these entries from the text file. Do I need to implement my own parser, including the file handling or is this already implemented through a IFileEditorInput interface?
In my ScrolledPropertiesBlock class, I call the method viewer.setContentProvider(new MasterContentProvider());. I am sure that I need to modify the MasterContentProvider class implementation. So far, I have this:
class MasterContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof FileEditorInput) {
//MyEditorInput input = (MyEditorInput) inputElement;
MyEditorInput input = new MyEditorInput("test");
return input.getModel().getContents();
}
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
If I delete the line MyEditorInput input = new MyEditorInput("test"); and do my cast instead, I get the following exception:
java.lang.ClassCastException: org.eclipse.ui.part.FileEditorInput cannot be cast to my.plugin.editor.MyEditorInput
Do I need to have a MyEditorInput which extends FormEditorInput (like in the example) and then implements IFileEditorInput?
public class MyEditorInput extends FormEditorInput {
private SimpleModel model;
public MyEditorInput(String name) {
super(name);
model = new SimpleModel();
}
public SimpleModel getModel() {
return model;
}
}
public class FormEditorInput implements IEditorInput {
private String name;
public FormEditorInput(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#exists()
*/
public boolean exists() {
return true;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getImageDescriptor()
*/
public ImageDescriptor getImageDescriptor() {
return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
ISharedImages.IMG_OBJ_ELEMENT);
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getName()
*/
public String getName() {
return name;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getPersistable()
*/
public IPersistableElement getPersistable() {
return null;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getToolTipText()
*/
public String getToolTipText() {
return getName();
}
/*
* (non-Javadoc)
*
* #see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
*/
public Object getAdapter(Class adapter) {
return null;
}
}
The SimpleModel class looks like this:
public class SimpleModel {
private ArrayList modelListeners;
private ArrayList objects;
public SimpleModel() {
modelListeners = new ArrayList();
initialize();
}
public void addModelListener(IModelListener listener) {
if (!modelListeners.contains(listener))
modelListeners.add(listener);
}
public void removeModelListener(IModelListener listener) {
modelListeners.remove(listener);
}
public void fireModelChanged(Object[] objects, String type, String property) {
for (int i = 0; i < modelListeners.size(); i++) {
((IModelListener) modelListeners.get(i)).modelChanged(objects,
type, property);
}
}
public Object[] getContents() {
return objects.toArray();
}
private void initialize() {
objects = new ArrayList();
NamedObject[] objects = {
new TypeOne(Messages.getString("SimpleModel.t1_i1"), 2, true, Messages.getString("SimpleModel.text1")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i2"), 1, false, Messages.getString("SimpleModel.text2")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i3"), 3, true, Messages.getString("SimpleModel.text3")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i4"), 0, false, Messages.getString("SimpleModel.text4")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i5"), 1, true, Messages.getString("SimpleModel.text5")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeTwo(Messages.getString("SimpleModel.t2_i1"), false, true), //$NON-NLS-1$
new TypeTwo(Messages.getString("SimpleModel.t2_i2"), true, false)}; //$NON-NLS-1$
add(objects, false);
}
public void add(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.add(objs[i]);
objs[i].setModel(this);
}
if (notify)
fireModelChanged(objs, IModelListener.ADDED, ""); //$NON-NLS-1$
}
public void remove(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.remove(objs[i]);
objs[i].setModel(null);
}
if (notify)
fireModelChanged(objs, IModelListener.REMOVED, ""); //$NON-NLS-1$
}
}
Eclipse gives you the input in an IEditorInput - this probably be an IFileEditorInput if the file is in the workspace. You can't cast this to something else. Creating new editor inputs does not help.
So you will have to read and parse the contents of the file from the editor input Eclipse gives you.
For a IFileEditorInput you can call getFile to get the input IFile. You can then call IFile.getContents to get an input stream containing the file's contents.
I recently needed to implement a slider in GWT to capture the percentage progress an user had made on a task. I was not happy with the slider from the GWT Incubator and was not too keen on using an external library like spiffy UI or SmartGWT. What alternatives could I use to implement an effective slider in GWT without doing too much donkey work ?
After a fair amount of searching I decided on going with a JQuery-Ui slider that would be implemented through a java wrapper class. The interaction between GWT and the JQuery slider would be through the GWT Java Script Native Interface. To keep the additions to the required library as small as possible I downloaded a custom Jquery-Ui package (just core+slider) which was pretty light weight. The result was satisfactory for our needs, fitted into our MVP design pattern and was UI Bound.
The slider wrapper was as follows :
package za.co.bsg.ems.client.framework.ui.slider;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Widget;
import java.util.ArrayList;
import java.util.List;
/**
* This widget wraps the JQuery UI Slider and allows for single slider .
*
* All options can be get or set using generic get/setIntOption, get/setStringOption, get/setBooleanOption
* methods, but some convenience methods are provided for most popular such as
* setValues and setMinimum and setMaximum. See SliderOptions for full list of options.
* #see SliderOption
*
*/
#SuppressWarnings("Convert2Diamond")
public class SingleSlider extends Widget {
private JSONObject defaultOptions;
private List<SliderListener> listeners = new ArrayList<SliderListener>();
/**
* Create the default slider with the specified ID. The ID is required
* because the slider needs a specific ID to connect to.
* #param id - id of the element to create
*/
public SingleSlider(String id) {
this(id, null);
}
/**
* Create a slider with the specified ID. The ID is required
* because the slider needs a specific ID to connect to.
* #param id - id of the element to create
* #param options - JSONObject of any possible option, can be null for defaults
*/
public SingleSlider(String id, JSONObject options) {
super();
Element divEle = DOM.createDiv();
setElement(divEle);
divEle.setId(id);
defaultOptions = options;
if (defaultOptions == null) {
defaultOptions = getOptions(0, 100, 0);
}
}
/**
* A convenient way to create an options JSONObject. Use SliderOption for keys.
* #param min - default minimum of the slider
* #param max - default maximum of the slider
* #param defaultValue - default point of anchor
* #return a JSONObject of Slider options
*/
public static JSONObject getOptions(int min, int max, int defaultValue) {
JSONObject options = new JSONObject();
options.put(SliderOption.MIN.toString(), new JSONNumber(min));
options.put(SliderOption.MAX.toString(), new JSONNumber(max));
options.put(SliderOption.VALUE.toString(), new JSONNumber(defaultValue));
options.put(SliderOption.RANGE.toString(), new JSONString("min"));
return options;
}
#Override
protected void onLoad() {
createSliderJS(this, getElement().getId(), defaultOptions.getJavaScriptObject());
super.onLoad();
}
#Override
protected void onUnload() {
destroySliderJS(this, getElement().getId());
super.onUnload();
}
/**
* Gets the minimum possible value for the slider
* #return Returns the minimum.
*/
public int getMinimum() {
return getIntOptionJS(getElement().getId(), SliderOption.MIN.toString());
}
/**
* Sets the minimum possible value for the slider
* #param minimum The minimum to set.
*/
public void setMinimum(int minimum) {
setIntOptionJS(getElement().getId(), SliderOption.MIN.toString(), minimum);
}
/**
* Gets the maximum possible value for the slider
* #return Returns the maximum.
*/
public int getMaximum() {
return getIntOptionJS(getElement().getId(), SliderOption.MAX.toString());
}
/**
* Sets the maximum possible value for the slider
* #param maximum The maximum to set.
*/
public void setMaximum(int maximum) {
setIntOptionJS(getElement().getId(), SliderOption.MAX.toString(), maximum);
}
/**
* Convenience method for only 1 anchor
* #param value to set.
*/
public void setValue(int value) {
setValueJS(getElement().getId(), value);
}
/**
* Set an option numeric value
* #param option the SliderOption
* #param value the numeric
*/
public void setIntOption(SliderOption option, int value) {
setIntOptionJS(getElement().getId(), option.toString(), value);
}
/**
* Get an option numeric value
* #param option the SliderOption
* #return value the numeric
*/
public int getIntOption(SliderOption option) {
return getIntOptionJS(getElement().getId(), option.toString());
}
/**
* Set an option boolean value
* #param option the SliderOption
* #param value the boolean
*/
public void setBooleanOption(SliderOption option, boolean value) {
setBooleanOptionJS(getElement().getId(), option.toString(), value);
}
/**
* Get an option boolean value
* #param option the SliderOption
* #return value the boolean
*/
public boolean getBooleanOption(SliderOption option) {
return getBooleanOptionJS(getElement().getId(), option.toString());
}
/**
* Set an option string value
* #param option the SliderOption
* #param value the String
*/
public void setStringOption(SliderOption option, String value) {
setStringOptionJS(getElement().getId(), option.toString(), value);
}
/**
* Set an option string value
* #param option the SliderOption
* #return value the String
*/
public String setStringOption(SliderOption option) {
return getStringOptionJS(getElement().getId(), option.toString());
}
/**
* Add a SliderListener
* #param l - SliderListener
*/
public void addListener(SliderListener l) {
listeners.add(l);
}
/**
* Removes the SliderListener
* #param l - SliderListener
*/
public void removeListener(SliderListener l) {
listeners.remove(l);
}
private void fireOnStartEvent(Event evt, int value) {
SliderEvent e = new SliderEvent(evt, this, value);
for (SliderListener l : listeners) {
l.onStart(e);
}
}
private boolean fireOnSlideEvent(Event evt, int value) {
SliderEvent e = new SliderEvent(evt, this, value);
for (SliderListener l : listeners) {
l.onStart(e);
}
boolean ret = true;
for (SliderListener l : listeners) {
if (!l.onSlide(e)) {
//if any of the listeners returns false, return false,
//but let them all do their thing
ret = false;
}
}
return ret;
}
private void fireOnChangeEvent(Event evt, int value, boolean hasOriginalEvent) {
SliderEvent e = new SliderEvent(evt, this, value, hasOriginalEvent);
for (SliderListener l : listeners) {
l.onChange(e);
}
}
private void fireOnStopEvent(Event evt, int value) {
SliderEvent e = new SliderEvent(evt, this, value);
for (SliderListener l : listeners) {
l.onStop(e);
}
}
private native void setIntOptionJS(String id, String option, int value) /*-{
$wnd.$("#" + id).slider("option", option, value);
}-*/;
private native int getIntOptionJS(String id, String option) /*-{
return $wnd.$("#" + id).slider("option", option);
}-*/;
private native void setBooleanOptionJS(String id, String option, boolean value) /*-{
$wnd.$("#" + id).slider("option", option, value);
}-*/;
private native boolean getBooleanOptionJS(String id, String option) /*-{
return $wnd.$("#" + id).slider("option", option);
}-*/;
private native void setStringOptionJS(String id, String option, String value) /*-{
$wnd.$("#" + id).slider("option", option, value);
}-*/;
private native String getStringOptionJS(String id, String option) /*-{
return $wnd.$("#" + id).slider("option", option);
}-*/;
private native void setValueJS(String id, int value) /*-{
$wnd.$("#" + id).slider("option", "value", value);
}-*/;
private native void createSliderJS(SingleSlider x, String id, JavaScriptObject options) /*-{
options.start = function(event, ui) {
x.#za.co.bsg.ems.client.framework.ui.slider.SingleSlider::fireOnStartEvent(Lcom/google/gwt/user/client/Event;I)(event, ui.value);
};
options.slide = function(event, ui) {
return x.#za.co.bsg.ems.client.framework.ui.slider.SingleSlider::fireOnSlideEvent(Lcom/google/gwt/user/client/Event;I)(event, ui.value);
};
options.change = function(event, ui) {
var has = event.originalEvent ? true : false;
x.#za.co.bsg.ems.client.framework.ui.slider.SingleSlider::fireOnChangeEvent(Lcom/google/gwt/user/client/Event;IZ)(event, ui.value, has);
};
options.stop = function(event, ui) {
x.#za.co.bsg.ems.client.framework.ui.slider.SingleSlider::fireOnStopEvent(Lcom/google/gwt/user/client/Event;I)(event, ui.value);
};
$wnd.$("#" + id).slider(options);
}-*/;
private native void destroySliderJS(SingleSlider x, String id) /*-{
$wnd.$("#" + id).slider("destroy");
}-*/;
}
This slider was then incorporated into the following widget to create a percentage slider
package za.co.bsg.ems.client.framework.ui.slider;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import za.co.bsg.ems.client.event.IntegerValueChangeEvent;
import java.util.HashSet;
import java.util.Set;
public class PercentageSliderWidget extends Composite implements SliderListener, HasValueChangeHandlers<Integer> {
interface PercentageSliderUiBinder extends UiBinder<Widget, PercentageSliderWidget> {}
private static final PercentageSliderUiBinder UI_BINDER = GWT.create(PercentageSliderUiBinder.class);
#UiField
Label headingLabel;
#UiField(provided = true)
SingleSlider singleSlider;
#UiField
IntegerBox percentBox;
#UiField
Label percentageSignLabel;
private Set<ValueChangeHandler<Integer>> valueChangeHandlers;
public PercentageSliderWidget(long taskId, String heading, int existingProgress) {
valueChangeHandlers = new HashSet<>();
JSONObject options = SingleSlider.getOptions(0, 100, existingProgress);
singleSlider = new SingleSlider("singleSlider" + taskId, options);
singleSlider.setTitle(existingProgress + "%");
initWidget(UI_BINDER.createAndBindUi(this));
singleSlider.addListener(this);
percentBox.setValue(existingProgress);
percentBox.addValueChangeHandler(new ValueChangeHandler<Integer>() {
#Override
public void onValueChange(ValueChangeEvent<Integer> event) {
updateValues(event.getValue(), true);
}
});
headingLabel.setText(heading);
percentageSignLabel.setText("%");
}
public void setValue(int value) {
updateValues(value, false);
}
#Override
public HandlerRegistration addValueChangeHandler(final ValueChangeHandler<Integer> handler) {
valueChangeHandlers.add(handler);
return new HandlerRegistration() {
#Override
public void removeHandler() {
valueChangeHandlers.remove(handler);
}
};
}
#Override
public boolean onSlide(SliderEvent e) {
percentBox.setValue(e.getValue());
return true;
}
#Override
public void onChange(SliderEvent e) {
// do nothing
}
#Override
public void onStop(SliderEvent e) {
updateValues(e.getValue(), true);
}
#Override
public void onStart(SliderEvent e) {
// do nothing
}
private void updateValues(int progressValue, boolean fireEvents) {
singleSlider.setTitle(progressValue + "%");
singleSlider.setValue(progressValue);
percentBox.setValue(progressValue);
if (fireEvents) {
for (ValueChangeHandler<Integer> valueChangeHandler : valueChangeHandlers) {
valueChangeHandler.onValueChange(new IntegerValueChangeEvent(progressValue));
}
}
}
}
My starting resource was the following
http://www.zackgrossbart.com/hackito/gwt-slider/
I have two class ,following:
first Class:
package MemoryInterfaces;
import java.util.SortedSet;
import java.util.TreeSet;
/**
*
* #author omid
*/
public class MemoryData<T> {
private Object _datas =null;
public MemoryData() {
}
/**
* #return the _data
*/
public SortedSet<T> getDatas() {
if (this._datas==null) {
this._datas=new TreeSet<T>();
getDataOfDatabase();
}
return (SortedSet<T>)_datas;
}
/**
* #param data the _data to set
*/
public void setDatas(SortedSet<T> datas) {
this._datas=datas;
}
public T getDataOfMemory(int i) {
return (T) getDatas().toArray()[i];
}
public void addItem(T data) {
this.getDatas().add(data);
}
public void remove(T data) {
this.getDatas().remove(data);
}
protected void getDataOfDatabase() {
}
}
second Class:
public class CabinetImpl extends MemoryData<CabinetItem> {
private static SortedSet<CabinetItem> _datas = null;
public CabinetImpl() {
setDatas(_datas);
}
#Override
protected void getDataOfDatabase() {
CabinetDaoImpl _cab = new CabinetDaoImpl();
List<Cabinet> _lst = _cab.getTable();
if (!_lst.isEmpty()) {
for (int i = 0; i < _lst.size(); i++) {
AddToMemory(_lst.get(i).getID(), _lst.get(i).getName(), _lst.get(i).getSub(), _lst.get(i).getDepid());
}
_datas=getDatas();
}
}
private void AddToMemory(int id, String name, int sub, int depid) {
CabinetItem _cab = new CabinetItem(id, name, sub, depid);
addItem(_cab);
}
}
in second Class ,I have a static varible(_datas) be when constructing of cabinetImpl class,_datas transfer to MemoryData but not effect changes in MemoryData on _datas.
Please Help Me!
You are not allowed to override a private variable of a super class in an inherited class. Use the getters and setters of MemoryData
You have two different fields with the same name in different classes.
They are no relationship to each other and different types. If you want to have only one copy I suggest removing one of them.