Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RoleCollection |
|
| 2.2857142857142856;2.286 |
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.bean; | |
11 | ||
12 | import java.util.ArrayList; | |
13 | import java.util.Collection; | |
14 | import java.util.Collections; | |
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
17 | ||
18 | import org.openpermis.policy.Role; | |
19 | ||
20 | ||
21 | /** | |
22 | * A read-only collection of roles. | |
23 | * @since 0.3.0 | |
24 | */ | |
25 | public class RoleCollection | |
26 | implements Iterable<Role> | |
27 | { | |
28 | ||
29 | //---- Static | |
30 | ||
31 | /** | |
32 | * Does not contain any roles. | |
33 | * @since 0.3.0 | |
34 | */ | |
35 | 1 | public static final RoleCollection EMPTY = new RoleCollection(Collections.<Role>emptyList()); |
36 | ||
37 | /** | |
38 | * Creates a new role collection for the specified roles. | |
39 | * @param roles a {@link Collection} of roles. | |
40 | * @return a {@link RoleCollection} containing exactly the specified roles. | |
41 | * @since 0.3.0 | |
42 | */ | |
43 | public static RoleCollection create (Collection<Role> roles) { | |
44 | 833 | return new RoleCollection(roles); |
45 | } | |
46 | ||
47 | //---- State | |
48 | ||
49 | /** | |
50 | * @since 0.3.0 | |
51 | */ | |
52 | private final List<Role> roles; | |
53 | ||
54 | //---- Constructors | |
55 | ||
56 | /** | |
57 | * Creates a role collection for the given roles. | |
58 | * @param roles a {@link Collection} of roles. | |
59 | * @since 0.3.0 | |
60 | */ | |
61 | 835 | public RoleCollection (Collection<Role> roles) { |
62 | 835 | this.roles = |
63 | roles != null ? new ArrayList<Role>(roles) : Collections.<Role>emptyList(); | |
64 | 835 | } |
65 | ||
66 | //---- Methods | |
67 | ||
68 | /** | |
69 | * Returns whether this collection is empty. | |
70 | * @return <code>true</code> if the collection contains no roles. | |
71 | * @since 0.3.0 | |
72 | */ | |
73 | public boolean isEmpty () { | |
74 | 16 | return this.roles.isEmpty(); |
75 | } | |
76 | ||
77 | /** | |
78 | * Returns a list with the roles that this collection contains. | |
79 | * @return a {@link List} of roles. The caller is free to modify the returned list. | |
80 | * @since 0.3.0 | |
81 | */ | |
82 | public List<Role> toList () { | |
83 | 1663 | return new ArrayList<Role>(this.roles); |
84 | } | |
85 | ||
86 | //---- Object | |
87 | ||
88 | /** | |
89 | * @since 0.3.0 | |
90 | */ | |
91 | @Override | |
92 | public boolean equals (Object obj) { | |
93 | 1643 | if (obj == this) { |
94 | 0 | return true; |
95 | } | |
96 | 1643 | if (!(obj instanceof RoleCollection)) { |
97 | 0 | return false; |
98 | } | |
99 | 1643 | final RoleCollection that = (RoleCollection) obj; |
100 | 1643 | final List<Role> thatRoles = that.toList(); |
101 | 1643 | for (Role role : this.roles) { |
102 | 72 | if (!thatRoles.remove(role)) { |
103 | // No such role could be removed, i.e. it was not present. | |
104 | 3 | return false; |
105 | } | |
106 | } | |
107 | 1640 | return thatRoles.isEmpty(); |
108 | } | |
109 | ||
110 | /** | |
111 | * @since 0.3.0 | |
112 | */ | |
113 | @Override | |
114 | public int hashCode () { | |
115 | 38 | int hashCode = 0; |
116 | 38 | for (Role role : this.roles) { |
117 | 50 | hashCode += role.hashCode(); |
118 | } | |
119 | 38 | return hashCode; |
120 | } | |
121 | ||
122 | //---- Iterable | |
123 | ||
124 | /** | |
125 | * @since 0.3.0 | |
126 | */ | |
127 | public Iterator<Role> iterator () { | |
128 | 50 | return Collections.unmodifiableList(this.roles).iterator(); |
129 | } | |
130 | ||
131 | } |