代码味道:将这个匿名内部 class 设为 lambda

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);
        }
    };
}

我有上面的功能,但我有一种代码味道,上面写着 " Make this anonymous inner class a lambda " .

现在我不确定如何将其转换为 lambda 函数,因为在 valuemap 中放置了值。 如何将上述函数转换为lambda函数?

这就是你需要的,不需要创建一些对象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);

    };