Merge branch 'stable-2.15'

* stable-2.15:
  SingleUserGroupTest: Reformat with google-java-format 1.6

Change-Id: I097cc10a5b53135dbb0acf4dab5e78bf333cef22
diff --git a/src/main/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroup.java b/src/main/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroup.java
index 6138171..0ba1210 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroup.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroup.java
@@ -15,11 +15,10 @@
 package com.googlesource.gerrit.plugins.singleusergroup;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.stream.Collectors.toList;
 
-import com.google.common.base.Function;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
 import com.google.gerrit.common.Nullable;
 import com.google.gerrit.common.data.GroupDescription;
 import com.google.gerrit.common.data.GroupReference;
@@ -34,6 +33,7 @@
 import com.google.gerrit.server.account.GroupBackend;
 import com.google.gerrit.server.account.GroupMembership;
 import com.google.gerrit.server.account.ListGroupMembership;
+import com.google.gerrit.server.account.externalids.ExternalId;
 import com.google.gerrit.server.project.ProjectState;
 import com.google.gerrit.server.query.account.AccountPredicates;
 import com.google.gerrit.server.query.account.AccountQueryBuilder;
@@ -45,6 +45,7 @@
 import com.google.inject.Singleton;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Optional;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -94,8 +95,8 @@
   public GroupMembership membershipsOf(IdentifiedUser user) {
     ImmutableList.Builder<AccountGroup.UUID> groups = ImmutableList.builder();
     groups.add(uuid(user.getAccountId()));
-    if (user.getUserName() != null) {
-      groups.add(uuid(user.getUserName()));
+    if (user.getUserName().isPresent()) {
+      groups.add(uuid(user.getUserName().get()));
     }
     return new ListGroupMembership(groups.build());
   }
@@ -103,17 +104,17 @@
   @Override
   public GroupDescription.Basic get(AccountGroup.UUID uuid) {
     String ident = username(uuid);
-    AccountState state;
+    Optional<AccountState> state;
     if (ident.matches(ACCOUNT_ID_PATTERN)) {
       state = accountCache.get(new Account.Id(Integer.parseInt(ident)));
-    } else if (ident.matches(Account.USER_NAME_PATTERN)) {
+    } else if (ExternalId.isValidUsername(ident)) {
       state = accountCache.getByUsername(ident);
     } else {
       return null;
     }
-    if (state != null) {
-      final String name = nameOf(uuid, state);
-      final String email = Strings.emptyToNull(state.getAccount().getPreferredEmail());
+    if (state.isPresent()) {
+      String name = nameOf(uuid, state.get());
+      String email = Strings.emptyToNull(state.get().getAccount().getPreferredEmail());
       return new GroupDescription.Basic() {
         @Override
         public AccountGroup.UUID getGroupUUID() {
@@ -144,30 +145,26 @@
   @Override
   public Collection<GroupReference> suggest(String name, @Nullable ProjectState project) {
     try {
-      return Lists.transform(
-          queryProvider
-              .get()
-              .setUserProvidedLimit(MAX)
-              .query(AccountPredicates.andActive(queryBuilder.defaultQuery(name)))
-              .entities(),
-          new Function<AccountState, GroupReference>() {
-            @Override
-            public GroupReference apply(AccountState state) {
-              AccountGroup.UUID uuid;
-              if (state.getUserName() != null) {
-                uuid = uuid(state.getUserName());
-              } else {
-                uuid = uuid(state.getAccount().getId());
-              }
-              return new GroupReference(uuid, nameOf(uuid, state));
-            }
-          });
+      return queryProvider
+          .get()
+          .setUserProvidedLimit(MAX)
+          .query(AccountPredicates.andActive(queryBuilder.defaultQuery(name)))
+          .entities()
+          .stream()
+          .map(SingleUserGroup::accountToGroup)
+          .collect(toList());
     } catch (OrmException | QueryParseException err) {
       log.warn("Cannot suggest users", err);
       return Collections.emptyList();
     }
   }
 
+  private static GroupReference accountToGroup(AccountState s) {
+    AccountGroup.UUID uuid =
+        s.getUserName().isPresent() ? uuid(s.getUserName().get()) : uuid(s.getAccount().getId());
+    return new GroupReference(uuid, nameOf(uuid, s));
+  }
+
   private static String username(AccountGroup.UUID uuid) {
     checkUUID(uuid);
     return uuid.get().substring(UUID_PREFIX.length());
@@ -186,21 +183,21 @@
         uuid.get().startsWith(UUID_PREFIX), "SingleUserGroup does not handle %s", uuid.get());
   }
 
-  private static String nameOf(AccountGroup.UUID uuid, AccountState account) {
+  private static String nameOf(AccountGroup.UUID uuid, AccountState accountState) {
     StringBuilder buf = new StringBuilder();
-    if (account.getAccount().getFullName() != null) {
-      buf.append(account.getAccount().getFullName());
+    if (accountState.getAccount().getFullName() != null) {
+      buf.append(accountState.getAccount().getFullName());
     }
-    if (account.getUserName() != null) {
+    if (accountState.getUserName().isPresent()) {
       if (buf.length() > 0) {
-        buf.append(" (").append(account.getUserName()).append(")");
+        buf.append(" (").append(accountState.getUserName().get()).append(")");
       } else {
-        buf.append(account.getUserName());
+        buf.append(accountState.getUserName().get());
       }
     } else if (buf.length() > 0) {
-      buf.append(" (").append(account.getAccount().getId().get()).append(")");
+      buf.append(" (").append(accountState.getAccount().getId().get()).append(")");
     } else {
-      buf.append(account.getAccount().getId().get());
+      buf.append(accountState.getAccount().getId().get());
     }
 
     String ident = username(uuid);