简体   繁体   English

如何将参数化的匿名类转换为lambda?

[英]How to turn parameterized anonymous class into lambda?

I want to refactor my code (Sonar also wants me to do that :) ). 我想重构我的代码(Sonar也希望我这样做:))。

I should use a lambda instead of an anonymous class. 我应该使用lambda而不是匿名类。

I have the following code: 我有以下代码:

@Component
public class InsuranceValidateRespDataToDtoMapper extends PropertyMapConfigurerSupport<InsuranceValidateResponseData,
        com.package.InsuranceValidateResponseData> {

    @Override
    public PropertyMap<InsuranceValidateResponseData, com.package.InsuranceValidateResponseData> mapping() {
        return new PropertyMap<InsuranceValidateResponseData, com.package.InsuranceValidateResponseData>() {
            @Override
            protected void configure() {
                using(BIG_DECIMAL_TO_INTEGER_AMOUNT_CONVERTER).map(source.getLackCurr()).setLackCurr(null);
                using(BIG_DECIMAL_TO_INTEGER_AMOUNT_CONVERTER).map(source.getLackEur()).setLackEur(null);
            }
        };
    }
}

When I make the following change, the code won't compile. 当我进行以下更改时,代码将无法编译。

@Component
public class InsuranceValidateRespDataToDtoMapper extends PropertyMapConfigurerSupport<InsuranceValidateResponseData,
        com.package.InsuranceValidateResponseData> {

    @Override
    public PropertyMap<InsuranceValidateResponseData, com.package.InsuranceValidateResponseData> mapping() {
        return () -> {
            using(BIG_DECIMAL_TO_INTEGER_AMOUNT_CONVERTER).map(source.getLackCurr()).setLackCurr(null);
            using(BIG_DECIMAL_TO_INTEGER_AMOUNT_CONVERTER).map(source.getLackEur()).setLackEur(null);
        };
    }
}

Compilation fails because it can't resolve "using" and "source" 编译失败,因为它无法解析“使用”和“源”

Here: 这里:

return new PropertyMap<InsuranceValidateResponseData, com.package.InsuranceValidateResponseData>() {
return () -> {
        using(BIG_DECIMAL_TO_INTEGER_AMOUNT_CONVERTER).map(source.getLackCurr()).setLackCurr(null);
        using(BIG_DECIMAL_TO_INTEGER_AMOUNT_CONVERTER).map(source.getLackEur()).setLackEur(null);
    };
}

There are two returns in there - doesn't make sense; 那里有两个回报-没有道理; go for that second 那一秒钟

return -> { ... }

only; 只要; and remove the other stuff that you have in front of it! 并删除前面的其他内容!

But the more important aspect here: don't do things blindly because some tool tells you so. 但是这里重要的方面是:不要盲目做事,因为某些工具可以告诉您。

Instead: step back; 相反:退后一步; understand what the tool is telling you (like: what are the options; and do you really buy into "that other option is better than what you have right now"). 了解该工具告诉您的内容(例如:有哪些选择;您是否真的认同“其他选择比您现在拥有的更好”)。 Sure, if everybody agrees on "we want 0 SonarQube warnings", then that is what you could do. 当然,如果每个人都同意“我们想要0个SonarQube警告”,那么这就是您可以做的。 On the other hand; 另一方面; when touching code; 当触摸代码时; as said: don't do it blindly. 如前所述:不要盲目地做。 Maybe you can step back; 也许你可以退后一步; and even do a better refactoring here. 甚至在这里做得更好

(I am not saying that this is possible here; I am just pointing out: when you are touching your code; make sure that the change you will commit makes really sense to you). (我并不是说这是可能的;我只是指出:当您触摸代码时,请确保所做的更改对您而言确实有意义)。

Try rewriting your method like this, if your PropertyMap is functional interface 如果您的PropertyMap是功能性接口,请尝试这样重写您的方法

@Override
public PropertyMap<InsuranceValidateResponseData, com.package.InsuranceValidateResponseData> mapping() {
    return () -> {
        using(BIG_DECIMAL_TO_INTEGER_AMOUNT_CONVERTER).map(source.getLackCurr()).setLackCurr(null);
        using(BIG_DECIMAL_TO_INTEGER_AMOUNT_CONVERTER).map(source.getLackEur()).setLackEur(null);
    };
}

Functional interfaces can be represented like lambda-functions, and in this case you only need to write a lambda-function for that single method from functional interface. 功能接口可以像lambda函数一样表示,在这种情况下,您只需要从功能接口为该单个方法编写一个lambda函数。 Simple example with BiConsumer class : BiConsumer类的简单示例:

BiConsumer<String, String> getConsumer() {
    return new BiConsumer<String, String>() {
        @Override
        public void accept(String s, String s2) {
            // accept strings
        }
    };
}

Can be represented as lambda-function with specifying only accept method: 可以通过仅指定accept方法表示为lambda函数:

BiConsumer<String, String> getConsumer() {
    return (s, s2) -> {
        // accept strings
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM