简体   繁体   中英

Code Smell : Make this anonymous inner class a lambda

public Transformer getTransformed(Locale locale, SlingHttpServletRequest request) {
    return new Transformer() {
        public Object transform(Object o) {
            Tag tag = (Tag) o;

            String tagId = tag.getTagID();
            ValueMap vm = new ValueMapDecorator(new HashMap<>());
            vm.put("value", tagId);
            vm.put("text", tag.getTitlePath(locale));
            return new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), "nt:unstructured", vm);
        }
    };
}

在此处输入图像描述 I have the above function and I got a code smell that says " Make this anonymous inner class a lambda " .

Now I am not sure how to convert this to a lambda function because of the putting of value in valuemap. How to convert the above function into a lambda function?

This is what you need, no need to create an object of a some class:

Function<Object, Object> transform = o -> {
        Tag tag = (Tag) o;

        String tagId = tag.getTagID();
        ValueMap vm = new ValueMapDecorator(new HashMap<>());
        vm.put("value", tagId);
        vm.put("text", tag.getTitlePath(locale));
        return new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), "nt:unstructured", vm);

    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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