1818
1919import static com .google .common .base .Preconditions .checkNotNull ;
2020
21+ import com .google .api .core .ApiFunction ;
2122import com .google .common .base .CaseFormat ;
2223
2324import java .io .Serializable ;
@@ -48,52 +49,91 @@ public final class Identity implements Serializable {
4849 /**
4950 * The types of IAM identities.
5051 */
51- public enum Type {
52+ public static final class Type extends StringEnumValue {
53+ private static final long serialVersionUID = 3809891273596003916L ;
54+
55+ private Type (String constant ) {
56+ super (constant );
57+ }
58+
59+ private static final ApiFunction <String , Type > CONSTRUCTOR =
60+ new ApiFunction <String , Type >() {
61+ @ Override
62+ public Type apply (String constant ) {
63+ return new Type (constant );
64+ }
65+ };
66+
67+ private static final StringEnumType <Type > type = new StringEnumType (
68+ Type .class ,
69+ CONSTRUCTOR );
5270
5371 /**
5472 * Represents anyone who is on the internet; with or without a Google account.
5573 */
56- ALL_USERS ,
74+ public static final Type ALL_USERS = type . createAndRegister ( "ALL_USERS" );
5775
5876 /**
5977 * Represents anyone who is authenticated with a Google account or a service account.
6078 */
61- ALL_AUTHENTICATED_USERS ,
79+ public static final Type ALL_AUTHENTICATED_USERS = type . createAndRegister ( "ALL_AUTHENTICATED_USERS" );
6280
6381 /**
6482 * Represents a specific Google account.
6583 */
66- USER ,
84+ public static final Type USER = type . createAndRegister ( "USER" );
6785
6886 /**
6987 * Represents a service account.
7088 */
71- SERVICE_ACCOUNT ,
89+ public static final Type SERVICE_ACCOUNT = type . createAndRegister ( "SERVICE_ACCOUNT" );
7290
7391 /**
7492 * Represents a Google group.
7593 */
76- GROUP ,
94+ public static final Type GROUP = type . createAndRegister ( "GROUP" );
7795
7896 /**
7997 * Represents all the users of a Google Apps domain name.
8098 */
81- DOMAIN ,
82-
99+ public static final Type DOMAIN = type . createAndRegister ( "DOMAIN" );
100+
83101 /**
84102 * Represents owners of a Google Cloud Platform project.
85103 */
86- PROJECT_OWNER ,
87-
104+ public static final Type PROJECT_OWNER = type . createAndRegister ( "PROJECT_OWNER" );
105+
88106 /**
89107 * Represents editors of a Google Cloud Platform project.
90108 */
91- PROJECT_EDITOR ,
92-
109+ public static final Type PROJECT_EDITOR = type . createAndRegister ( "PROJECT_EDITOR" );
110+
93111 /**
94112 * Represents viewers of a Google Cloud Platform project.
95113 */
96- PROJECT_VIEWER
114+ public static final Type PROJECT_VIEWER = type .createAndRegister ("PROJECT_VIEWER" );
115+
116+ /**
117+ * Get the Type for the given String constant, and throw an exception if the constant is
118+ * not recognized.
119+ */
120+ public static Type valueOfStrict (String constant ) {
121+ return type .valueOfStrict (constant );
122+ }
123+
124+ /**
125+ * Get the Type for the given String constant, and allow unrecognized values.
126+ */
127+ public static Type valueOf (String constant ) {
128+ return type .valueOf (constant );
129+ }
130+
131+ /**
132+ * Return the known values for Type.
133+ */
134+ public static Type [] values () {
135+ return type .values ();
136+ }
97137 }
98138
99139 private Identity (Type type , String value ) {
@@ -225,27 +265,11 @@ public boolean equals(Object obj) {
225265 * {@code Identity} objects to strings for protobuf-generated policies.
226266 */
227267 public String strValue () {
228- switch (type ) {
229- case ALL_USERS :
230- return "allUsers" ;
231- case ALL_AUTHENTICATED_USERS :
232- return "allAuthenticatedUsers" ;
233- case USER :
234- return "user:" + value ;
235- case SERVICE_ACCOUNT :
236- return "serviceAccount:" + value ;
237- case GROUP :
238- return "group:" + value ;
239- case DOMAIN :
240- return "domain:" + value ;
241- case PROJECT_OWNER :
242- return "projectOwner:" + value ;
243- case PROJECT_EDITOR :
244- return "projectEditor:" + value ;
245- case PROJECT_VIEWER :
246- return "projectViewer:" + value ;
247- default :
248- throw new IllegalStateException ("Unexpected identity type: " + type );
268+ String protobufString = CaseFormat .UPPER_UNDERSCORE .to (CaseFormat .LOWER_CAMEL , type .toString ());
269+ if (value == null ) {
270+ return protobufString ;
271+ } else {
272+ return protobufString + ":" + value ;
249273 }
250274 }
251275
@@ -256,27 +280,12 @@ public String strValue() {
256280 public static Identity valueOf (String identityStr ) {
257281 String [] info = identityStr .split (":" );
258282 Type type = Type .valueOf (CaseFormat .LOWER_CAMEL .to (CaseFormat .UPPER_UNDERSCORE , info [0 ]));
259- switch (type ) {
260- case ALL_USERS :
261- return Identity .allUsers ();
262- case ALL_AUTHENTICATED_USERS :
263- return Identity .allAuthenticatedUsers ();
264- case USER :
265- return Identity .user (info [1 ]);
266- case SERVICE_ACCOUNT :
267- return Identity .serviceAccount (info [1 ]);
268- case GROUP :
269- return Identity .group (info [1 ]);
270- case DOMAIN :
271- return Identity .domain (info [1 ]);
272- case PROJECT_OWNER :
273- return Identity .projectOwner (info [1 ]);
274- case PROJECT_EDITOR :
275- return Identity .projectEditor (info [1 ]);
276- case PROJECT_VIEWER :
277- return Identity .projectViewer (info [1 ]);
278- default :
279- throw new IllegalStateException ("Unexpected identity type " + type );
283+ if (info .length == 1 ) {
284+ return new Identity (type , null );
285+ } else if (info .length == 2 ){
286+ return new Identity (type , info [1 ]);
287+ } else {
288+ throw new IllegalArgumentException ("Illegal identity string: \" " + identityStr + "\" " );
280289 }
281290 }
282291}
0 commit comments