1717package com .google .gcloud ;
1818
1919import static com .google .common .base .Preconditions .checkArgument ;
20+ import static com .google .common .base .Preconditions .checkNotNull ;
2021
2122import com .google .common .collect .ImmutableMap ;
2223import com .google .common .collect .ImmutableSet ;
2324
2425import java .io .Serializable ;
2526import java .util .Arrays ;
26- import java .util .Collection ;
2727import java .util .HashMap ;
2828import java .util .HashSet ;
29- import java .util .LinkedList ;
30- import java .util .List ;
29+ import java .util .LinkedHashSet ;
3130import java .util .Map ;
3231import java .util .Objects ;
3332import java .util .Set ;
@@ -69,12 +68,16 @@ protected Builder() {}
6968 /**
7069 * Replaces the builder's map of bindings with the given map of bindings.
7170 *
72- * @throws IllegalArgumentException if the provided map is null or contain any null values
71+ * @throws NullPointerException if the given map is null or contains any null keys or values
72+ * @throws IllegalArgumentException if any identities in the given map are null
7373 */
7474 public final B bindings (Map <R , Set <Identity >> bindings ) {
75- checkArgument (bindings != null , "The provided map of bindings cannot be null." );
75+ checkNotNull (bindings , "The provided map of bindings cannot be null." );
7676 for (Map .Entry <R , Set <Identity >> binding : bindings .entrySet ()) {
77- verifyBinding (binding .getKey (), binding .getValue ());
77+ checkNotNull (binding .getKey (), "The role cannot be null." );
78+ Set <Identity > identities = binding .getValue ();
79+ checkNotNull (identities , "A role cannot be assigned to a null set of identities." );
80+ checkArgument (!identities .contains (null ), "Null identities are not permitted." );
7881 }
7982 this .bindings .clear ();
8083 for (Map .Entry <R , Set <Identity >> binding : bindings .entrySet ()) {
@@ -84,78 +87,50 @@ public final B bindings(Map<R, Set<Identity>> bindings) {
8487 }
8588
8689 /**
87- * Adds a binding to the policy.
88- *
89- * @throws IllegalArgumentException if the policy already contains a binding with the same role
90- * or if the role or any identities are null
91- */
92- public final B addBinding (R role , Set <Identity > identities ) {
93- verifyBinding (role , identities );
94- checkArgument (!bindings .containsKey (role ),
95- "The policy already contains a binding with the role " + role .toString () + "." );
96- bindings .put (role , new HashSet <Identity >(identities ));
97- return self ();
98- }
99-
100- /**
101- * Adds a binding to the policy.
102- *
103- * @throws IllegalArgumentException if the policy already contains a binding with the same role
104- * or if the role or any identities are null
105- */
106- public final B addBinding (R role , Identity first , Identity ... others ) {
107- HashSet <Identity > identities = new HashSet <>();
108- identities .add (first );
109- identities .addAll (Arrays .asList (others ));
110- return addBinding (role , identities );
111- }
112-
113- private void verifyBinding (R role , Collection <Identity > identities ) {
114- checkArgument (role != null , "The role cannot be null." );
115- verifyIdentities (identities );
116- }
117-
118- private void verifyIdentities (Collection <Identity > identities ) {
119- checkArgument (identities != null , "A role cannot be assigned to a null set of identities." );
120- checkArgument (!identities .contains (null ), "Null identities are not permitted." );
121- }
122-
123- /**
124- * Removes the binding associated with the specified role.
90+ * Removes the role (and all identities associated with that role) from the policy.
12591 */
126- public final B removeBinding (R role ) {
92+ public final B removeRole (R role ) {
12793 bindings .remove (role );
12894 return self ();
12995 }
13096
13197 /**
132- * Adds one or more identities to an existing binding .
98+ * Adds one or more identities to the policy under the role specified .
13399 *
134- * @throws IllegalArgumentException if the policy doesn't contain a binding with the specified
135- * role or any identities are null
100+ * @throws NullPointerException if the role or any of the identities is null.
136101 */
137102 public final B addIdentity (R role , Identity first , Identity ... others ) {
138- checkArgument (bindings .containsKey (role ),
139- "The policy doesn't contain the role " + role .toString () + "." );
140- List <Identity > toAdd = new LinkedList <>();
103+ String nullIdentityMessage = "Null identities are not permitted." ;
104+ checkNotNull (first , nullIdentityMessage );
105+ checkNotNull (others , nullIdentityMessage );
106+ for (Identity identity : others ) {
107+ checkNotNull (identity , nullIdentityMessage );
108+ }
109+ Set <Identity > toAdd = new LinkedHashSet <>();
141110 toAdd .add (first );
142111 toAdd .addAll (Arrays .asList (others ));
143- verifyIdentities (toAdd );
144- bindings .get (role ).addAll (toAdd );
112+ Set <Identity > identities = bindings .get (checkNotNull (role , "The role cannot be null." ));
113+ if (identities == null ) {
114+ identities = new HashSet <Identity >();
115+ bindings .put (role , identities );
116+ }
117+ identities .addAll (toAdd );
145118 return self ();
146119 }
147120
148121 /**
149- * Removes one or more identities from an existing binding.
150- *
151- * @throws IllegalArgumentException if the policy doesn't contain a binding with the specified
152- * role
122+ * Removes one or more identities from an existing binding. Does nothing if the binding
123+ * associated with the provided role doesn't exist.
153124 */
154125 public final B removeIdentity (R role , Identity first , Identity ... others ) {
155- checkArgument (bindings .containsKey (role ),
156- "The policy doesn't contain the role " + role .toString () + "." );
157- bindings .get (role ).remove (first );
158- bindings .get (role ).removeAll (Arrays .asList (others ));
126+ Set <Identity > identities = bindings .get (role );
127+ if (identities != null ) {
128+ identities .remove (first );
129+ identities .removeAll (Arrays .asList (others ));
130+ }
131+ if (identities != null && identities .isEmpty ()) {
132+ bindings .remove (role );
133+ }
159134 return self ();
160135 }
161136
0 commit comments