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

Change-Id: I8a3b8f1247df99f824c3b4d34c7573b5d16a10a4
This commit is contained in:
Dave Borowitz
2016-09-20 11:52:03 -04:00
parent 8c6540e84b
commit eed3ed3b1a
16 changed files with 100 additions and 208 deletions

View File

@@ -14,7 +14,6 @@
package com.google.gerrit.acceptance;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.mail.Address;
@@ -29,13 +28,7 @@ import java.util.Arrays;
public class TestAccount {
public static FluentIterable<Account.Id> ids(
Iterable<TestAccount> accounts) {
return FluentIterable.from(accounts)
.transform(new Function<TestAccount, Account.Id>() {
@Override
public Account.Id apply(TestAccount in) {
return in.id;
}
});
return FluentIterable.from(accounts).transform(a -> a.id);
}
public static FluentIterable<Account.Id> ids(TestAccount... accounts) {
@@ -43,13 +36,7 @@ public class TestAccount {
}
public static FluentIterable<String> names(Iterable<TestAccount> accounts) {
return FluentIterable.from(accounts)
.transform(new Function<TestAccount, String>() {
@Override
public String apply(TestAccount in) {
return in.fullName;
}
});
return FluentIterable.from(accounts).transform(a -> a.fullName);
}
public static FluentIterable<String> names(TestAccount... accounts) {

View File

@@ -14,9 +14,8 @@
package com.google.gerrit.pgm;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gerrit.common.IoUtil;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.common.PluginData;
@@ -42,6 +41,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/** Initialize a new Gerrit installation. */
public class Init extends BaseInit {
@@ -237,16 +237,10 @@ public class Init extends BaseInit {
if (nullOrEmpty(installPlugins) || nullOrEmpty(plugins)) {
return;
}
ArrayList<String> copy = Lists.newArrayList(installPlugins);
List<String> pluginNames = Lists.transform(plugins, new Function<PluginData, String>() {
@Override
public String apply(PluginData input) {
return input.name;
}
});
copy.removeAll(pluginNames);
if (!copy.isEmpty()) {
ui.message("Cannot find plugin(s): %s\n", Joiner.on(", ").join(copy));
Set<String> missing = Sets.newHashSet(installPlugins);
plugins.stream().forEach(p -> missing.remove(p.name));
if (!missing.isEmpty()) {
ui.message("Cannot find plugin(s): %s\n", Joiner.on(", ").join(missing));
listPlugins = true;
}
}

View File

@@ -17,7 +17,6 @@ package com.google.gerrit.pgm;
import static com.google.gerrit.reviewdb.server.ReviewDbUtil.unwrapDb;
import static com.google.gerrit.server.schema.DataSourceProvider.Context.MULTI_USER;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ArrayListMultimap;
@@ -234,13 +233,8 @@ public class RebuildNoteDb extends SiteProgram {
ArrayListMultimap.create();
try (ReviewDb db = schemaFactory.open()) {
if (projects.isEmpty() && !changes.isEmpty()) {
Iterable<Change> todo = unwrapDb(db).changes().get(
Iterables.transform(changes, new Function<Integer, Change.Id>() {
@Override
public Change.Id apply(Integer in) {
return new Change.Id(in);
}
}));
Iterable<Change> todo = unwrapDb(db).changes()
.get(Iterables.transform(changes, Change.Id::new));
for (Change c : todo) {
changesByProject.put(c.getProject(), c.getId());
}

View File

@@ -16,10 +16,8 @@ package com.google.gerrit.pgm;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gerrit.server.schema.DataSourceProvider.Context.MULTI_USER;
import static java.util.stream.Collectors.toSet;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.gerrit.common.Die;
import com.google.gerrit.extensions.config.FactoryModule;
@@ -134,14 +132,8 @@ public class Reindex extends SiteProgram {
}
checkNotNull(indexDefs, "Called this method before injectMembers?");
Set<String> valid = FluentIterable.from(indexDefs).transform(
new Function<IndexDefinition<?, ?, ?>, String>() {
@Override
public String apply(IndexDefinition<?, ?, ?> input) {
return input.getName();
}
}).toSortedSet(Ordering.natural());
Set<String> valid = indexDefs.stream()
.map(IndexDefinition::getName).sorted().collect(toSet());
Set<String> invalid = Sets.difference(Sets.newHashSet(indices), valid);
if (invalid.isEmpty()) {
return;

View File

@@ -14,7 +14,8 @@
package com.google.gerrit.server;
import com.google.common.base.Function;
import static java.util.Comparator.comparingInt;
import com.google.common.collect.Ordering;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.server.ReviewDb;
@@ -40,16 +41,8 @@ public class ChangeUtil {
private static final String SUBJECT_CROP_APPENDIX = "...";
private static final int SUBJECT_CROP_RANGE = 10;
public static final Function<PatchSet, Integer> TO_PS_ID =
new Function<PatchSet, Integer>() {
@Override
public Integer apply(PatchSet in) {
return in.getId().get();
}
};
public static final Ordering<PatchSet> PS_ID_ORDER = Ordering.natural()
.onResultOf(TO_PS_ID);
public static final Ordering<PatchSet> PS_ID_ORDER =
Ordering.from(comparingInt(PatchSet::getPatchSetId));
/**
* Generate a new unique identifier for change message entities.

View File

@@ -15,7 +15,6 @@
package com.google.gerrit.server;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
@@ -24,16 +23,10 @@ public class OptionUtil {
private static final Splitter COMMA_OR_SPACE =
Splitter.on(CharMatcher.anyOf(", ")).omitEmptyStrings().trimResults();
private static final Function<String, String> TO_LOWER_CASE =
new Function<String, String>() {
@Override
public String apply(String input) {
return input.toLowerCase();
}
};
public static Iterable<String> splitOptionValue(String value) {
return Iterables.transform(COMMA_OR_SPACE.split(value), TO_LOWER_CASE);
return Iterables.transform(
COMMA_OR_SPACE.split(value),
String::toLowerCase);
}
private OptionUtil() {

View File

@@ -14,14 +14,14 @@
package com.google.gerrit.server;
import com.google.common.base.Function;
import static java.util.Comparator.comparing;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.extensions.common.AccountInfo;
@@ -68,10 +68,8 @@ import java.util.Set;
public class ReviewersUtil {
private static final String MAX_SUFFIX = "\u9fa5";
private static final Ordering<SuggestedReviewerInfo> ORDERING =
Ordering.natural().onResultOf(new Function<SuggestedReviewerInfo, String>() {
@Nullable
@Override
public String apply(@Nullable SuggestedReviewerInfo suggestedReviewerInfo) {
Ordering.from(comparing(
suggestedReviewerInfo -> {
if (suggestedReviewerInfo == null) {
return null;
}
@@ -79,8 +77,8 @@ public class ReviewersUtil {
? MoreObjects.firstNonNull(suggestedReviewerInfo.account.email,
Strings.nullToEmpty(suggestedReviewerInfo.account.name))
: Strings.nullToEmpty(suggestedReviewerInfo.group.name);
}
});
}));
private final AccountLoader accountLoader;
private final AccountCache accountCache;
private final AccountIndexCollection indexes;

View File

@@ -14,8 +14,9 @@
package com.google.gerrit.server.change;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.gerrit.server.config.DownloadConfig;
import com.google.inject.Inject;
@@ -28,7 +29,7 @@ import java.util.Set;
@Singleton
public class AllowedFormats {
final ImmutableMap<String, ArchiveFormat> extensions;
final Set<ArchiveFormat> allowed;
final ImmutableSet<ArchiveFormat> allowed;
@Inject
AllowedFormats(DownloadConfig cfg) {
@@ -43,14 +44,8 @@ public class AllowedFormats {
// Zip is not supported because it may be interpreted by a Java plugin as a
// valid JAR file, whose code would have access to cookies on the domain.
allowed = Sets.filter(
cfg.getArchiveFormats(),
new Predicate<ArchiveFormat>() {
@Override
public boolean apply(ArchiveFormat format) {
return (format != ArchiveFormat.ZIP);
}
});
allowed = Sets.immutableEnumSet(
Iterables.filter(cfg.getArchiveFormats(), f -> f != ArchiveFormat.ZIP));
}
public Set<ArchiveFormat> getAllowed() {

View File

@@ -18,9 +18,9 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.gerrit.reviewdb.client.Change.INITIAL_PATCH_SET_ID;
import static java.util.stream.Collectors.toSet;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicate;
import com.google.common.collect.Sets;
import com.google.gerrit.common.FooterConstants;
import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.common.data.LabelTypes;
@@ -377,21 +377,23 @@ public class ChangeInserter extends BatchUpdate.InsertChangeOp {
private Set<Account.Id> filterOnChangeVisibility(final ReviewDb db,
final ChangeNotes notes, Set<Account.Id> accounts) {
return Sets.filter(accounts, new Predicate<Account.Id>() {
@Override
public boolean apply(Account.Id accountId) {
return accounts.stream()
.filter(
accountId -> {
try {
IdentifiedUser user = userFactory.create(accountId);
return changeControlFactory.controlFor(notes, user).isVisible(db);
return changeControlFactory.controlFor(notes, user)
.isVisible(db);
} catch (OrmException | NoSuchChangeException e) {
log.warn(
String.format("Failed to check if account %d can see change %d",
String.format(
"Failed to check if account %d can see change %d",
accountId.get(), notes.getChangeId().get()),
e);
return false;
}
}
});
})
.collect(toSet());
}
@Override

View File

@@ -35,8 +35,9 @@ import static com.google.gerrit.extensions.client.ListChangesOption.REVIEWER_UPD
import static com.google.gerrit.extensions.client.ListChangesOption.WEB_LINKS;
import static com.google.gerrit.server.CommonConverters.toGitPerson;
import static java.util.stream.Collectors.toList;
import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
@@ -298,13 +299,8 @@ public class ChangeJson {
public List<List<ChangeInfo>> formatQueryResults(
List<QueryResult<ChangeData>> in) throws OrmException {
accountLoader = accountLoaderFactory.create(has(DETAILED_ACCOUNTS));
ensureLoaded(FluentIterable.from(in).transformAndConcat(
new Function<QueryResult<ChangeData>, List<ChangeData>>() {
@Override
public List<ChangeData> apply(QueryResult<ChangeData> in) {
return in.entities();
}
}));
ensureLoaded(
FluentIterable.from(in).transformAndConcat(QueryResult::entities));
List<List<ChangeInfo>> res = Lists.newArrayListWithCapacity(in.size());
Map<Change.Id, ChangeInfo> out = new HashMap<>();
@@ -599,7 +595,7 @@ public class ChangeJson {
? labelsForOpenChange(ctl, cd, labelTypes, standard, detailed)
: labelsForClosedChange(cd, labelTypes, standard, detailed);
return ImmutableMap.copyOf(
Maps.transformValues(withStatus, LabelWithStatus.TO_LABEL_INFO));
Maps.transformValues(withStatus, LabelWithStatus::label));
}
private Map<String, LabelWithStatus> labelsForOpenChange(ChangeControl ctl,
@@ -952,14 +948,10 @@ public class ChangeJson {
private Collection<AccountInfo> toAccountInfo(
Collection<Account.Id> accounts) {
return FluentIterable.from(accounts)
.transform(new Function<Account.Id, AccountInfo>() {
@Override
public AccountInfo apply(Account.Id id) {
return accountLoader.get(id);
}
})
.toSortedList(AccountInfoComparator.ORDER_NULLS_FIRST);
return accounts.stream()
.map(accountLoader::get)
.sorted(AccountInfoComparator.ORDER_NULLS_FIRST)
.collect(toList());
}
@Nullable
@@ -1185,14 +1177,6 @@ public class ChangeJson {
@AutoValue
abstract static class LabelWithStatus {
private static final Function<LabelWithStatus, LabelInfo> TO_LABEL_INFO =
new Function<LabelWithStatus, LabelInfo>() {
@Override
public LabelInfo apply(LabelWithStatus in) {
return in.label();
}
};
private static LabelWithStatus create(LabelInfo label,
SubmitRecord.Label.Status status) {
return new AutoValue_ChangeJson_LabelWithStatus(label, status);

View File

@@ -16,7 +16,6 @@ package com.google.gerrit.server.change;
import static com.google.gerrit.server.PatchLineCommentsUtil.COMMENT_INFO_ORDER;
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.gerrit.extensions.client.Comment.Range;
@@ -100,17 +99,13 @@ class CommentJson {
List<CommentInfo> formatAsList(Iterable<PatchLineComment> l)
throws OrmException {
final AccountLoader accountLoader = fillAccounts
AccountLoader accountLoader = fillAccounts
? accountLoaderFactory.create(true)
: null;
List<CommentInfo> out = FluentIterable
.from(l)
.transform(new Function<PatchLineComment, CommentInfo>() {
@Override
public CommentInfo apply(PatchLineComment c) {
return toCommentInfo(c, accountLoader);
}
}).toSortedList(COMMENT_INFO_ORDER);
.transform(c -> toCommentInfo(c, accountLoader))
.toSortedList(COMMENT_INFO_ORDER);
if (accountLoader != null) {
accountLoader.fill();

View File

@@ -19,13 +19,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gerrit.reviewdb.client.RefNames.REFS_CHANGES;
import static com.google.gerrit.reviewdb.server.ReviewDbUtil.intKeyOrdering;
import static com.google.gerrit.server.ChangeUtil.PS_ID_ORDER;
import static com.google.gerrit.server.ChangeUtil.TO_PS_ID;
import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.gerrit.common.FooterConstants;
@@ -256,12 +253,9 @@ public class ConsistencyChecker {
Map<String, Ref> refs;
try {
refs = repo.getRefDatabase().exactRef(
Lists.transform(all, new Function<PatchSet, String>() {
@Override
public String apply(PatchSet ps) {
return ps.getId().toRefName();
}
}).toArray(new String[all.size()]));
all.stream()
.map(ps -> ps.getId().toRefName())
.toArray(String[]::new));
} catch (IOException e) {
error("error reading refs", e);
refs = Collections.emptyMap();
@@ -319,7 +313,7 @@ public class ConsistencyChecker {
if (e.getValue().size() > 1) {
problem(String.format("Multiple patch sets pointing to %s: %s",
e.getKey().name(),
Collections2.transform(e.getValue(), TO_PS_ID)));
Collections2.transform(e.getValue(), PatchSet::getPatchSetId)));
}
}

View File

@@ -14,7 +14,6 @@
package com.google.gerrit.server.change;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.gerrit.common.TimeUtil;
@@ -210,7 +209,7 @@ public class DeleteReviewer
}
private Iterable<PatchSetApproval> approvals(ChangeContext ctx,
final Account.Id accountId) throws OrmException {
Account.Id accountId) throws OrmException {
Change.Id changeId = ctx.getNotes().getChangeId();
Iterable<PatchSetApproval> approvals;
@@ -229,14 +228,7 @@ public class DeleteReviewer
approvalsUtil.byChange(ctx.getDb(), ctx.getNotes()).values();
}
return Iterables.filter(
approvals,
new Predicate<PatchSetApproval>() {
@Override
public boolean apply(PatchSetApproval input) {
return accountId.equals(input.getAccountId());
}
});
return Iterables.filter(approvals, accountId::equals);
}
private String formatLabelValue(short value) {

View File

@@ -17,7 +17,6 @@ package com.google.gerrit.server.change;
import static com.google.gerrit.extensions.client.ReviewerState.CC;
import static com.google.gerrit.extensions.client.ReviewerState.REVIEWER;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
@@ -357,13 +356,8 @@ public class PostReviewers
}
emailReviewers(rsrc.getChange(), addedReviewers, addedCCs);
if (!addedReviewers.isEmpty()) {
List<Account.Id> reviewers = Lists.transform(addedReviewers,
new Function<PatchSetApproval, Account.Id>() {
@Override
public Account.Id apply(PatchSetApproval psa) {
return psa.getAccountId();
}
});
List<Account.Id> reviewers =
Lists.transform(addedReviewers, PatchSetApproval::getAccountId);
reviewerAdded.fire(rsrc.getChange(), patchSet, reviewers,
ctx.getAccount(), ctx.getWhen());
}

View File

@@ -14,15 +14,13 @@
package com.google.gerrit.server.change;
import static java.util.stream.Collectors.joining;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.gerrit.common.data.ParameterizedString;
@@ -282,14 +280,10 @@ public class Submit implements RestModifyView<RevisionResource, SubmitInput>,
return CHANGE_UNMERGEABLE;
}
}
return CHANGES_NOT_MERGEABLE + Joiner.on(", ").join(
Iterables.transform(unmergeable,
new Function<ChangeData, String>() {
@Override
public String apply(ChangeData cd) {
return String.valueOf(cd.getId().get());
}
}));
return CHANGES_NOT_MERGEABLE +
unmergeable.stream()
.map(c -> c.getId().toString())
.collect(joining(", "));
}
} catch (ResourceConflictException e) {
return BLOCKED_SUBMIT_TOOLTIP;
@@ -405,14 +399,10 @@ public class Submit implements RestModifyView<RevisionResource, SubmitInput>,
*/
public ChangeMessage getConflictMessage(RevisionResource rsrc)
throws OrmException {
return FluentIterable.from(cmUtil.byPatchSet(dbProvider.get(), rsrc.getNotes(),
rsrc.getPatchSet().getId()))
.filter(new Predicate<ChangeMessage>() {
@Override
public boolean apply(ChangeMessage input) {
return input.getAuthor() == null;
}
})
return FluentIterable.from(
cmUtil.byPatchSet(
dbProvider.get(), rsrc.getNotes(), rsrc.getPatchSet().getId()))
.filter(cm -> cm.getAuthor() == null)
.last()
.orNull();
}

View File

@@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkState;
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
@@ -72,11 +71,8 @@ class WalkSorter {
LoggerFactory.getLogger(WalkSorter.class);
private static final Ordering<List<PatchSetData>> PROJECT_LIST_SORTER =
Ordering.natural().nullsFirst()
.onResultOf(
new Function<List<PatchSetData>, Project.NameKey>() {
@Override
public Project.NameKey apply(List<PatchSetData> in) {
Ordering.natural().nullsFirst().onResultOf(
(List<PatchSetData> in) -> {
if (in == null || in.isEmpty()) {
return null;
}
@@ -85,7 +81,6 @@ class WalkSorter {
} catch (OrmException e) {
throw new IllegalStateException(e);
}
}
});
private final GitRepositoryManager repoManager;