Skip to content

Package: Rights

Rights

nameinstructionbranchcomplexitylinemethod
Rights()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
Rights(Rights)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
Rights(Rights.Right)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
Rights(String)
M: 21 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
add(Rights)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
add(Rights.Right)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
clone()
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
contains(Rights)
M: 23 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
contains(Rights.Right)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
equals(Object)
M: 30 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getRights()
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
hashCode()
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
remove(Rights)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
remove(Rights.Right)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toString()
M: 26 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.imap;
18:
19: import java.util.ArrayList;
20: import java.util.List;
21:
22: /**
23: * The Rights class represents the set of rights for an authentication
24: * identifier (for instance, a user or a group). <p>
25: *
26: * A right is represented by the <code>Rights.Right</code>
27: * inner class. <p>
28: *
29: * A set of standard rights are predefined (see RFC 2086). Most folder
30: * implementations are expected to support these rights. Some
31: * implementations may also support site-defined rights. <p>
32: *
33: * The following code sample illustrates how to examine your
34: * rights for a folder.
35: * <pre>
36: *
37: * Rights rights = folder.myRights();
38: *
39: * // Check if I can write this folder
40: * if (rights.contains(Rights.Right.WRITE))
41: *         System.out.println("Can write folder");
42: *
43: * // Now give Joe all my rights, except the ability to write the folder
44: * rights.remove(Rights.Right.WRITE);
45: * ACL acl = new ACL("joe", rights);
46: * folder.setACL(acl);
47: * </pre>
48: * <p>
49: *
50: * @author Bill Shannon
51: */
52:
53: public class Rights implements Cloneable {
54:
55: private boolean[] rights = new boolean[128]; // XXX
56:
57: /**
58: * This inner class represents an individual right. A set
59: * of standard rights objects are predefined here.
60: */
61: public static final class Right {
62: private static Right[] cache = new Right[128];
63:
64: // XXX - initialization order?
65: /**
66: * Lookup - mailbox is visible to LIST/LSUB commands.
67: */
68: public static final Right LOOKUP = getInstance('l');
69:
70: /**
71: * Read - SELECT the mailbox, perform CHECK, FETCH, PARTIAL,
72: * SEARCH, COPY from mailbox
73: */
74: public static final Right READ = getInstance('r');
75:
76: /**
77: * Keep seen/unseen information across sessions - STORE \SEEN flag.
78: */
79: public static final Right KEEP_SEEN = getInstance('s');
80:
81: /**
82: * Write - STORE flags other than \SEEN and \DELETED.
83: */
84: public static final Right WRITE = getInstance('w');
85:
86: /**
87: * Insert - perform APPEND, COPY into mailbox.
88: */
89: public static final Right INSERT = getInstance('i');
90:
91: /**
92: * Post - send mail to submission address for mailbox,
93: * not enforced by IMAP4 itself.
94: */
95: public static final Right POST = getInstance('p');
96:
97: /**
98: * Create - CREATE new sub-mailboxes in any implementation-defined
99: * hierarchy, RENAME or DELETE mailbox.
100: */
101: public static final Right CREATE = getInstance('c');
102:
103: /**
104: * Delete - STORE \DELETED flag, perform EXPUNGE.
105: */
106: public static final Right DELETE = getInstance('d');
107:
108: /**
109: * Administer - perform SETACL.
110: */
111: public static final Right ADMINISTER = getInstance('a');
112:
113: char right; // the right represented by this Right object
114:
115: /**
116: * Private constructor used only by getInstance.
117: */
118: private Right(char right) {
119: if ((int) right >= 128)
120: throw new IllegalArgumentException("Right must be ASCII");
121: this.right = right;
122: }
123:
124: /**
125: * Get a Right object representing the specified character.
126: * Characters are assigned per RFC 2086.
127: *
128: * @param right the character representing the right
129: * @return the Right object
130: */
131: public static synchronized Right getInstance(char right) {
132: if ((int) right >= 128)
133: throw new IllegalArgumentException("Right must be ASCII");
134: if (cache[(int) right] == null)
135: cache[(int) right] = new Right(right);
136: return cache[(int) right];
137: }
138:
139: @Override
140: public String toString() {
141: return String.valueOf(right);
142: }
143: }
144:
145:
146: /**
147: * Construct an empty Rights object.
148: */
149: public Rights() {
150: }
151:
152: /**
153: * Construct a Rights object initialized with the given rights.
154: *
155: * @param rights the rights for initialization
156: */
157: public Rights(Rights rights) {
158: System.arraycopy(rights.rights, 0, this.rights, 0, this.rights.length);
159: }
160:
161: /**
162: * Construct a Rights object initialized with the given rights.
163: *
164: * @param rights the rights for initialization
165: */
166: public Rights(String rights) {
167:• for (int i = 0; i < rights.length(); i++)
168: add(Right.getInstance(rights.charAt(i)));
169: }
170:
171: /**
172: * Construct a Rights object initialized with the given right.
173: *
174: * @param right the right for initialization
175: */
176: public Rights(Right right) {
177: this.rights[(int) right.right] = true;
178: }
179:
180: /**
181: * Add the specified right to this Rights object.
182: *
183: * @param right the right to add
184: */
185: public void add(Right right) {
186: this.rights[(int) right.right] = true;
187: }
188:
189: /**
190: * Add all the rights in the given Rights object to this
191: * Rights object.
192: *
193: * @param rights Rights object
194: */
195: public void add(Rights rights) {
196:• for (int i = 0; i < rights.rights.length; i++)
197:• if (rights.rights[i])
198: this.rights[i] = true;
199: }
200:
201: /**
202: * Remove the specified right from this Rights object.
203: *
204: * @param right the right to be removed
205: */
206: public void remove(Right right) {
207: this.rights[(int) right.right] = false;
208: }
209:
210: /**
211: * Remove all rights in the given Rights object from this
212: * Rights object.
213: *
214: * @param rights the rights to be removed
215: */
216: public void remove(Rights rights) {
217:• for (int i = 0; i < rights.rights.length; i++)
218:• if (rights.rights[i])
219: this.rights[i] = false;
220: }
221:
222: /**
223: * Check whether the specified right is present in this Rights object.
224: *
225: * @return true of the given right is present, otherwise false.
226: * @param right the Right to check
227: */
228: public boolean contains(Right right) {
229: return this.rights[(int) right.right];
230: }
231:
232: /**
233: * Check whether all the rights in the specified Rights object are
234: * present in this Rights object.
235: *
236: * @param rights the Rights to check
237: * @return true if all rights in the given Rights object are present,
238: * otherwise false.
239: */
240: public boolean contains(Rights rights) {
241:• for (int i = 0; i < rights.rights.length; i++)
242:• if (rights.rights[i] && !this.rights[i])
243: return false;
244:
245: // If we've made it till here, return true
246: return true;
247: }
248:
249: /**
250: * Check whether the two Rights objects are equal.
251: *
252: * @return true if they're equal
253: */
254: @Override
255: public boolean equals(Object obj) {
256:• if (!(obj instanceof Rights))
257: return false;
258:
259: Rights rights = (Rights) obj;
260:
261:• for (int i = 0; i < rights.rights.length; i++)
262:• if (rights.rights[i] != this.rights[i])
263: return false;
264:
265: return true;
266: }
267:
268: /**
269: * Compute a hash code for this Rights object.
270: *
271: * @return the hash code
272: */
273: @Override
274: public int hashCode() {
275: int hash = 0;
276:• for (int i = 0; i < this.rights.length; i++)
277:• if (this.rights[i])
278: hash++;
279: return hash;
280: }
281:
282: /**
283: * Return all the rights in this Rights object. Returns
284: * an array of size zero if no rights are set.
285: *
286: * @return array of Rights.Right objects representing rights
287: */
288: public Right[] getRights() {
289: List<Right> v = new ArrayList<>();
290:• for (int i = 0; i < this.rights.length; i++)
291:• if (this.rights[i])
292: v.add(Right.getInstance((char) i));
293: return v.toArray(new Right[0]);
294: }
295:
296: /**
297: * Returns a clone of this Rights object.
298: */
299: @Override
300: public Object clone() {
301: Rights r = null;
302: try {
303: r = (Rights) super.clone();
304: r.rights = new boolean[128];
305: System.arraycopy(this.rights, 0, r.rights, 0, this.rights.length);
306: } catch (CloneNotSupportedException cex) {
307: // ignore, can't happen
308: }
309: return r;
310: }
311:
312: @Override
313: public String toString() {
314: StringBuilder sb = new StringBuilder();
315:• for (int i = 0; i < this.rights.length; i++)
316:• if (this.rights[i])
317: sb.append((char) i);
318: return sb.toString();
319: }
320:
321: /*****
322: public static void main(String argv[]) throws Exception {
323: // a new rights object
324: Rights f1 = new Rights();
325: f1.add(Rights.Right.READ);
326: f1.add(Rights.Right.WRITE);
327: f1.add(Rights.Right.CREATE);
328: f1.add(Rights.Right.DELETE);
329:
330: // check copy constructor
331: Rights fc = new Rights(f1);
332: if (f1.equals(fc) && fc.equals(f1))
333: System.out.println("success");
334: else
335: System.out.println("fail");
336:
337: // check clone
338: fc = (Rights)f1.clone();
339: if (f1.equals(fc) && fc.equals(f1))
340: System.out.println("success");
341: else
342: System.out.println("fail");
343:
344: // add a right and make sure it still works right
345: f1.add(Rights.Right.ADMINISTER);
346:
347: // shouldn't be equal here
348: if (!f1.equals(fc) && !fc.equals(f1))
349: System.out.println("success");
350: else
351: System.out.println("fail");
352:
353: // check clone
354: fc = (Rights)f1.clone();
355: if (f1.equals(fc) && fc.equals(f1))
356: System.out.println("success");
357: else
358: System.out.println("fail");
359:
360: fc.add(Rights.Right.INSERT);
361: if (!f1.equals(fc) && !fc.equals(f1))
362: System.out.println("success");
363: else
364: System.out.println("fail");
365:
366: // check copy constructor
367: fc = new Rights(f1);
368: if (f1.equals(fc) && fc.equals(f1))
369: System.out.println("success");
370: else
371: System.out.println("fail");
372:
373: // another new rights object
374: Rights f2 = new Rights(Rights.Right.READ);
375: f2.add(Rights.Right.WRITE);
376:
377: if (f1.contains(Rights.Right.READ))
378: System.out.println("success");
379: else
380: System.out.println("fail");
381:
382: if (f1.contains(Rights.Right.WRITE))
383: System.out.println("success");
384: else
385: System.out.println("fail");
386:
387: if (f1.contains(Rights.Right.CREATE))
388: System.out.println("success");
389: else
390: System.out.println("fail");
391:
392: if (f1.contains(Rights.Right.DELETE))
393: System.out.println("success");
394: else
395: System.out.println("fail");
396:
397: if (f2.contains(Rights.Right.WRITE))
398: System.out.println("success");
399: else
400: System.out.println("fail");
401:
402:
403: System.out.println("----------------");
404:
405: Right[] r = f1.getRights();
406: for (int i = 0; i < r.length; i++)
407: System.out.println(r[i]);
408: System.out.println("----------------");
409:
410: if (f1.contains(f2)) // this should be true
411: System.out.println("success");
412: else
413: System.out.println("fail");
414:
415: if (!f2.contains(f1)) // this should be false
416: System.out.println("success");
417: else
418: System.out.println("fail");
419:
420: Rights f3 = new Rights();
421: f3.add(Rights.Right.READ);
422: f3.add(Rights.Right.WRITE);
423: f3.add(Rights.Right.CREATE);
424: f3.add(Rights.Right.DELETE);
425: f3.add(Rights.Right.ADMINISTER);
426: f3.add(Rights.Right.LOOKUP);
427:
428: f1.add(Rights.Right.LOOKUP);
429:
430: if (f1.equals(f3))
431: System.out.println("equals success");
432: else
433: System.out.println("fail");
434: if (f3.equals(f1))
435: System.out.println("equals success");
436: else
437: System.out.println("fail");
438: System.out.println("f1 hash code " + f1.hashCode());
439: System.out.println("f3 hash code " + f3.hashCode());
440: if (f1.hashCode() == f3.hashCode())
441: System.out.println("success");
442: else
443: System.out.println("fail");
444: }
445: ****/
446: }