CurrentUser: Return Optional from methods that retrieve a property

Returning Optional makes it more explicit that callers must expect that
the return value is absent.

Change-Id: If739844482ac8966dbcdc2285869cfac7ab7e059
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-02-02 11:42:15 +01:00
parent 79c916c462
commit 8128e3f149
5 changed files with 28 additions and 24 deletions

View File

@@ -41,6 +41,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.eclipse.jgit.errors.ConfigInvalidException;
@Singleton
@@ -81,7 +82,7 @@ public class DeleteExternalIds implements RestModifyView<AccountResource, List<S
.collect(toMap(i -> i.key(), i -> i));
List<ExternalId> toDelete = new ArrayList<>();
ExternalId.Key last = resource.getUser().getLastLoginExternalIdKey();
Optional<ExternalId.Key> last = resource.getUser().getLastLoginExternalIdKey();
for (String externalIdStr : extIds) {
ExternalId id = externalIdMap.get(ExternalId.Key.parse(externalIdStr));
@@ -91,7 +92,7 @@ public class DeleteExternalIds implements RestModifyView<AccountResource, List<S
}
if ((!id.isScheme(SCHEME_USERNAME))
&& ((last == null) || (!last.get().equals(id.key().get())))) {
&& (!last.isPresent() || (!last.get().equals(id.key())))) {
toDelete.add(id);
} else {
throw new ResourceConflictException(

View File

@@ -37,6 +37,7 @@ import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@Singleton
public class GetExternalIds implements RestReadView<AccountResource> {
@@ -78,8 +79,8 @@ public class GetExternalIds implements RestReadView<AccountResource> {
// establish this web session, and if only if an identity was
// actually used to establish this web session.
if (!id.isScheme(SCHEME_USERNAME)) {
ExternalId.Key last = resource.getUser().getLastLoginExternalIdKey();
info.canDelete = toBoolean(last == null || !last.get().equals(info.identity));
Optional<ExternalId.Key> last = resource.getUser().getLastLoginExternalIdKey();
info.canDelete = toBoolean(!last.isPresent() || !last.get().get().equals(info.identity));
}
result.add(info);
}