1 | package edu.stanford.rt.credential; |
---|
2 | |
---|
3 | import java.util.*; |
---|
4 | |
---|
5 | /** |
---|
6 | * @author Ninghui Li, Sandra Qiu<br> |
---|
7 | * |
---|
8 | * Implementation of <code>IntersectionContainment</code> element. |
---|
9 | * |
---|
10 | */ |
---|
11 | public class RoleIntersection implements PrincipalExpression |
---|
12 | { |
---|
13 | /** |
---|
14 | * list of members in this intersection. |
---|
15 | */ |
---|
16 | private ArrayList parts; // required element. |
---|
17 | |
---|
18 | /** |
---|
19 | * Constuctor for an empty RoleIntersection. |
---|
20 | */ |
---|
21 | public RoleIntersection() |
---|
22 | { |
---|
23 | parts = new ArrayList(); |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * Constructor for RoleIntersection |
---|
28 | */ |
---|
29 | public RoleIntersection(List p) |
---|
30 | { |
---|
31 | if (p == null || p.size() == 0) |
---|
32 | throw new IllegalArgumentException("RoleIntersection: empty parts."); |
---|
33 | // copy over |
---|
34 | parts = new ArrayList(); |
---|
35 | Iterator it = p.iterator(); |
---|
36 | while (it.hasNext()) |
---|
37 | { |
---|
38 | Object pr = it.next(); |
---|
39 | if (!(pr instanceof RoleTerm) && !(pr instanceof Role)) |
---|
40 | throw new IllegalArgumentException("RoleIntersection: wrong element type"); |
---|
41 | parts.add(pr); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Method getParts. |
---|
47 | * returns an unmodifiable view of the members. |
---|
48 | * @return List |
---|
49 | */ |
---|
50 | public List getParts() |
---|
51 | { |
---|
52 | return Collections.unmodifiableList(parts); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Method and. |
---|
57 | * AND operation. |
---|
58 | * @param part |
---|
59 | * @throws CredException |
---|
60 | */ |
---|
61 | public void and(Object part) throws CredException |
---|
62 | { |
---|
63 | if (!(part instanceof RoleTerm) && !(part instanceof Role)) |
---|
64 | throw new CredException("Illegal part type"); |
---|
65 | |
---|
66 | // add this new part to the set. |
---|
67 | if (!parts.contains(part)) |
---|
68 | parts.add(part); |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | /* (non-Javadoc) |
---|
73 | * @see edu.stanford.rt.credential.PrincipalExpression#toString(String) |
---|
74 | */ |
---|
75 | public String toString(String indent) |
---|
76 | { |
---|
77 | String thisIndent = indent + " "; |
---|
78 | StringBuffer sb = new StringBuffer(); |
---|
79 | sb.append(thisIndent).append("Role Intersection: \n"); |
---|
80 | for (int i = 0; i < parts.size(); i++) |
---|
81 | { |
---|
82 | if ((parts.get(i)) instanceof Role) |
---|
83 | { |
---|
84 | sb.append( |
---|
85 | ((Role) parts.get(i)).toString( |
---|
86 | thisIndent + " ")); |
---|
87 | } |
---|
88 | else if ((parts.get(i)) instanceof RoleTerm) |
---|
89 | { |
---|
90 | sb.append( |
---|
91 | ((RoleTerm) parts.get(i)).toString( |
---|
92 | thisIndent + " ")); |
---|
93 | } |
---|
94 | sb.append("\n"); |
---|
95 | } |
---|
96 | return sb.toString(); |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|