Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Role |
|
| 2.0;2 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.policy; | |
11 | ||
12 | import java.io.Serializable; | |
13 | import java.util.Collection; | |
14 | import java.util.HashSet; | |
15 | import java.util.Set; | |
16 | ||
17 | /** | |
18 | * A reference to a named role within a role hierarchy. | |
19 | * @since 0.3.0 | |
20 | */ | |
21 | public final class Role | |
22 | implements Serializable | |
23 | { | |
24 | ||
25 | //---- Static | |
26 | ||
27 | /** | |
28 | * @since 0.3.0 | |
29 | */ | |
30 | private static final long serialVersionUID = -3359339547258162667L; | |
31 | ||
32 | /** | |
33 | * Factory method for creating a role reference. | |
34 | * @param hierarchy the {@link RoleHierarchy} from which to refer to a role. | |
35 | * @param name the name of the role to refer to. | |
36 | * @return a new {@link Role}. | |
37 | * @since 0.3.0 | |
38 | */ | |
39 | public static Role create (RoleHierarchy hierarchy, String name) { | |
40 | 956 | return new Role(hierarchy, name); |
41 | } | |
42 | ||
43 | /** | |
44 | * Creates a set of roles from a role hierarchy and a collection of role names. | |
45 | * @param hierarchy the {@link RoleHierarchy} to which the roles belong. | |
46 | * @param names a collection of role names. | |
47 | * @return a {@link Set} of roles. | |
48 | * @since 0.3.0 | |
49 | */ | |
50 | public static Set<Role> set (RoleHierarchy hierarchy, Collection<String> names) { | |
51 | 53 | final Set<Role> result = new HashSet<Role>(); |
52 | 53 | for (String name : names) { |
53 | 92 | result.add(create(hierarchy, name)); |
54 | } | |
55 | 53 | return result; |
56 | } | |
57 | ||
58 | //---- State | |
59 | ||
60 | /** | |
61 | * @since 0.3.0 | |
62 | */ | |
63 | private final RoleHierarchy roleHierarchy; | |
64 | ||
65 | /** | |
66 | * @since 0.3.0 | |
67 | */ | |
68 | private final String name; | |
69 | ||
70 | //---- Constructors | |
71 | ||
72 | /** | |
73 | * Creates a new role reference. | |
74 | * @param roleHierarchy the {@link RoleHierarchy} to which the role belongs. | |
75 | * @param name the name identifying the role within its hierarchy. | |
76 | * @since 0.3.0 | |
77 | */ | |
78 | 1154 | public Role (RoleHierarchy roleHierarchy, String name) { |
79 | 1154 | if (roleHierarchy == null) { |
80 | 1 | throw new IllegalArgumentException("RoleHierarchy must not be null."); |
81 | } | |
82 | 1153 | if (name == null) { |
83 | 1 | throw new IllegalArgumentException("Name must not be null."); |
84 | } | |
85 | ||
86 | 1152 | this.roleHierarchy = roleHierarchy; |
87 | 1152 | this.name = name; |
88 | 1152 | } |
89 | ||
90 | //---- Methods | |
91 | ||
92 | /** | |
93 | * @since 0.3.0 | |
94 | */ | |
95 | public RoleHierarchy getRoleHierarchy () { | |
96 | 1234 | return this.roleHierarchy; |
97 | } | |
98 | ||
99 | /** | |
100 | * @since 0.3.0 | |
101 | */ | |
102 | public String getName () { | |
103 | 1063 | return this.name; |
104 | } | |
105 | ||
106 | /** | |
107 | * Returns all the roles from which this role inherits privileges. | |
108 | * @return a {@link Set} of roles from which this role inherits privileges. The passed | |
109 | * role is itself included, so the returned set will always contain at least one role. | |
110 | * @since 0.3.0 | |
111 | */ | |
112 | public Set<Role> getSuperRoles () { | |
113 | 30 | return getRoleSet(getRoleHierarchy().getSuperRoles(getName())); |
114 | } | |
115 | ||
116 | /** | |
117 | * Returns all the roles that inherit privileges from this role. | |
118 | * @return a {@link Set} of roles that inherit privileges from this role. The passed | |
119 | * role is itself included, so the returned set will always contain at least one role. | |
120 | * @since 0.3.0 | |
121 | */ | |
122 | public Set<Role> getSubRoles () { | |
123 | 50 | return getRoleSet(getRoleHierarchy().getSubRoles(getName())); |
124 | } | |
125 | ||
126 | /** | |
127 | * @since 0.3.0 | |
128 | */ | |
129 | private Set<Role> getRoleSet (Set<String> names) { | |
130 | 80 | final Set<Role> roles = new HashSet<Role>(names.size()); |
131 | 80 | for (String roleName : names) { |
132 | 168 | roles.add(new Role(getRoleHierarchy(), roleName)); |
133 | } | |
134 | 80 | return roles; |
135 | } | |
136 | ||
137 | //---- Object | |
138 | ||
139 | /** | |
140 | * @since 0.3.0 | |
141 | */ | |
142 | @Override | |
143 | public String toString () { | |
144 | 0 | final StringBuilder sb = new StringBuilder(getClass().getSimpleName()); |
145 | 0 | sb.append(" name=").append(getName()); |
146 | 0 | sb.append(" [roleHierarchy=").append(getRoleHierarchy()); |
147 | 0 | sb.append("]"); |
148 | 0 | return sb.toString(); |
149 | } | |
150 | ||
151 | /** | |
152 | * @since 0.3.0 | |
153 | */ | |
154 | @Override | |
155 | public boolean equals (Object obj) { | |
156 | 203 | if (obj == this) { |
157 | 8 | return true; |
158 | } | |
159 | 195 | if (!(obj instanceof Role)) { |
160 | 0 | return false; |
161 | } | |
162 | 195 | final Role that = (Role) obj; |
163 | 195 | return this.roleHierarchy.equals(that.roleHierarchy) && this.name.equals(that.name); |
164 | } | |
165 | ||
166 | /** | |
167 | * @since 0.3.0 | |
168 | */ | |
169 | @Override | |
170 | public int hashCode () { | |
171 | 893 | final int factor = 23; |
172 | 893 | return factor * getRoleHierarchy().hashCode() + getName().hashCode(); |
173 | } | |
174 | ||
175 | } |