Merge branch 'stable-2.15' * stable-2.15: Bazel: Reformat BUILD file with buildifier 0.17.2 Change-Id: I35086c7360d0fb3f80b20397eda39733b2701adb
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 bfe18c1..18875cd 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,11 @@ 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.common.flogger.FluentLogger; import com.google.gerrit.common.Nullable; import com.google.gerrit.common.data.GroupDescription; import com.google.gerrit.common.data.GroupReference; @@ -46,8 +46,7 @@ import com.google.inject.Singleton; import java.util.Collection; import java.util.Collections; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.Optional; /** * Makes a group out of each user. @@ -57,7 +56,7 @@ */ @Singleton public class SingleUserGroup extends AbstractGroupBackend { - private static final Logger log = LoggerFactory.getLogger(SingleUserGroup.class); + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final String UUID_PREFIX = "user:"; private static final String NAME_PREFIX = "user/"; @@ -95,8 +94,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()); } @@ -104,7 +103,7 @@ @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 (ExternalId.isValidUsername(ident)) { @@ -112,9 +111,9 @@ } 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() { @@ -145,30 +144,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); + logger.atWarning().withCause(err).log("Cannot suggest users"); 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()); @@ -187,21 +182,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);