generics - Java Compile time parameter interdependency -
i'm designing event driven system sends events data queue. listener on queue dispatches messages via javax.enterprise.event, have methods listening these events via observerving qualifiers. system behaves i'd expect i'm missing compile-time type validation. basic code can found below:
public interface ieventsender { void send(final servereventtypeenum servereventtypeenum, final ieventdata eventdata); } public class observingclientclass{ public void eventaprocessservice(@observes @ieventtypequalifier(event_1) servereventmessage servereventmessage) { //handle event, cast data concrete implementation x (x) servereventmessage.geteventdata() } } public enum servereventtypeenum implements ieventtypeenum { event_1, event_2 } public interface ieventdata extends serializable{ } public class x implements ieventdata { } public class y implements ieventdata { }
the servereventtypeenum enum types of events support.
the eventdata generic interface actual eventdata classes implement.
- the servereventmessage wrapper class contains eventdata , other data not relevant problem.
the problem eventtypeenum 1, observering qualifiers expects concrete implementation of type x , eventypeenum 2 expect eventdata of concrete type y. @ moment observing clients cast ieventdata concrete class.
this means when i'm sending eventdata messages queue, have check client code expects receive, risk of classcastexception @ runtime. i'm wondering if there's way, maybe generics, can use define expected implementation type in send method.
i want specify relation between eventtype , concrete implementations in enum itself. have change in 'send' method compiletime type validation? example of want enum:
public enum servereventtypeenum implements ieventtypeenum { event_1(x.class), event_2(y.class); private servereventtypeenum(class concreteimplementationoftheeventdata) { //set fields... } }
technology:
- java se 8
- java ee 7
wiki
Comments
Post a Comment