Page 2 of 2

Posted: 21 Dec 2010, 17:36
by dragagon
I could see you do something slightly different that makes the code a little cleaner, but no less lengthy:

Code: Select all

IPermissionProfile profile = null;

switch(Short.parseShort((userData.getUtfString("PRIVILEGE")).trim()))
{
   case 1:
      profile = DefaultPermissionProfile.STANDARD;
      break;
   case 2:
      profile = DefaultPermissionProfile.MODERATOR;
      break;
   case 3:
      profile = DefaultPermissionProfile.ADMINISTRATOR;
      break;
   case 0:
   default:
      profile = DefaultPermissionProfile.GUEST;
}

session.setProperty($permission, profile);
It makes it a little easier to add new profile types in the future. I would also recommend that this be made its own function and placed in another class so that you can call it, pass it an int and have it return an IPermissionProfile so that you can call it from places and never repeat the code, but otherwise I don't see how you could optimize it further.

Posted: 21 Dec 2010, 17:49
by Lapo
Actually there is a trick, but it's not implemented in this particular class.
A static method on the Enum itself which returns the proper IPermissionProfile instance from the numeric id.

It can be useful especially with Enums with lots of values. With this one it's probably not the case.

To exemplify:

Code: Select all

IPermissionProfile prof = DefaultPermissionProfile.fromId(theValue);

Posted: 22 Dec 2010, 14:25
by rjgtav
When will it be implemented then?

Posted: 22 Dec 2010, 14:40
by Lapo
As I explained we don't implement it for small enums like that. A normal if/then or switch can be used.
It was a suggestion for a custom implementation

Posted: 22 Dec 2010, 14:41
by rjgtav
Oh ok sorry

Posted: 09 May 2011, 06:58
by Lapo
We'll add a static fromId() method so that you can do this:

DefaultPermissionProfile profile = DefaultPermissionProfile profile =

Code: Select all

DefaultPermissionProfile.fromId(profileNmericValue);
switch(profile)
{
	case GUEST:
		// action for GUEST level user
	break;
	
	case STANDARD:
		// action for STANDARD level user
	break;
	
	case MODERATOR:
		// action for MODERATOR level user
	break;
}

Posted: 09 May 2011, 12:26
by rjgtav
Thanks Lapo. Just what i needed :-)