AccountState#getUserName: Return Optional<String> instead of nullable String

This makes it more explicit that callers must handle the case where the
returned username is absent.

Change-Id: I01d2e3fd32726db6175c157bc71a551d35b4803f
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-01-18 18:09:34 +01:00
parent b820248633
commit 9cb0b0cb88
18 changed files with 45 additions and 43 deletions

View File

@@ -163,8 +163,8 @@ class BecomeAnyAccountLoginServlet extends HttpServlet {
AccountState accountState = accountCache.get(accountId);
Account account = accountState.getAccount();
String displayName;
if (accountState.getUserName() != null) {
displayName = accountState.getUserName();
if (accountState.getUserName().isPresent()) {
displayName = accountState.getUserName().get();
} else if (account.getFullName() != null && !account.getFullName().isEmpty()) {
displayName = account.getFullName();
} else if (account.getPreferredEmail() != null) {

View File

@@ -293,8 +293,9 @@ public class IdentifiedUser extends CurrentUser {
/** @return the user's user name; null if one has not been selected/assigned. */
@Override
@Nullable
public String getUserName() {
return state().getUserName();
return state().getUserName().orElse(null);
}
public Account getAccount() {

View File

@@ -112,23 +112,26 @@ public class AccountDeactivator implements Runnable {
}
}
private boolean processAccount(AccountState account) {
log.debug("processing account " + account.getUserName());
private boolean processAccount(AccountState accountState) {
if (!accountState.getUserName().isPresent()) {
return false;
}
String userName = accountState.getUserName().get();
log.debug("processing account " + userName);
try {
if (account.getUserName() != null
&& realm.accountBelongsToRealm(account.getExternalIds())
&& !realm.isActive(account.getUserName())) {
sif.deactivate(account.getAccount().getId());
log.info("deactivated account " + account.getUserName());
if (realm.accountBelongsToRealm(accountState.getExternalIds()) && !realm.isActive(userName)) {
sif.deactivate(accountState.getAccount().getId());
log.info("deactivated account " + userName);
return true;
}
} catch (ResourceConflictException e) {
log.info("Account {} already deactivated, continuing...", account.getUserName());
log.info("Account {} already deactivated, continuing...", userName);
} catch (Exception e) {
log.error(
"Error deactivating account: {} ({}) {}",
account.getUserName(),
account.getAccount().getId(),
userName,
accountState.getAccount().getId(),
e.getMessage(),
e);
}

View File

@@ -196,9 +196,8 @@ public class AccountState {
*
* <p>The username is the {@link ExternalId} using the scheme {@link ExternalId#SCHEME_USERNAME}.
*/
@Nullable
public String getUserName() {
return userName.orElse(null);
public Optional<String> getUserName() {
return userName;
}
public boolean checkPassword(String password, String username) {

View File

@@ -88,7 +88,7 @@ public class InternalAccountDirectory extends AccountDirectory {
if (options.contains(FillOptions.NAME)) {
info.name = Strings.emptyToNull(account.getFullName());
if (info.name == null) {
info.name = accountState.getUserName();
info.name = accountState.getUserName().orElse(null);
}
}
if (options.contains(FillOptions.EMAIL)) {

View File

@@ -590,7 +590,7 @@ public class EventFactory {
AccountAttribute who = new AccountAttribute();
who.name = accountState.getAccount().getFullName();
who.email = accountState.getAccount().getPreferredEmail();
who.username = accountState.getUserName();
who.username = accountState.getUserName().orElse(null);
return who;
}

View File

@@ -102,7 +102,7 @@ public class EventUtil {
AccountInfo accountInfo = new AccountInfo(account.getId().get());
accountInfo.email = account.getPreferredEmail();
accountInfo.name = account.getFullName();
accountInfo.username = accountState.getUserName();
accountInfo.username = accountState.getUserName().orElse(null);
return accountInfo;
}

View File

@@ -156,7 +156,7 @@ class DbGroupMemberAuditListener implements GroupMemberAuditListener {
List<String> descriptions = new ArrayList<>();
for (AccountGroupMember m : values) {
Account.Id accountId = m.getAccountId();
String userName = accountCache.get(accountId).getUserName();
String userName = accountCache.get(accountId).getUserName().orElse(null);
AccountGroup.Id groupId = m.getAccountGroupId();
String groupName = getGroupName(groupId);
@@ -193,7 +193,7 @@ class DbGroupMemberAuditListener implements GroupMemberAuditListener {
message.append(" ");
message.append(me);
message.append("/");
message.append(accountCache.get(me).getUserName());
message.append(accountCache.get(me).getUserName().orElse(null));
message.append(": ");
message.append(Joiner.on("; ").join(values));
log.error(message.toString(), e);

View File

@@ -22,7 +22,6 @@ import static com.google.gerrit.index.FieldDef.timestamp;
import static java.util.stream.Collectors.toSet;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
@@ -116,7 +115,7 @@ public class AccountField {
timestamp("registered").build(a -> a.getAccount().getRegisteredOn());
public static final FieldDef<AccountState, String> USERNAME =
exact("username").build(a -> Strings.nullToEmpty(a.getUserName()).toLowerCase());
exact("username").build(a -> a.getUserName().map(String::toLowerCase).orElse(""));
public static final FieldDef<AccountState, Iterable<String>> WATCHED_PROJECT =
exact("watchedproject")

View File

@@ -389,11 +389,7 @@ public abstract class OutgoingEmail {
} else if (name != null) {
return name;
}
String username = who.getUserName();
if (username != null) {
return username;
}
return null;
return who.getUserName().orElse(null);
}
protected boolean shouldSendMessage() {

View File

@@ -61,7 +61,7 @@ class GetOAuthToken implements RestReadView<AccountResource> {
throw new ResourceNotFoundException();
}
OAuthTokenInfo accessTokenInfo = new OAuthTokenInfo();
accessTokenInfo.username = rsrc.getUser().state().getUserName();
accessTokenInfo.username = rsrc.getUser().state().getUserName().orElse(null);
accessTokenInfo.resourceHost = getHostName(canonicalWebUrlProvider.get());
accessTokenInfo.accessToken = accessToken.getToken();
accessTokenInfo.providerId = accessToken.getProviderId();

View File

@@ -28,10 +28,6 @@ public class GetUsername implements RestReadView<AccountResource> {
@Override
public String apply(AccountResource rsrc) throws AuthException, ResourceNotFoundException {
String username = rsrc.getUser().state().getUserName();
if (username == null) {
throw new ResourceNotFoundException();
}
return username;
return rsrc.getUser().state().getUserName().orElseThrow(ResourceNotFoundException::new);
}
}

View File

@@ -320,7 +320,7 @@ public abstract class BaseCommand implements Command {
if (user.isIdentifiedUser()) {
final IdentifiedUser u = user.asIdentifiedUser();
m.append(" (user ");
m.append(u.state().getUserName());
m.append(u.state().getUserName().orElse(null));
m.append(" account ");
m.append(u.getAccountId());
m.append(")");
@@ -381,7 +381,9 @@ public abstract class BaseCommand implements Command {
m.append(getTaskDescription());
if (user.isIdentifiedUser()) {
IdentifiedUser u = user.asIdentifiedUser();
m.append(" (").append(u.state().getUserName()).append(")");
if (u.state().getUserName().isPresent()) {
m.append(" (").append(u.state().getUserName().get()).append(")");
}
}
return m.toString();
}

View File

@@ -235,7 +235,7 @@ class SshLog implements LifecycleListener {
if (user != null && user.isIdentifiedUser()) {
IdentifiedUser u = user.asIdentifiedUser();
userName = u.state().getUserName();
userName = u.state().getUserName().orElse(null);
accountId = "a/" + u.getAccountId().toString();
} else if (user instanceof PeerDaemonUser) {

View File

@@ -116,7 +116,7 @@ final class Receive extends AbstractGitCommand {
StringBuilder msg = new StringBuilder();
msg.append("Receive error on project \"").append(projectState.getName()).append("\"");
msg.append(" (user ");
msg.append(currentUser.state().getUserName());
msg.append(currentUser.state().getUserName().orElse(null));
msg.append(" account ");
msg.append(currentUser.getAccountId());
msg.append("): ");

View File

@@ -38,6 +38,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.apache.sshd.common.io.IoAcceptor;
import org.apache.sshd.common.io.IoSession;
import org.apache.sshd.common.io.mina.MinaAcceptor;
@@ -203,9 +204,9 @@ final class ShowConnections extends SshCommand {
IdentifiedUser u = user.asIdentifiedUser();
if (!numeric) {
String name = u.state().getUserName();
if (name != null && !name.isEmpty()) {
return name;
Optional<String> name = u.state().getUserName().filter(n -> !n.isEmpty());
if (name.isPresent()) {
return name.get();
}
}

View File

@@ -107,7 +107,12 @@ final class StreamEvents extends BaseCommand {
@Override
public String toString() {
return "Stream Events (" + currentUser.state().getUserName() + ")";
StringBuilder b = new StringBuilder();
b.append("Stream Events");
if (currentUser.state().getUserName().isPresent()) {
b.append(" (" + currentUser.state().getUserName().get() + ")");
}
return b.toString();
}
};