Convert some Functions/Predicates to streams & lambdas (2)

Change-Id: I6558525ed5df1da588fcf69d177952e4f692cc2d
This commit is contained in:
Dave Borowitz
2016-09-20 10:40:00 -04:00
parent 6647c6c7cb
commit d3848b15a3
14 changed files with 77 additions and 188 deletions

View File

@@ -19,7 +19,6 @@ import static com.google.gerrit.server.notedb.ReviewerStateInternal.REVIEWER;
import static java.util.Comparator.comparing;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Iterables;
@@ -89,12 +88,8 @@ public class ApprovalsUtil {
private static Iterable<PatchSetApproval> filterApprovals(
Iterable<PatchSetApproval> psas, final Account.Id accountId) {
return Iterables.filter(psas, new Predicate<PatchSetApproval>() {
@Override
public boolean apply(PatchSetApproval input) {
return Objects.equals(input.getAccountId(), accountId);
}
});
return Iterables.filter(
psas, a -> Objects.equals(a.getAccountId(), accountId));
}
private final NotesMigration migration;

View File

@@ -16,11 +16,10 @@ package com.google.gerrit.server;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.stream.Collectors.toList;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
@@ -60,6 +59,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.StreamSupport;
/**
* Utility functions to manipulate PatchLineComments.
@@ -178,13 +178,7 @@ public class PatchLineCommentsUtil {
ResultSet<PatchLineComment> comments,
final PatchLineComment.Status status) {
return Lists.newArrayList(
Iterables.filter(comments, new Predicate<PatchLineComment>() {
@Override
public boolean apply(PatchLineComment input) {
return (input.getStatus() == status);
}
})
);
Iterables.filter(comments, c -> c.getStatus() == status));
}
public List<PatchLineComment> byPatchSet(ReviewDb db,
@@ -251,16 +245,11 @@ public class PatchLineCommentsUtil {
throws OrmException {
if (!migration.readChanges()) {
final Change.Id matchId = notes.getChangeId();
return FluentIterable
.from(db.patchComments().draftByAuthor(author))
.filter(new Predicate<PatchLineComment>() {
@Override
public boolean apply(PatchLineComment in) {
Change.Id changeId =
in.getKey().getParentKey().getParentKey().getParentKey();
return changeId.equals(matchId);
}
}).toSortedList(PLC_ORDER);
return StreamSupport.stream(
db.patchComments().draftByAuthor(author).spliterator(), false)
.filter(c -> c.getPatchSetId().getParentKey().equals(matchId))
.sorted(PLC_ORDER)
.collect(toList());
}
List<PatchLineComment> comments = new ArrayList<>();
comments.addAll(notes.getDraftComments(author).values());

View File

@@ -14,8 +14,8 @@
package com.google.gerrit.server.account;
import com.google.common.base.Predicate;
import com.google.common.collect.Sets;
import static java.util.stream.Collectors.toSet;
import com.google.gerrit.common.data.PermissionRule;
import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.reviewdb.client.Account;
@@ -28,7 +28,6 @@ import com.google.gerrit.server.project.ProjectCache;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.HashSet;
import java.util.Set;
/** Access control management for one account's access to other accounts. */
@@ -186,14 +185,9 @@ public class AccountControl {
}
private Set<AccountGroup.UUID> groupsOf(IdentifiedUser user) {
return new HashSet<>(Sets.filter(
user.getEffectiveGroups().getKnownGroups(),
new Predicate<AccountGroup.UUID>() {
@Override
public boolean apply(AccountGroup.UUID in) {
return !SystemGroupBackend.isSystemGroup(in);
}
}));
return user.getEffectiveGroups().getKnownGroups().stream()
.filter(a -> !SystemGroupBackend.isSystemGroup(a))
.collect(toSet());
}
private abstract static class OtherUser {

View File

@@ -17,7 +17,6 @@ package com.google.gerrit.server.index;
import static com.google.gerrit.server.git.QueueProvider.QueueType.BATCH;
import static com.google.gerrit.server.git.QueueProvider.QueueType.INTERACTIVE;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
@@ -110,19 +109,11 @@ public class IndexModule extends LifecycleModule {
accounts,
changes);
Set<String> expected = FluentIterable.from(ALL_SCHEMA_DEFS)
.transform(new Function<SchemaDefinitions<?>, String>() {
@Override
public String apply(SchemaDefinitions<?> in) {
return in.getName();
}
}).toSet();
.transform(SchemaDefinitions::getName)
.toSet();
Set<String> actual = FluentIterable.from(result)
.transform(new Function<IndexDefinition<?, ?, ?>, String>() {
@Override
public String apply(IndexDefinition<?, ?, ?> in) {
return in.getName();
}
}).toSet();
.transform(IndexDefinition::getName)
.toSet();
if (!expected.equals(actual)) {
throw new ProvisionException(
"need index definitions for all schemas: "

View File

@@ -14,14 +14,12 @@
package com.google.gerrit.server.index.account;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import com.google.gerrit.reviewdb.client.AccountExternalId;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.account.WatchConfig.ProjectWatchKey;
import com.google.gerrit.server.index.FieldDef;
import com.google.gerrit.server.index.FieldType;
import com.google.gerrit.server.index.SchemaUtil;
@@ -47,13 +45,7 @@ public class AccountField {
@Override
public Iterable<String> get(AccountState input, FillArgs args) {
return Iterables.transform(
input.getExternalIds(),
new Function<AccountExternalId, String>() {
@Override
public String apply(AccountExternalId in) {
return in.getKey().get();
}
});
input.getExternalIds(), id -> id.getKey().get());
}
};
@@ -68,12 +60,7 @@ public class AccountField {
fullName,
Iterables.transform(
input.getExternalIds(),
new Function<AccountExternalId, String>() {
@Override
public String apply(AccountExternalId in) {
return in.getEmailAddress();
}
}));
AccountExternalId::getEmailAddress));
// Additional values not currently added by getPersonParts.
// TODO(dborowitz): Move to getPersonParts and remove this hack.
@@ -108,23 +95,11 @@ public class AccountField {
@Override
public Iterable<String> get(AccountState input, FillArgs args) {
return FluentIterable.from(input.getExternalIds())
.transform(
new Function<AccountExternalId, String>() {
@Override
public String apply(AccountExternalId in) {
return in.getEmailAddress();
}
})
.transform(AccountExternalId::getEmailAddress)
.append(
Collections.singleton(input.getAccount().getPreferredEmail()))
.filter(Predicates.notNull())
.transform(
new Function<String, String>() {
@Override
public String apply(String in) {
return in.toLowerCase();
}
})
.transform(String::toLowerCase)
.toSet();
}
};
@@ -153,12 +128,8 @@ public class AccountField {
@Override
public Iterable<String> get(AccountState input, FillArgs args) {
return FluentIterable.from(input.getProjectWatches().keySet())
.transform(new Function<ProjectWatchKey, String>() {
@Override
public String apply(ProjectWatchKey in) {
return in.project().get();
}
}).toSet();
.transform(k -> k.project().get())
.toSet();
}
};

View File

@@ -15,10 +15,11 @@
package com.google.gerrit.server.index.change;
import static com.google.common.base.MoreObjects.firstNonNull;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toSet;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
@@ -246,13 +247,9 @@ public class ChangeField {
@Override
public Iterable<String> get(ChangeData input, FillArgs args)
throws OrmException {
return ImmutableSet.copyOf(Iterables.transform(input.hashtags(),
new Function<String, String>() {
@Override
public String apply(String input) {
return input.toLowerCase();
}
}));
return input.hashtags().stream()
.map(String::toLowerCase)
.collect(toSet());
}
};
@@ -263,13 +260,9 @@ public class ChangeField {
@Override
public Iterable<byte[]> get(ChangeData input, FillArgs args)
throws OrmException {
return ImmutableSet.copyOf(Iterables.transform(input.hashtags(),
new Function<String, byte[]>() {
@Override
public byte[] apply(String hashtag) {
return hashtag.getBytes(UTF_8);
}
}));
return input.hashtags().stream()
.map(t -> t.getBytes(UTF_8))
.collect(toSet());
}
};
@@ -656,13 +649,7 @@ public class ChangeField {
@Override
public Iterable<Integer> get(ChangeData input, FillArgs args)
throws OrmException {
return Iterables.transform(input.starredBy(),
new Function<Account.Id, Integer>() {
@Override
public Integer apply(Account.Id accountId) {
return accountId.get();
}
});
return Iterables.transform(input.starredBy(), Account.Id::get);
}
};
@@ -675,13 +662,11 @@ public class ChangeField {
@Override
public Iterable<String> get(ChangeData input, FillArgs args)
throws OrmException {
return Iterables.transform(input.stars().entries(),
new Function<Map.Entry<Account.Id, String>, String>() {
@Override
public String apply(Map.Entry<Account.Id, String> e) {
return Iterables.transform(
input.stars().entries(),
(Map.Entry<Account.Id, String> e) -> {
return StarredChangesUtil.StarField.create(
e.getKey(), e.getValue()).toString();
}
});
}
};
@@ -738,13 +723,9 @@ public class ChangeField {
@Override
public Iterable<Integer> get(ChangeData input, FillArgs args)
throws OrmException {
return ImmutableSet.copyOf(Iterables.transform(input.editsByUser(),
new Function<Account.Id, Integer>() {
@Override
public Integer apply(Account.Id account) {
return account.get();
}
}));
return input.editsByUser().stream()
.map(Account.Id::get)
.collect(toSet());
}
};
@@ -756,13 +737,9 @@ public class ChangeField {
@Override
public Iterable<Integer> get(ChangeData input, FillArgs args)
throws OrmException {
return ImmutableSet.copyOf(Iterables.transform(input.draftsByUser(),
new Function<Account.Id, Integer>() {
@Override
public Integer apply(Account.Id account) {
return account.get();
}
}));
return input.draftsByUser().stream()
.map(Account.Id::get)
.collect(toSet());
}
};

View File

@@ -19,7 +19,6 @@ import static com.google.gerrit.server.index.change.ChangeField.CHANGE;
import static com.google.gerrit.server.index.change.ChangeField.PROJECT;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.gerrit.reviewdb.client.Change;
@@ -94,12 +93,9 @@ public class IndexedChangeQuery extends IndexedQuery<Change.Id, ChangeData>
public Iterator<ChangeData> iterator() {
return Iterables.transform(
rs,
new Function<ChangeData, ChangeData>() {
@Override
public ChangeData apply(ChangeData cd) {
cd -> {
fromSource.put(cd, currSource);
return cd;
}
}).iterator();
}

View File

@@ -383,13 +383,8 @@ public class ChangeBundle {
if (current == null) {
return Predicates.alwaysFalse();
}
final int max = current.get();
return new Predicate<PatchSet.Id>() {
@Override
public boolean apply(PatchSet.Id in) {
return in.get() <= max;
}
};
int max = current.get();
return p -> p.get() <= max;
}
private Map<PatchSet.Id, PatchSet> filterPatchSets() {

View File

@@ -22,7 +22,6 @@ import static com.google.gerrit.server.notedb.NoteDbTable.CHANGES;
import static java.util.Comparator.comparing;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
@@ -77,6 +76,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
/** View of a single {@link Change} based on the log of its notes branch. */
public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
@@ -236,7 +236,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
if (args.migration.enabled()) {
for (Change.Id cid : changeIds) {
ChangeNotes cn = create(db, project, cid);
if (cn.getChange() != null && predicate.apply(cn)) {
if (cn.getChange() != null && predicate.test(cn)) {
notes.add(cn);
}
}
@@ -246,7 +246,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
for (Change c : ReviewDbUtil.unwrapDb(db).changes().get(changeIds)) {
if (c != null && project.equals(c.getDest().getParentKey())) {
ChangeNotes cn = createFromChangeOnlyWhenNoteDbDisabled(c);
if (predicate.apply(cn)) {
if (predicate.test(cn)) {
notes.add(cn);
}
}
@@ -262,7 +262,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
try (Repository repo = args.repoManager.openRepository(project)) {
List<ChangeNotes> changes = scanNoteDb(repo, db, project);
for (ChangeNotes cn : changes) {
if (predicate.apply(cn)) {
if (predicate.test(cn)) {
m.put(project, cn);
}
}
@@ -271,7 +271,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
} else {
for (Change change : ReviewDbUtil.unwrapDb(db).changes().all()) {
ChangeNotes notes = createFromChangeOnlyWhenNoteDbDisabled(change);
if (predicate.apply(notes)) {
if (predicate.test(notes)) {
m.put(change.getProject(), notes);
}
}
@@ -445,16 +445,13 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
// failed.
Multimap<RevId, PatchLineComment> filtered = Multimaps.filterEntries(
draftCommentNotes.getComments(),
new Predicate<Map.Entry<RevId, PatchLineComment>>() {
@Override
public boolean apply(Map.Entry<RevId, PatchLineComment> in) {
for (PatchLineComment c : published.get(in.getKey())) {
if (c.getKey().equals(in.getValue().getKey())) {
(Map.Entry<RevId, PatchLineComment> e) -> {
for (PatchLineComment c : published.get(e.getKey())) {
if (c.getKey().equals(e.getValue().getKey())) {
return false;
}
}
return true;
}
});
return ImmutableListMultimap.copyOf(
filtered);

View File

@@ -20,13 +20,13 @@ import static com.google.common.base.Preconditions.checkState;
import static com.google.gerrit.reviewdb.client.RefNames.changeMetaRef;
import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_HASHTAGS;
import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_PATCH_SET;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toList;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
@@ -391,13 +391,10 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
private static List<PatchLineComment> getPatchLineComments(ChangeBundle bundle,
final PatchSet ps) {
return FluentIterable.from(bundle.getPatchLineComments())
.filter(new Predicate<PatchLineComment>() {
@Override
public boolean apply(PatchLineComment in) {
return in.getPatchSetId().equals(ps.getId());
}
}).toSortedList(PatchLineCommentsUtil.PLC_ORDER);
return bundle.getPatchLineComments().stream()
.filter(c -> c.getPatchSetId().equals(ps.getId()))
.sorted(PatchLineCommentsUtil.PLC_ORDER)
.collect(toList());
}
private void sortAndFillEvents(Change change, Change noteDbChange,

View File

@@ -16,7 +16,6 @@ package com.google.gerrit.server.project;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
@@ -445,13 +444,8 @@ public class ListProjects implements RestReadView<TopLevelResource> {
} else if (matchSubstring != null) {
checkMatchOptions(matchPrefix == null && matchRegex == null);
return Iterables.filter(projectCache.all(),
new Predicate<Project.NameKey>() {
@Override
public boolean apply(Project.NameKey in) {
return in.get().toLowerCase(Locale.US)
.contains(matchSubstring.toLowerCase(Locale.US));
}
});
p -> p.get().toLowerCase(Locale.US)
.contains(matchSubstring.toLowerCase(Locale.US)));
} else if (matchRegex != null) {
checkMatchOptions(matchPrefix == null && matchSubstring == null);
RegexListSearcher<Project.NameKey> searcher;

View File

@@ -17,7 +17,6 @@ package com.google.gerrit.server.project;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.gerrit.extensions.restapi.AuthException;
@@ -124,12 +123,9 @@ public class SetParent implements RestModifyView<ProjectResource, Input> {
+ " not found");
}
if (Iterables.tryFind(parent.tree(), new Predicate<ProjectState>() {
@Override
public boolean apply(ProjectState input) {
return input.getProject().getNameKey()
if (Iterables.tryFind(parent.tree(), p -> {
return p.getProject().getNameKey()
.equals(ctl.getProject().getNameKey());
}
}).isPresent()) {
throw new ResourceConflictException("cycle exists between "
+ ctl.getProject().getName() + " and "

View File

@@ -904,15 +904,12 @@ public class ChangeData {
* @throws OrmException an error occurred reading the database.
*/
public Collection<PatchSet> visiblePatchSets() throws OrmException {
Predicate<PatchSet> predicate = new Predicate<PatchSet>() {
@Override
public boolean apply(PatchSet input) {
Predicate<PatchSet> predicate = ps -> {
try {
return changeControl().isPatchVisible(input, db);
return changeControl().isPatchVisible(ps, db);
} catch (OrmException e) {
return false;
}
}
};
return FluentIterable.from(patchSets()).filter(predicate).toList();
}