Created
February 6, 2015 12:14
-
-
Save rogermb/6537cbf4ceafa8362e6b to your computer and use it in GitHub Desktop.
Event creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EventManager { | |
private interface EventFactory { | |
TS3Event createEvent(HashMap<String, String> eventData); | |
} | |
/* | |
* Single-line EventFactory | |
*/ | |
private static final HashMap<String, EventFactory> EVENT_FACTORIES_1 = new HashMap<String, EventFactory>() {{ | |
put("notifytextmessage", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new TextMessageEvent(eventData); }}); | |
put("notifycliententerview", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ClientJoinEvent(eventData); }}); | |
put("notifyclientleftview", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ClientLeaveEvent(eventData); }}); | |
put("notifyserveredited", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ServerEditedEvent(eventData); }}); | |
put("notifychanneledited", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ChannelEditedEvent(eventData); }}); | |
put("notifychanneldescriptionchanged", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ChannelDescriptionEditedEvent(eventData); }}); | |
put("notifyclientmoved", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ClientMovedEvent(eventData); }}); | |
put("notifychannelcreated", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ChannelCreateEvent(eventData); }}); | |
put("notifychanneldeleted", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ChannelDeletedEvent(eventData); }}); | |
put("notifychannelmoved", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ChannelMovedEvent(eventData); }}); | |
put("notifychannelpasswordchanged", new EventFactory() { public TS3Event createEvent(HashMap<String, String> eventData) { return new ChannelPasswordChangedEvent(eventData); }}); | |
}}; | |
private static TS3Event createEvent_1(String notifyName, String notifyBody) { | |
final HashMap<String, String> eventData = new DefaultArrayResponse(notifyBody).getArray().get(0); | |
final EventFactory factory = EVENT_FACTORIES_1.get(notifyName); | |
if (factory == null) { | |
throw new TS3UnknownEventException(notifyName + " " + notifyBody); | |
} | |
return factory.createEvent(eventData); | |
} | |
/* | |
* Explicit EventFactory | |
*/ | |
private static final HashMap<String, EventFactory> EVENT_FACTORIES_2 = new HashMap<>(); | |
static { | |
EVENT_FACTORIES_2.put("notifytextmessage", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new TextMessageEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifycliententerview", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ClientJoinEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifyclientleftview", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ClientLeaveEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifyserveredited", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ServerEditedEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifychanneledited", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ChannelEditedEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifychanneldescriptionchanged", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ChannelDescriptionEditedEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifyclientmoved", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ClientMovedEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifychannelcreated", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ChannelCreateEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifychanneldeleted", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ChannelDeletedEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifychannelmoved", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ChannelMovedEvent(eventData); | |
} | |
}); | |
EVENT_FACTORIES_2.put("notifychannelpasswordchanged", new EventFactory() { | |
@Override | |
public TS3Event createEvent(HashMap<String, String> eventData) { | |
return new ChannelPasswordChangedEvent(eventData); | |
} | |
}); | |
} | |
// Same as createEvent_1 | |
private static TS3Event createEvent_2(String notifyName, String notifyBody) { | |
final HashMap<String, String> eventData = new DefaultArrayResponse(notifyBody).getArray().get(0); | |
final EventFactory factory = EVENT_FACTORIES_2.get(notifyName); | |
if (factory == null) { | |
throw new TS3UnknownEventException(notifyName + " " + notifyBody); | |
} | |
return factory.createEvent(eventData); | |
} | |
/* | |
* Switch statement | |
*/ | |
private static TS3Event createEvent(String notifyName, String notifyBody) { | |
final HashMap<String, String> eventData = new DefaultArrayResponse(notifyBody).getArray().get(0); | |
switch (notifyName) { | |
case "notifytextmessage": | |
return new TextMessageEvent(eventData); | |
case "notifycliententerview": | |
return new ClientJoinEvent(eventData); | |
case "notifyclientleftview": | |
return new ClientLeaveEvent(eventData); | |
case "notifyserveredited": | |
return new ServerEditedEvent(eventData); | |
case "notifychanneledited": | |
return new ChannelEditedEvent(eventData); | |
case "notifychanneldescriptionchanged": | |
return new ChannelDescriptionEditedEvent(eventData); | |
case "notifyclientmoved": | |
return new ClientMovedEvent(eventData); | |
case "notifychannelcreated": | |
return new ChannelCreateEvent(eventData); | |
case "notifychanneldeleted": | |
return new ChannelDeletedEvent(eventData); | |
case "notifychannelmoved": | |
return new ChannelMovedEvent(eventData); | |
case "notifychannelpasswordchanged": | |
return new ChannelPasswordChangedEvent(eventData); | |
default: | |
throw new TS3UnknownEventException(notifyName + " " + notifyBody); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment