Last active
August 29, 2015 14:02
-
-
Save DarkSeraphim/88e427d50e8671de5104 to your computer and use it in GitHub Desktop.
Simple small permission system
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 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