1- // Copyright 2018 Google LLC
1+ // Copyright 2019 Google LLC
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
2828import java .io .IOException ;
2929import java .io .InputStream ;
3030import java .io .InputStreamReader ;
31+ import java .util .AbstractMap ;
3132import java .util .Arrays ;
33+ import java .util .Iterator ;
3234import java .util .List ;
3335import java .util .stream .Collectors ;
3436
@@ -137,16 +139,25 @@ public static void main(String[] args) throws IOException {
137139 }
138140 }
139141
142+ /** Returns a string representation of the first elements in a list. */
140143 private static String truncated (List <String > array ) {
141- return truncated (array , 2 );
144+ return truncatedTo (array , 2 );
142145 }
143146
144- private static String truncated (List <String > array , int limit ) {
147+ /** Returns a string representation of the first elements in a list. */
148+ private static String truncatedTo (List <String > array , int limit ) {
145149 String contents = array .stream ().limit (limit ).collect (Collectors .joining (", " ));
146150 String more = array .size () > limit ? ", ..." : "" ;
147151 return "[" + contents + more + "]" ;
148152 }
149153
154+ /** Returns the name of a set property in an object, or else "unknown". */
155+ private static <T > String getOneOf (AbstractMap <String , T > obj ) {
156+ Iterator <String > iterator = obj .keySet ().iterator ();
157+ return iterator .hasNext () ? iterator .next () : "unknown" ;
158+ }
159+
160+ /** Returns a time associated with an activity. */
150161 private static String getTimeInfo (DriveActivity activity ) {
151162 if (activity .getTimestamp () != null ) {
152163 return activity .getTimestamp ();
@@ -157,26 +168,30 @@ private static String getTimeInfo(DriveActivity activity) {
157168 return "unknown" ;
158169 }
159170
171+ /** Returns the type of action. */
160172 private static String getActionInfo (ActionDetail actionDetail ) {
161- return actionDetail . keySet (). iterator (). next ( );
173+ return getOneOf ( actionDetail );
162174 }
163175
176+ /** Returns user information, or the type of user if not a known user. */
164177 private static String getUserInfo (User user ) {
165178 if (user .getKnownUser () != null ) {
166179 KnownUser knownUser = user .getKnownUser ();
167180 Boolean isMe = knownUser .getIsCurrentUser ();
168181 return (isMe != null && isMe ) ? "people/me" : knownUser .getPersonName ();
169182 }
170- return user . keySet (). iterator (). next ( );
183+ return getOneOf ( user );
171184 }
172185
186+ /** Returns actor information, or the type of actor if not a user. */
173187 private static String getActorInfo (Actor actor ) {
174188 if (actor .getUser () != null ) {
175189 return getUserInfo (actor .getUser ());
176190 }
177- return actor . keySet (). iterator (). next ( );
191+ return getOneOf ( actor );
178192 }
179193
194+ /** Returns the type of a target and an associated title. */
180195 private static String getTargetInfo (Target target ) {
181196 if (target .getDriveItem () != null ) {
182197 return "driveItem:\" " + target .getDriveItem ().getTitle () + "\" " ;
@@ -191,7 +206,7 @@ private static String getTargetInfo(Target target) {
191206 }
192207 return "fileComment:unknown" ;
193208 }
194- return target . keySet (). iterator (). next ( );
209+ return getOneOf ( target );
195210 }
196211}
197212// [END drive_activity_v2_quickstart]
0 commit comments