From: Julia Beliaeva Date: Sat, 8 Oct 2016 19:17:41 +0000 (+0300) Subject: [git] minor: use lambdas X-Git-Tag: appcode/171.12~13 X-Git-Url: https://git.jetbrains.org/?p=idea%2Fcommunity.git;a=commitdiff_plain;h=d91e92fd65fc88a8702e25702685c7360964c531;hp=1dd2f91f1b442fc10a1f6b149f69d845e74d8cfa [git] minor: use lambdas --- diff --git a/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java b/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java index 410148fc7602..f14c32593451 100644 --- a/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java +++ b/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java @@ -34,7 +34,10 @@ import com.intellij.openapi.vcs.history.VcsRevisionDescription; import com.intellij.openapi.vcs.history.VcsRevisionDescriptionImpl; import com.intellij.openapi.vcs.history.VcsRevisionNumber; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.util.*; +import com.intellij.util.ArrayUtil; +import com.intellij.util.Consumer; +import com.intellij.util.NullableFunction; +import com.intellij.util.SmartList; import com.intellij.util.concurrency.Semaphore; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.OpenTHashSet; @@ -247,56 +250,54 @@ public class GitHistoryUtils { final AtomicReference logHandler = new AtomicReference<>(); final AtomicBoolean skipFurtherOutput = new AtomicBoolean(); - final Consumer resultAdapter = new Consumer() { - public void consume(GitLogRecord record) { - if (skipFurtherOutput.get()) { - return; - } - if (record == null) { - exceptionConsumer.consume(new VcsException("revision details are null.")); - return; - } - record.setUsedHandler(logHandler.get()); - final GitRevisionNumber revision = new GitRevisionNumber(record.getHash(), record.getDate()); - firstCommit.set(record.getHash()); - final String[] parentHashes = record.getParentsHashes(); - if (parentHashes.length < 1) { - firstCommitParent.set(null); + final Consumer resultAdapter = record -> { + if (skipFurtherOutput.get()) { + return; + } + if (record == null) { + exceptionConsumer.consume(new VcsException("revision details are null.")); + return; + } + record.setUsedHandler(logHandler.get()); + final GitRevisionNumber revision = new GitRevisionNumber(record.getHash(), record.getDate()); + firstCommit.set(record.getHash()); + final String[] parentHashes = record.getParentsHashes(); + if (parentHashes.length < 1) { + firstCommitParent.set(null); + } + else { + firstCommitParent.set(parentHashes[0]); + } + final String message = record.getFullMessage(); + + FilePath revisionPath; + try { + final List paths = record.getFilePaths(finalRoot); + if (paths.size() > 0) { + revisionPath = paths.get(0); } else { - firstCommitParent.set(parentHashes[0]); + // no paths are shown for merge commits, so we're using the saved path we're inspecting now + revisionPath = currentPath.get(); } - final String message = record.getFullMessage(); - FilePath revisionPath; - try { - final List paths = record.getFilePaths(finalRoot); - if (paths.size() > 0) { - revisionPath = paths.get(0); - } - else { - // no paths are shown for merge commits, so we're using the saved path we're inspecting now - revisionPath = currentPath.get(); - } - - Couple authorPair = Couple.of(record.getAuthorName(), record.getAuthorEmail()); - Couple committerPair = Couple.of(record.getCommitterName(), record.getCommitterEmail()); - Collection parents = Arrays.asList(parentHashes); - consumer.consume(new GitFileRevision(project, finalRoot, revisionPath, revision, Couple.of(authorPair, committerPair), message, - null, new Date(record.getAuthorTimeStamp()), parents)); - List statusInfos = record.getStatusInfos(); - if (statusInfos.isEmpty()) { - // can safely be empty, for example, for simple merge commits that don't change anything. - return; - } - if (statusInfos.get(0).getType() == GitChangeType.ADDED && !filePath.isDirectory()) { - skipFurtherOutput.set(true); - } + Couple authorPair = Couple.of(record.getAuthorName(), record.getAuthorEmail()); + Couple committerPair = Couple.of(record.getCommitterName(), record.getCommitterEmail()); + Collection parents = Arrays.asList(parentHashes); + consumer.consume(new GitFileRevision(project, finalRoot, revisionPath, revision, Couple.of(authorPair, committerPair), message, + null, new Date(record.getAuthorTimeStamp()), parents)); + List statusInfos = record.getStatusInfos(); + if (statusInfos.isEmpty()) { + // can safely be empty, for example, for simple merge commits that don't change anything. + return; } - catch (VcsException e) { - exceptionConsumer.consume(e); + if (statusInfos.get(0).getType() == GitChangeType.ADDED && !filePath.isDirectory()) { + skipFurtherOutput.set(true); } } + catch (VcsException e) { + exceptionConsumer.consume(e); + } }; GitVcs vcs = GitVcs.getInstance(project); @@ -456,18 +457,15 @@ public class GitHistoryUtils { String output = h.run(); List records = parser.parse(output); - return ContainerUtil.map(records, new Function() { - @Override - public VcsShortCommitDetails fun(GitLogRecord record) { - List parents = new SmartList<>(); - for (String parent : record.getParentsHashes()) { - parents.add(HashImpl.build(parent)); - } - return factory.createShortDetails(HashImpl.build(record.getHash()), parents, record.getCommitTime(), root, - record.getSubject(), record.getAuthorName(), record.getAuthorEmail(), record.getCommitterName(), - record.getCommitterEmail(), - record.getAuthorTimeStamp()); + return ContainerUtil.map(records, record -> { + List parents = new SmartList<>(); + for (String parent : record.getParentsHashes()) { + parents.add(HashImpl.build(parent)); } + return factory.createShortDetails(HashImpl.build(record.getHash()), parents, record.getCommitTime(), root, + record.getSubject(), record.getAuthorName(), record.getAuthorEmail(), record.getCommitterName(), + record.getCommitterEmail(), + record.getAuthorTimeStamp()); }); } @@ -496,15 +494,14 @@ public class GitHistoryUtils { List records = parser.parse(output); if (records.size() != refs.length) return null; - return ContainerUtil.map(records, new Function() { - @Override - public VcsCommitMetadata fun(GitLogRecord record) { - return factory.createCommitMetadata(factory.createHash(record.getHash()), getParentHashes(factory, record), record.getCommitTime(), - root, record.getSubject(), record.getAuthorName(), record.getAuthorEmail(), - record.getFullMessage(), record.getCommitterName(), record.getCommitterEmail(), - record.getAuthorTimeStamp()); - } - }); + return ContainerUtil.map(records, + record -> factory.createCommitMetadata(factory.createHash(record.getHash()), getParentHashes(factory, record), + record.getCommitTime(), + root, record.getSubject(), record.getAuthorName(), + record.getAuthorEmail(), + record.getFullMessage(), record.getCommitterName(), + record.getCommitterEmail(), + record.getAuthorTimeStamp())); } private static void processHandlerOutputByLine(@NotNull GitLineHandler handler, @@ -640,21 +637,18 @@ public class GitHistoryUtils { @NotNull Consumer refConsumer, @NotNull VcsLogObjectsFactory factory, @NotNull VirtualFile root) { - List rec = parser.parse(record.toString()); - return ContainerUtil.mapNotNull(rec, new Function() { - @Override - public TimedVcsCommit fun(GitLogRecord record) { - if (record == null) { - return null; - } - Pair> pair = convert(record, factory, root); - TimedVcsCommit commit = pair.first; - for (VcsRef ref : pair.second) { - refConsumer.consume(ref); - } - userRegistry.consume(factory.createUser(record.getAuthorName(), record.getAuthorEmail())); - return commit; + List gitLogRecords = parser.parse(record.toString()); + return ContainerUtil.mapNotNull(gitLogRecords, gitLogRecord -> { + if (gitLogRecord == null) { + return null; + } + Pair> pair = convert(gitLogRecord, factory, root); + TimedVcsCommit commit = pair.first; + for (VcsRef ref : pair.second) { + refConsumer.consume(ref); } + userRegistry.consume(factory.createUser(gitLogRecord.getAuthorName(), gitLogRecord.getAuthorEmail())); + return commit; }); } @@ -673,26 +667,20 @@ public class GitHistoryUtils { @NotNull Hash hash, @NotNull VcsLogObjectsFactory factory, @NotNull VirtualFile root) { - return ContainerUtil.mapNotNull(refs, new Function() { - @Override - public VcsRef fun(String refName) { - VcsRefType type = GitRefManager.getRefType(refName); - refName = GitBranchUtil.stripRefsPrefix(refName); - return refName.equals(GitUtil.ORIGIN_HEAD) ? null : factory.createRef(hash, refName, type, root); - } + return ContainerUtil.mapNotNull(refs, refName -> { + VcsRefType type = GitRefManager.getRefType(refName); + refName = GitBranchUtil.stripRefsPrefix(refName); + return refName.equals(GitUtil.ORIGIN_HEAD) ? null : factory.createRef(hash, refName, type, root); }); } @Nullable private static VcsLogObjectsFactory getObjectsFactoryWithDisposeCheck(@NotNull Project project) { - return ApplicationManager.getApplication().runReadAction(new Computable() { - @Override - public VcsLogObjectsFactory compute() { - if (!project.isDisposed()) { - return ServiceManager.getService(project, VcsLogObjectsFactory.class); - } - return null; + return ApplicationManager.getApplication().runReadAction((Computable)() -> { + if (!project.isDisposed()) { + return ServiceManager.getService(project, VcsLogObjectsFactory.class); } + return null; }); } @@ -778,17 +766,7 @@ public class GitHistoryUtils { final List rc = new ArrayList<>(); final List exceptions = new ArrayList<>(); - history(project, path, root, startingFrom, new Consumer() { - @Override - public void consume(GitFileRevision gitFileRevision) { - rc.add(gitFileRevision); - } - }, new Consumer() { - @Override - public void consume(VcsException e) { - exceptions.add(e); - } - }, parameters); + history(project, path, root, startingFrom, gitFileRevision -> rc.add(gitFileRevision), e -> exceptions.add(e), parameters); if (!exceptions.isEmpty()) { throw exceptions.get(0); } @@ -847,21 +825,17 @@ public class GitHistoryUtils { } final Set refs = new OpenTHashSet<>(GitLogProvider.DONT_CONSIDER_SHA); final List commits = - loadDetails(project, root, withRefs, false, new NullableFunction() { - @Nullable - @Override - public VcsCommitMetadata fun(GitLogRecord record) { - GitCommit commit = createCommit(project, root, record, factory); - if (withRefs) { - Collection refsInRecord = parseRefs(record.getRefs(), commit.getId(), factory, root); - for (VcsRef ref : refsInRecord) { - if (!refs.add(ref)) { - LOG.error("Adding duplicate element to the set"); - } + loadDetails(project, root, withRefs, false, record -> { + GitCommit commit = createCommit(project, root, record, factory); + if (withRefs) { + Collection refsInRecord = parseRefs(record.getRefs(), commit.getId(), factory, root); + for (VcsRef ref : refsInRecord) { + if (!refs.add(ref)) { + LOG.error("Adding duplicate element to the set"); } } - return commit; } + return commit; }, params); return new LogDataImpl(refs, commits); } @@ -879,13 +853,7 @@ public class GitHistoryUtils { if (factory == null) { return Collections.emptyList(); } - return loadDetails(project, root, false, true, new NullableFunction() { - @Override - @Nullable - public GitCommit fun(GitLogRecord record) { - return createCommit(project, root, record, factory); - } - }, parameters); + return loadDetails(project, root, false, true, record -> createCommit(project, root, record, factory), parameters); } @NotNull @@ -954,12 +922,7 @@ public class GitHistoryUtils { @NotNull private static List getParentHashes(@NotNull VcsLogObjectsFactory factory, @NotNull GitLogRecord record) { - return ContainerUtil.map(record.getParentsHashes(), new Function() { - @Override - public Hash fun(String hash) { - return factory.createHash(hash); - } - }); + return ContainerUtil.map(record.getParentsHashes(), hash -> factory.createHash(hash)); } @NotNull