KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > internal > AccessLevel


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * A class that defines the access levels of the forum.
28  */

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 JavaDoc;
35
36 public final class AccessLevel
37 {
38     public static final long
39         // max: 0x8000000000000000
40
none = 0x0000000000000000L,
41         everything = 0xFFFFFFFFFFFFFFFFL,
42
43         // Forum
44
addUser = 0x0000000000000001L,
45         removeUser = 0x0000000000000002L,
46         changeUser = 0x0000000000000004L,
47         removeGroup = 0x0000000000000008L,
48         changeGroup = 0x0000000000000010L,
49         viewUser = 0x0000000000000020L,
50
51         // Planning
52
addEvent = 0x0000000000000040L,
53         removeEvent = 0x0000000000000080L,
54         setEventData = 0x0000000000000100L,
55         changeEvent = 0x0000000000000200L,
56         addGlobalEvent = 0x0000000000080000L,
57
58         // Area
59
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         // Quotes
72
addQuote = 0x0000000000100000L,
73         removeQuote = 0x0000000000200000L,
74         changeQuote = 0x0000000000400000L,
75
76         // Polls
77
addPoll = 0x0000000000800000L,
78         removePoll = 0x0000000001000000L,
79         changePoll = 0x0000000002000000L,
80         viewPoll = 0x0000000004000000L,
81         addGlobalPoll = 0x0000000008000000L,
82
83         // Comics
84
addComic = 0x0000000010000000L,
85         removeComic = 0x0000000020000000L,
86         changeComic = 0x0000000040000000L,
87
88         // FAQ
89
addFAQTopic = 0x0000000080000000L,
90         removeFAQTopic = 0x0000000100000000L,
91         changeFAQTopic = 0x0000000200000000L,
92         addFAQ = 0x0000000400000000L,
93         removeFAQ = 0x0000000800000000L,
94         changeFAQ = 0x0000001000000000L,
95
96         // misc
97
wiki = 0x0000002000000000L,
98         myWiki = 0x0000004000000000L,
99         archive = 0x0000008000000000L,
100         comics = 0x0000010000000000L,
101         options = 0x0000020000000000L,
102         userDetails = 0x0000040000000000L,
103
104         // skin
105
addGlobalSkin = 0x0000080000000000L,
106         viewSkin = 0x0000100000000000L,
107         addSkin = 0x0000200000000000L,
108         removeSkin = 0x0000400000000000L,
109         changeSkin = 0x0000800000000000L,
110
111         // task
112
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 JavaDoc _s)
125             {
126                 l = _l;
127                 s = _s;
128             }
129
130
131             public long l;
132             public String JavaDoc s;
133         };
134
135
136         M map[] = new M[]
137         {
138             // General
139
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             // Planning
148
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             // Area
156
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             // Quotes
172
new M(addQuote , "add quote" ),
173             new M(removeQuote , "remove quote" ),
174             new M(changeQuote , "change quote" ),
175
176
177             // Polls
178
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             // Comics
186
new M(addComic , "add comic" ),
187             new M(removeComic , "remove comic" ),
188             new M(changeComic , "change comic" ),
189
190
191             // FAQ
192
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             // misc
200
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             // skin
208
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             // task
215
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     /**
241      * Get a descriptive line of the access level specified.
242      */

243     public static String JavaDoc toString(long accessLevel)
244     {
245         switch ((int)accessLevel)
246         {
247         //case (int)none : return "none";
248
case (int)everything : return "everything";
249         default: break;
250         }
251
252
253         StringBuffer JavaDoc r = new StringBuffer JavaDoc();
254
255
256         boolean beginning = true;
257         String JavaDoc 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     /**
279      * Get a list of individual access levels from a long with multiple access levels.
280      */

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     /**
308      * Get a descriptive string from a single access level value.
309      */

310     private static String JavaDoc toStringSingleAccessLevel(long accessLevel)
311     {
312         if (accessLevel == 0L)return null;
313
314         return (String JavaDoc)longToString.get(accessLevel);
315     }
316
317     /**
318      * Parse a string of access levels
319      */

320     public static long parseAccessLevel(String JavaDoc accessLevel)
321     {
322         long r = 0;
323
324         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(accessLevel.toLowerCase(), ",|");
325
326         while (tokenizer.hasMoreTokens())
327         {
328             String JavaDoc token = tokenizer.nextToken().trim();
329             if (!stringToLong.containsKey(token))
330                 throw new IllegalArgumentException JavaDoc(token);
331
332             r |= stringToLong.getLong(token);
333         }
334
335         return r;
336     }
337 }
Popular Tags