小编Mah*_*ani的帖子

Java 8 Functional VS Imperative方法

我已经创建了一个基于Bean属性动态构建rest URI的方法,最初我必须将它重构为函数式,这是我第一次进行函数式编程.命令式和功能性都按预期工作,但我对功能可读性,功能性接缝以及此方法的过度杀戮感到不满意,或者可能是因为我仍然是新手功能程序员!

您如何将此方法重构为更清晰的功能方式?

或者你会保持它势在必行吗?

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.lang.reflect.Method;

import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.util.UriComponentsBuilder;

public String functionalBuildRestUri() throws Exception {

    final UriComponentsBuilder uriBuilder = UriComponentsBuilder.newInstance().scheme("https")
            .host("foo.com").path("/offers");
    //here is the functional 
    List<PropertyDescriptor> propDescList = Arrays.asList(BeanUtils.getPropertyDescriptors(getClass()));

    //this part is readable and precis, but to enable it had to add 4 methods 
    propDescList.stream().filter(notClassProp())
                         .filter(notNullPropValue())
                         .collect(Collectors.toMap(PropertyDescriptor::getName, propValue()))//conversion to map doesn't feel good to me how can …
Run Code Online (Sandbox Code Playgroud)

refactoring functional-programming naming-conventions java-8

2
推荐指数
1
解决办法
609
查看次数