Home > database >  Code Smell : Make this anonymous inner class a lambda
Code Smell : Make this anonymous inner class a lambda

Time:05-29

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

enter image description here 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?

CodePudding user response:

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

    };
  • Related