使用Collection :: add和Collection :: remove更新方法

时间:2016-03-21 11:35:08

标签: java java-8

在我的项目中,需要2个方法:一个用于添加版本到对象,另一个用于删除它。 这两种方法的逻辑几乎相同:验证条件,如果一切正常,请添加/删除版本以发布版本Collection。 我在使用Collectiion::addCollection::remove方面遇到了一些问题,这就是为什么我为它创建了2个静态方法removeVersionaddVersion的原因。如何更新此方法,因此在FixVersionServiceImpl::addVersionFixVersionServiceImpl::removeVersion中不需要FixVersionServiceImpl - 当前类名?

private boolean updateIssueVersion(User user,
                                       IssuesVersionUpdateDescription issueVersionUpdate,
                                       Version version,
                                       BiFunction<Version, Collection<Version>, Boolean> function) {
        boolean updated = false;
        for (String issueKey : issueVersionUpdate.getIssueKeys()) {
            IssueService.IssueResult issueResult = issueService.getIssue(user, issueKey);
            MutableIssue issue = issueResult.getIssue();
            if (issue == null) {
                LOG.info("Issue was not found for user.");
                continue;
            }

            if (issue.getProjectObject() == null
                || !issue.getProjectObject().getKey().equals(version.getProject().getKey())) {
                LOG.error("Issue project doesn't contain this fix version.");
                continue;
            }
            Collection<Version> fixVersions = issue.getFixVersions();
            function.apply(version, fixVersions);
            issue.setFixVersions(fixVersions);
            issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
            updated = true;
        }
        return updated;
    }

    private static boolean addVersion(Version version, Collection<Version> fixVersions) {
        return fixVersions.add(version);
    }

    private static boolean removeVersion(Version version, Collection<Version> fixVersions) {
        return fixVersions.remove(version);
    }

1 个答案:

答案 0 :(得分:2)

Collection::add::remove的类型为BiFunction<Collection<T>, T, Boolean>,其中第一种类型是指封闭集合实例。因此,将这些方法作为参数传递并将它们用作lambda,如:

function.apply(fixVersions, version);

您需要将该参数的类型更改为BiFunction<Collection<Version>, Version, Boolean>

相关问题