1
2
3
4
5
6
7
8
9
10 package ch.panter.li.bi.util;
11
12
13
14 /** Simplifies bit operations and makes them more readable.
15 * TODO: rewrite to own class
16 @author Leon Poyyayil
17 @version 0.5.250
18 @since 0.0.112
19 */
20 public final class FlagTool {
21
22
23
24
25 /** Sets some specified bits in a flag list to '1'.
26 @since 0.0.112
27 @param pnFlagList the list of flags to 'modify'
28 @param pnFlagToSet the flags to set to '1'
29 @return the 'modified' flag list with the desired bits set to '1'
30 */
31 public static byte set(
32 byte pnFlagList,
33 byte pnFlagToSet
34 )
35 {
36 byte lnFlags = pnFlagList;
37 lnFlags |= pnFlagToSet;
38 return lnFlags;
39 }
40
41
42
43 /** Sets some specified bits in a flag list to '1' or '0' depending on the given
44 boolean parameter.
45 @since 0.0.112
46 @param pnFlagList the list of flags to 'modify'
47 @param pnFlagToSet the flags to set to the desired state
48 @param pbSet the state to set the flags to: true means '1', false means '0'
49 @return the 'modified' flag list with the desired bits set according to pbSet
50 */
51 public static byte set(
52 byte pnFlagList,
53 byte pnFlagToSet,
54 boolean pbSet
55 )
56 {
57 return pbSet ? set( pnFlagList, pnFlagToSet ) : clear( pnFlagList, pnFlagToSet );
58 }
59
60
61
62 /** Sets some specified bits in a flag list to '0'.
63 @since 0.0.112
64 @param pnFlagList the list of flags to 'modify'
65 @param pnFlagToClear the flags to set to '0'
66 @return the 'modified' flag list with the desired bits set to '0'
67 */
68 public static byte clear(
69 byte pnFlagList,
70 byte pnFlagToClear
71 )
72 {
73 byte lnFlags = pnFlagList;
74 lnFlags &= ~pnFlagToClear;
75 return lnFlags;
76 }
77
78
79
80 /** Tests whether some bits in a flag list are set to '1'.
81 @since 0.0.112
82 @param pnFlagList the list of flags to test
83 @param pnFlagToTest the flags to check for state '1'
84 @return true if <b>all</b> non-zero bits of pnFlagToTest are set to '1' in
85 pnFlagList, false otherwise
86 */
87 public static boolean isSet(
88 byte pnFlagList,
89 byte pnFlagToTest
90 )
91 {
92 return ( pnFlagList & pnFlagToTest ) == pnFlagToTest;
93 }
94
95
96
97 /** Sets some specified bits in a flag list to '1'.
98 @since 0.0.112
99 @param pnFlagList the list of flags to 'modify'
100 @param pnFlagToSet the flags to set to '1'
101 @return the 'modified' flag list with the desired bits set to '1'
102 */
103 public static short set(
104 short pnFlagList,
105 short pnFlagToSet
106 )
107 {
108 short lnFlags = pnFlagList;
109 lnFlags |= pnFlagToSet;
110 return lnFlags;
111 }
112
113
114
115 /** Sets some specified bits in a flag list to '1' or '0' depending on the given
116 boolean parameter.
117 @since 0.0.112
118 @param pnFlagList the list of flags to 'modify'
119 @param pnFlagToSet the flags to set to the desired state
120 @param pbSet the state to set the flags to: true means '1', false means '0'
121 @return the 'modified' flag list with the desired bits set according to pbSet
122 */
123 public static short set(
124 short pnFlagList,
125 short pnFlagToSet,
126 boolean pbSet
127 )
128 {
129 return pbSet ? set( pnFlagList, pnFlagToSet ) : clear( pnFlagList, pnFlagToSet );
130 }
131
132
133
134 /** Sets some specified bits in a flag list to '0'.
135 @since 0.0.112
136 @param pnFlagList the list of flags to 'modify'
137 @param pnFlagToClear the flags to set to '0'
138 @return the 'modified' flag list with the desired bits set to '0'
139 */
140 public static short clear(
141 short pnFlagList,
142 short pnFlagToClear
143 )
144 {
145 short lnFlags = pnFlagList;
146 lnFlags &= ~pnFlagToClear;
147 return lnFlags;
148 }
149
150
151
152 /** Tests whether some bits in a flag list are set to '1'.
153 @since 0.0.112
154 @param pnFlagList the list of flags to test
155 @param pnFlagToTest the flags to check for state '1'
156 @return true if <b>all</b> non-zero bits of pnFlagToTest are set to '1' in
157 pnFlagList, false otherwise
158 */
159 public static boolean isSet(
160 short pnFlagList,
161 short pnFlagToTest
162 )
163 {
164 return ( pnFlagList & pnFlagToTest ) == pnFlagToTest;
165 }
166
167
168
169 /** Sets some specified bits in a flag list to '1'.
170 @since 0.0.112
171 @param pnFlagList the list of flags to 'modify'
172 @param pnFlagToSet the flags to set to '1'
173 @return the 'modified' flag list with the desired bits set to '1'
174 */
175 public static char set(
176 char pnFlagList,
177 char pnFlagToSet
178 )
179 {
180 char lnFlags = pnFlagList;
181 lnFlags |= pnFlagToSet;
182 return lnFlags;
183 }
184
185
186
187 /** Sets some specified bits in a flag list to '1' or '0' depending on the given
188 boolean parameter.
189 @since 0.0.112
190 @param pnFlagList the list of flags to 'modify'
191 @param pnFlagToSet the flags to set to the desired state
192 @param pbSet the state to set the flags to: true means '1', false means '0'
193 @return the 'modified' flag list with the desired bits set according to pbSet
194 */
195 public static char set(
196 char pnFlagList,
197 char pnFlagToSet,
198 boolean pbSet
199 )
200 {
201 return pbSet ? set( pnFlagList, pnFlagToSet ) : clear( pnFlagList, pnFlagToSet );
202 }
203
204
205
206 /** Sets some specified bits in a flag list to '0'.
207 @since 0.0.112
208 @param pnFlagList the list of flags to 'modify'
209 @param pnFlagToClear the flags to set to '0'
210 @return the 'modified' flag list with the desired bits set to '0'
211 */
212 public static char clear(
213 char pnFlagList,
214 char pnFlagToClear
215 )
216 {
217 char lnFlags = pnFlagList;
218 lnFlags &= ~pnFlagToClear;
219 return lnFlags;
220 }
221
222
223
224 /** Tests whether some bits in a flag list are set to '1'.
225 @since 0.0.112
226 @param pnFlagList the list of flags to test
227 @param pnFlagToTest the flags to check for state '1'
228 @return true if <b>all</b> non-zero bits of pnFlagToTest are set to '1' in
229 pnFlagList, false otherwise
230 */
231 public static boolean isSet(
232 char pnFlagList,
233 char pnFlagToTest
234 )
235 {
236 return ( pnFlagList & pnFlagToTest ) == pnFlagToTest;
237 }
238
239
240
241 /** Sets some specified bits in a flag list to '1'.
242 @since 0.0.112
243 @param pnFlagList the list of flags to 'modify'
244 @param pnFlagToSet the flags to set to '1'
245 @return the 'modified' flag list with the desired bits set to '1'
246 */
247 public static int set(
248 int pnFlagList,
249 int pnFlagToSet
250 )
251 {
252 int lnFlags = pnFlagList;
253 lnFlags |= pnFlagToSet;
254 return lnFlags;
255 }
256
257
258
259 /** Sets some specified bits in a flag list to '1' or '0' depending on the given
260 boolean parameter.
261 @since 0.0.112
262 @param pnFlagList the list of flags to 'modify'
263 @param pnFlagToSet the flags to set to the desired state
264 @param pbSet the state to set the flags to: true means '1', false means '0'
265 @return the 'modified' flag list with the desired bits set according to pbSet
266 */
267 public static int set(
268 int pnFlagList,
269 int pnFlagToSet,
270 boolean pbSet
271 )
272 {
273 return pbSet ? set( pnFlagList, pnFlagToSet ) : clear( pnFlagList, pnFlagToSet );
274 }
275
276
277
278 /** Sets some specified bits in a flag list to '0'.
279 @since 0.0.112
280 @param pnFlagList the list of flags to 'modify'
281 @param pnFlagToClear the flags to set to '0'
282 @return the 'modified' flag list with the desired bits set to '0'
283 */
284 public static int clear(
285 int pnFlagList,
286 int pnFlagToClear
287 )
288 {
289 int lnFlags = pnFlagList;
290 lnFlags &= ~pnFlagToClear;
291 return lnFlags;
292 }
293
294
295
296 /** Tests whether some bits in a flag list are set to '1'.
297 @since 0.0.112
298 @param pnFlagList the list of flags to test
299 @param pnFlagToTest the flags to check for state '1'
300 @return true if <b>all</b> non-zero bits of pnFlagToTest are set to '1' in
301 pnFlagList, false otherwise
302 */
303 public static boolean isSet(
304 int pnFlagList,
305 int pnFlagToTest
306 )
307 {
308 return ( pnFlagList & pnFlagToTest ) == pnFlagToTest;
309 }
310
311
312
313 /** Sets some specified bits in a flag list to '1'.
314 @since 0.0.112
315 @param pnFlagList the list of flags to 'modify'
316 @param pnFlagToSet the flags to set to '1'
317 @return the 'modified' flag list with the desired bits set to '1'
318 */
319 public static long set(
320 long pnFlagList,
321 long pnFlagToSet
322 )
323 {
324 long lnFlags = pnFlagList;
325 lnFlags |= pnFlagToSet;
326 return lnFlags;
327 }
328
329
330
331 /** Sets some specified bits in a flag list to '1' or '0' depending on the given
332 boolean parameter.
333 @since 0.0.112
334 @param pnFlagList the list of flags to 'modify'
335 @param pnFlagToSet the flags to set to the desired state
336 @param pbSet the state to set the flags to: true means '1', false means '0'
337 @return the 'modified' flag list with the desired bits set according to pbSet
338 */
339 public static long set(
340 long pnFlagList,
341 long pnFlagToSet,
342 boolean pbSet
343 )
344 {
345 return pbSet ? set( pnFlagList, pnFlagToSet ) : clear( pnFlagList, pnFlagToSet );
346 }
347
348
349
350 /** Sets some specified bits in a flag list to '0'.
351 @since 0.0.112
352 @param pnFlagList the list of flags to 'modify'
353 @param pnFlagToClear the flags to set to '0'
354 @return the 'modified' flag list with the desired bits set to '0'
355 */
356 public static long clear(
357 long pnFlagList,
358 long pnFlagToClear
359 )
360 {
361 long lnFlags = pnFlagList;
362 lnFlags &= ~pnFlagToClear;
363 return lnFlags;
364 }
365
366
367
368 /** Tests whether some bits in a flag list are set to '1'.
369 @since 0.0.112
370 @param pnFlagList the list of flags to test
371 @param pnFlagToTest the flags to check for state '1'
372 @return true if <b>all</b> non-zero bits of pnFlagToTest are set to '1' in
373 pnFlagList, false otherwise
374 */
375 public static boolean isSet(
376 long pnFlagList,
377 long pnFlagToTest
378 )
379 {
380 return ( pnFlagList & pnFlagToTest ) == pnFlagToTest;
381 }
382
383
384 }
385
386
387
388