user1409534
user1409534

Reputation: 2304

How to handle event based on their type

Say I have and event class that contains data and the type of the event (from remote service)

public class Event{
  String StringData;
  Integer IntData;
  EvenType eventType;
}

where EventType is enum :

public enum EventType{
 NEW,UPDATE,DELETE
}

Say I have a pipeline that recessives stream of event and need to process the data inside the event according to its type. For example if it is new event need to save to db and log if it is delete event save it to file , so each event type has its own behavior need to implement.

for example:

List<Event> events = ...
events.forEach(e->??? ); //how to process the event based on its type

Upvotes: 0

Views: 119

Answers (1)

PaulProgrammer
PaulProgrammer

Reputation: 17630

Generally speaking, this would be handled by a switch statement.

List<Event> events = ...
events.forEach(e->{
    switch (e.eventType) {
        case NEW:
          // do new thing
          break;
        case UPDATE:
          // do update thing
          break;
        case DELETE:
          // do delete thing
          break;
        default:
          throw UnexpectedEventType(); // just in case
    }
});

Upvotes: 2

Related Questions