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 ()) {
@@ -83,30 +86,30 @@ public final B bindings(Map<R, Set<Identity>> bindings) {
8386 return self ();
8487 }
8588
86- private void verifyBinding (R role , Collection <Identity > identities ) {
87- checkArgument (role != null , "The role cannot be null." );
88- checkArgument (identities != null , "A role cannot be assigned to a null set of identities." );
89- checkArgument (!identities .contains (null ), "Null identities are not permitted." );
90- }
91-
9289 /**
93- * Removes the binding associated with the specified role.
90+ * Removes the role (and all identities associated with that role) from the policy .
9491 */
95- public final B removeBinding (R role ) {
92+ public final B removeRole (R role ) {
9693 bindings .remove (role );
9794 return self ();
9895 }
9996
10097 /**
101- * Adds one or more identities to the policy under the role specified. Creates a new role
102- * binding if the binding corresponding to the given role did not previously exist.
98+ * Adds one or more identities to the policy under the role specified.
99+ *
100+ * @throws NullPointerException if the role or any of the identities is null.
103101 */
104102 public final B addIdentity (R role , Identity first , Identity ... others ) {
105- 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 <>();
106110 toAdd .add (first );
107111 toAdd .addAll (Arrays .asList (others ));
108- verifyBinding (role , toAdd );
109- Set <Identity > identities = bindings .get (role );
112+ Set <Identity > identities = bindings .get (checkNotNull (role , "The role cannot be null." ));
110113 if (identities == null ) {
111114 identities = new HashSet <Identity >();
112115 bindings .put (role , identities );
@@ -125,6 +128,9 @@ public final B removeIdentity(R role, Identity first, Identity... others) {
125128 identities .remove (first );
126129 identities .removeAll (Arrays .asList (others ));
127130 }
131+ if (identities != null && identities .isEmpty ()) {
132+ bindings .remove (role );
133+ }
128134 return self ();
129135 }
130136
0 commit comments