简体   繁体   中英

How can i reduce this repeat code in Java 8?

I have the following code that keeps repeating a lot. Maybe there is a way to reduce it but i dont know too much of java. It looks like this:

public Optional<byte[]> generateReportA(List<ClassThatChange> request){
        if(CollectionUtils.isEmpty(request)) {
           return Optional.empty();
        }
        
        List<ClassThatNOChange> list = new ArrayList<>();

        
        for(ClassThatChange item : request){
            ClassThatNOChange c = new ClassThatNOChange()
            
            //here is the line of code that changes from report to report
            c.setResult(callMethodA(item.getVariable1(), item.getVariable2()));

            list.add(c);
        }
        
        return Optional.of(callSameMethod(list));
}

public Optional<byte[]> generateReportB(List<ClassThatChange> request){
        if(CollectionUtils.isEmpty(request)) {
           return Optional.empty();
        }
        
        List<ClassThatNOChange> list = new ArrayList<>();

        
        for(ClassThatChange item : request){
            ClassThatNOChange c = new ClassThatNOChange()
            
            //here is the line of code that changes from report to report
            c.setResult(callMethodB(item.getVariable2()));

            list.add(c);
        }
        
        return Optional.of(callSameMethod(list));
}

The only thing that change is the callMethodA, or B, or C... but all of them return the same type of answer.

The ClassThatChange is something like this:

public class Father{
      private Date date;
      // then the getters and setters
}

public class Son extends Father{
      private String description;
      // then the getters and setters
}

All the classes that can change extends from the father.

Is there a way to reduce this repeat code? Sorry for my bad English.

I think what you are looking for is a way to plug in a conversion from the type that changes in each case to the type that is always the same. That could be done with a Function , or with a customized functional interface that you define. Here, I'm showing Function .

private <T> Optional<byte[]> convert(
    List<? extends T> request, 
    Function<? super T, ? extends SomeTypeYouDidNotShow> conversion) {

    if (request.isEmpty()) return Optional.empty();
    List<ClassThatNOChange> list = request.stream()
        .map(item -> {
                ClassThatNOChange c = new ClassThatNOChange();
                c.setResult(conversion.apply(item));
                return c;
            })
        .collect(Collectors.toList());
    return Optional.of(callSameMethod(list));
}

Then all your duplicate methods would look something like this:

public Optional<byte[]> generateReportA(List<Father> request) {
    return convert(request, 
        item -> callMethodA(item.getVariable1(), item.getVariable2()));
}

public Optional<byte[]> generateReportB(List<Son> request) {
    return convert(request, 
        item -> callMethodB(item.getVariable2()));
}

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