I'm trying to mimic the functionality of useful enums, since Java doesn't have them. The idea is to be able to turn a BNCS packet id in to an object and back. Normally, this wouldn't be difficult, but Java doesn't allow you to specify ordinals; it assigns them automatically, incrementally beginning with zero. Therefore, it would be possible to simply specify a constant for each unused packet id, but I don't like that solution too much.
Here's what I've got now; I don't like the fromId() method in its current state because it has to search - this could be replaced with a map of some kind, which could potentially drop the algo from O(n) to O(log(n)), or an array to drop it to O(1), which would be equivalent to mapping each unused constant.
Does anyone have a better solution, or ideas to improve upon what I've got? I may just switch to defining the unused IDs.
FWIW, I'm doing this because it's useful for logging, it simplifies my switch statements, and I like being type safe.
/**
* This file is distributed under the GPL
* $Id: BNCSCommandIDs.java 755 2007-10-15 05:43:55Z scotta $
*/
package net.bnubot.core.bncs;
import net.bnubot.core.EnumIdNotPresentException;
public enum BNCSCommandIDs {
SID_NULL(0x00),
SID_STOPADV(0x02),
SID_CLIENTID(0x05),
SID_STARTVERSIONING(0x06),
SID_REPORTVERSION(0x07),
SID_STARTADVEX(0x08),
SID_GETADVLISTEX(0x09),
SID_ENTERCHAT(0x0A),
SID_GETCHANNELLIST(0x0B),
SID_JOINCHANNEL(0x0C),
SID_CHATCOMMAND(0x0E),
SID_CHATEVENT(0x0F),
SID_LEAVECHAT(0x10),
SID_LOCALEINFO(0x12),
SID_FLOODDETECTED(0x13),
SID_UDPPINGRESPONSE(0x14),
SID_CHECKAD(0x15),
SID_CLICKAD(0x16),
SID_MESSAGEBOX(0x19),
SID_STARTADVEX3(0x1C),
SID_LOGONCHALLENGEEX(0x1D),
SID_CLIENTID2(0x1E),
SID_LEAVEGAME(0x1F),
SID_DISPLAYAD(0x21),
SID_NOTIFYJOIN(0x22),
SID_PING(0x25),
SID_READUSERDATA(0x26),
SID_WRITEUSERDATA(0x27),
SID_LOGONCHALLENGE(0x28),
SID_LOGONRESPONSE(0x29),
SID_CREATEACCOUNT(0x2A),
SID_GAMERESULT(0x2C),
SID_GETICONDATA(0x2D),
SID_GETLADDERDATA(0x2E),
SID_FINDLADDERUSER(0x2F),
SID_CDKEY(0x30),
SID_CHANGEPASSWORD(0x31),
SID_CHECKDATAFILE(0x32),
SID_GETFILETIME(0x33),
SID_QUERYREALMS(0x34),
SID_PROFILE(0x35),
SID_CDKEY2(0x36),
SID_LOGONRESPONSE2(0x3A),
SID_CHECKDATAFILE2(0x3C),
SID_CREATEACCOUNT2(0x3D),
SID_LOGONREALMEX(0x3E),
SID_STARTVERSIONING2(0x3F),
SID_QUERYREALMS2(0x40),
SID_QUERYADURL(0x41),
SID_WARCRAFTGENERAL(0x44),
SID_NETGAMEPORT(0x45),
SID_NEWS_INFO(0x46),
SID_OPTIONALWORK(0x4A),
SID_EXTRAWORK(0x4B),
SID_REQUIREDWORK(0x4C),
SID_AUTH_INFO(0x50),
SID_AUTH_CHECK(0x51),
SID_AUTH_ACCOUNTCREATE(0x52),
SID_AUTH_ACCOUNTLOGON(0x53),
SID_AUTH_ACCOUNTLOGONPROOF(0x54),
SID_AUTH_ACCOUNTCHANGE(0x55),
SID_AUTH_ACCOUNTCHANGEPROOF(0x56),
SID_AUTH_ACCOUNTUPGRADE(0x57),
SID_AUTH_ACCOUNTUPGRADEPROOF(0x58),
SID_SETEMAIL(0x59),
SID_RESETPASSWORD(0x5A),
SID_CHANGEEMAIL(0x5B),
SID_SWITCHPRODUCT(0x5C),
SID_GAMEPLAYERSEARCH(0x60),
SID_FRIENDSLIST(0x65),
SID_FRIENDSUPDATE(0x66),
SID_FRIENDSADD(0x67),
SID_FRIENDSREMOVE(0x68),
SID_FRIENDSPOSITION(0x69),
SID_CLANFINDCANDIDATES(0x70),
SID_CLANINVITEMULTIPLE(0x71),
SID_CLANCREATIONINVITATION(0x72),
SID_CLANDISBAND(0x73),
SID_CLANMAKECHIEFTAIN(0x74),
SID_CLANINFO(0x75),
SID_CLANQUITNOTIFY(0x76),
SID_CLANINVITATION(0x77),
SID_CLANREMOVEMEMBER(0x78),
SID_CLANINVITATIONRESPONSE(0x79),
SID_CLANRANKCHANGE(0x7A),
SID_CLANSETMOTD(0x7B),
SID_CLANMOTD(0x7C),
SID_CLANMEMBERLIST(0x7D),
SID_CLANMEMBERREMOVED(0x7E),
SID_CLANMEMBERSTATUSCHANGE(0x7F),
SID_CLANMEMBERRANKCHANGE(0x81),
SID_CLANMEMBERINFORMATION(0x82);
private final int id;
private BNCSCommandIDs(int id) {
this.id = id;
}
public int getId() {
return id;
}
public static BNCSCommandIDs fromId(int id) throws EnumIdNotPresentException {
// TODO: replace this with a map
for(BNCSCommandIDs command : values())
if(command.getId() == id)
return command;
throw new EnumIdNotPresentException(BNCSCommandIDs.class, id);
}
}