Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Last active August 29, 2015 14:02
Show Gist options
  • Save DarkSeraphim/88e427d50e8671de5104 to your computer and use it in GitHub Desktop.
Save DarkSeraphim/88e427d50e8671de5104 to your computer and use it in GitHub Desktop.
Simple small permission system
public class Permissions
{
public enum Permission
{
CREATE,
DERP
;
private final long val;
private Permission()
{
this.val = 2power(this.ordinal());
}
long getValue()
{
return this.val;
}
private long 2power(int i)
{
long ret = 1;
for(int j = 0; j < i; j++)
ret*=2;
return ret;
}
}
// To save and load
private long perm;
public Permissions(long theirPermissionValue)
{
this.perm = theirPermissionValue;
}
public boolean hasPermission(Permission perm)
{
long i = perm.getValue();
return (this.perm & i) == i;
}
public void givePermission(Permission perm)
{
long i = perm.getValue();
this.perm |= i;
}
public void revokePermission(Permission perm)
{
long i = perm.getValue();
this.perm &= ~i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment