1 25 26 29 package net.killingar.forum.internal; 30 31 import it.unimi.dsi.fastutil.longs.Long2ObjectAVLTreeMap; 32 import it.unimi.dsi.fastutil.objects.Object2LongAVLTreeMap; 33 34 import java.util.StringTokenizer ; 35 36 public final class AccessLevel 37 { 38 public static final long 39 none = 0x0000000000000000L, 41 everything = 0xFFFFFFFFFFFFFFFFL, 42 43 addUser = 0x0000000000000001L, 45 removeUser = 0x0000000000000002L, 46 changeUser = 0x0000000000000004L, 47 removeGroup = 0x0000000000000008L, 48 changeGroup = 0x0000000000000010L, 49 viewUser = 0x0000000000000020L, 50 51 addEvent = 0x0000000000000040L, 53 removeEvent = 0x0000000000000080L, 54 setEventData = 0x0000000000000100L, 55 changeEvent = 0x0000000000000200L, 56 addGlobalEvent = 0x0000000000080000L, 57 58 addAreaGroup = 0x0000000000000400L, 60 removeAreaGroup = 0x0000000000000800L, 61 changeAreaGroup = 0x0000000000001000L, 62 63 readArea = 0x0000000000002000L, 64 addArea = 0x0000000000004000L, 65 removeArea = 0x0000000000008000L, 66 changeArea = 0x0000000000010000L, 67 68 addMessage = 0x0000000000020000L, 69 removeMessage = 0x0000000000040000L, 70 71 addQuote = 0x0000000000100000L, 73 removeQuote = 0x0000000000200000L, 74 changeQuote = 0x0000000000400000L, 75 76 addPoll = 0x0000000000800000L, 78 removePoll = 0x0000000001000000L, 79 changePoll = 0x0000000002000000L, 80 viewPoll = 0x0000000004000000L, 81 addGlobalPoll = 0x0000000008000000L, 82 83 addComic = 0x0000000010000000L, 85 removeComic = 0x0000000020000000L, 86 changeComic = 0x0000000040000000L, 87 88 addFAQTopic = 0x0000000080000000L, 90 removeFAQTopic = 0x0000000100000000L, 91 changeFAQTopic = 0x0000000200000000L, 92 addFAQ = 0x0000000400000000L, 93 removeFAQ = 0x0000000800000000L, 94 changeFAQ = 0x0000001000000000L, 95 96 wiki = 0x0000002000000000L, 98 myWiki = 0x0000004000000000L, 99 archive = 0x0000008000000000L, 100 comics = 0x0000010000000000L, 101 options = 0x0000020000000000L, 102 userDetails = 0x0000040000000000L, 103 104 addGlobalSkin = 0x0000080000000000L, 106 viewSkin = 0x0000100000000000L, 107 addSkin = 0x0000200000000000L, 108 removeSkin = 0x0000400000000000L, 109 changeSkin = 0x0000800000000000L, 110 111 addTask = 0x0001000000000000L, 113 removeTask = 0x0002000000000000L, 114 setTaskData = 0x0004000000000000L, 115 changeTask = 0x0008000000000000L; 116 117 public static Object2LongAVLTreeMap stringToLong; 118 public static Long2ObjectAVLTreeMap longToString; 119 120 static 121 { 122 class M 123 { 124 public M(long _l, String _s) 125 { 126 l = _l; 127 s = _s; 128 } 129 130 131 public long l; 132 public String s; 133 }; 134 135 136 M map[] = new M[] 137 { 138 new M(addUser , "add user" ), 140 new M(removeUser , "remove user" ), 141 new M(changeUser , "change user" ), 142 new M(removeGroup , "remove group" ), 143 new M(changeGroup , "change group" ), 144 new M(viewUser , "view user" ), 145 146 147 new M(addEvent , "add event" ), 149 new M(removeEvent , "remove event" ), 150 new M(setEventData , "set eventdata" ), 151 new M(changeEvent , "change event" ), 152 new M(addGlobalEvent , "add global event" ), 153 154 155 new M(addAreaGroup , "add area group" ), 157 new M(removeAreaGroup , "remove area group"), 158 new M(changeAreaGroup , "change area group"), 159 160 161 new M(readArea , "read area" ), 162 new M(addArea , "add area" ), 163 new M(removeArea , "remove area" ), 164 new M(changeArea , "change area" ), 165 166 167 new M(addMessage , "add message" ), 168 new M(removeMessage , "remove message" ), 169 170 171 new M(addQuote , "add quote" ), 173 new M(removeQuote , "remove quote" ), 174 new M(changeQuote , "change quote" ), 175 176 177 new M(addPoll , "add poll" ), 179 new M(removePoll , "remove poll" ), 180 new M(changePoll , "change poll" ), 181 new M(viewPoll , "view poll" ), 182 new M(addGlobalPoll , "add global poll" ), 183 184 185 new M(addComic , "add comic" ), 187 new M(removeComic , "remove comic" ), 188 new M(changeComic , "change comic" ), 189 190 191 new M(addFAQTopic , "add faq topic" ), 193 new M(removeFAQTopic , "remove faq topic" ), 194 new M(changeFAQTopic , "change faq topic" ), 195 new M(addFAQ , "add faq" ), 196 new M(removeFAQ , "remove faq" ), 197 new M(changeFAQ , "change faq" ), 198 199 new M(wiki , "wiki" ), 201 new M(myWiki , "my wiki" ), 202 new M(archive , "archive" ), 203 new M(comics , "comics" ), 204 new M(options , "change options" ), 205 new M(userDetails , "change user details"), 206 207 new M(addGlobalSkin , "add global skin" ), 209 new M(viewSkin , "view skin" ), 210 new M(addSkin , "add skin" ), 211 new M(removeSkin , "remove skin" ), 212 new M(changeSkin , "change skin" ), 213 214 new M(addTask , "add task" ), 216 new M(removeTask , "remove task" ), 217 new M(setTaskData , "set taskdata" ), 218 new M(changeTask , "change task" ), 219 }; 220 221 222 stringToLong = new Object2LongAVLTreeMap(); 223 for (int i = 0; i < map.length; i++) 224 { 225 stringToLong.put(map[i].s, map[i].l); 226 } 227 228 229 longToString = new Long2ObjectAVLTreeMap(); 230 for (int i = 0; i < map.length; i++) 231 { 232 longToString.put(map[i].l, map[i].s); 233 } 234 } 235 236 237 private AccessLevel(){} 238 239 240 243 public static String toString(long accessLevel) 244 { 245 switch ((int)accessLevel) 246 { 247 case (int)everything : return "everything"; 249 default: break; 250 } 251 252 253 StringBuffer r = new StringBuffer (); 254 255 256 boolean beginning = true; 257 String tmp; 258 for (int i = 0; i < 63; i++) 259 { 260 if ((accessLevel & 1L<<i) != 0) 261 { 262 tmp = toStringSingleAccessLevel(accessLevel & 1L<<i); 263 if (tmp != null) 264 { 265 if (!beginning) 266 r.append(", "); 267 r.append(tmp); 268 beginning = false; 269 } 270 } 271 } 272 273 274 return r.toString(); 275 } 276 277 278 281 public static long[] list(long accessLevel) 282 { 283 int len = 0; 284 for (int i = 0; i < 63; i++) 285 { 286 if ((accessLevel & 1L<<i) != 0L) 287 len++; 288 } 289 long r[] = new long[len]; 290 int j = 0; 291 292 293 for (int i = 0; i < 63; i++) 294 { 295 if ((accessLevel & 1L<<i) != 0L) 296 { 297 r[j] = accessLevel & (1L<<i); 298 j++; 299 } 300 } 301 302 303 return r; 304 } 305 306 307 310 private static String toStringSingleAccessLevel(long accessLevel) 311 { 312 if (accessLevel == 0L)return null; 313 314 return (String )longToString.get(accessLevel); 315 } 316 317 320 public static long parseAccessLevel(String accessLevel) 321 { 322 long r = 0; 323 324 StringTokenizer tokenizer = new StringTokenizer (accessLevel.toLowerCase(), ",|"); 325 326 while (tokenizer.hasMoreTokens()) 327 { 328 String token = tokenizer.nextToken().trim(); 329 if (!stringToLong.containsKey(token)) 330 throw new IllegalArgumentException (token); 331 332 r |= stringToLong.getLong(token); 333 } 334 335 return r; 336 } 337 } | Popular Tags |