Refactoring for a loop with each iteration sets a different property.

I was shown the following Java code as part of a code review, and this is frankly disgusting. However, I do not understand how best to rewrite it.

We have a list that is always in the same order (0-5), and each index corresponds to a different property that needs to be set. Using groovy, that would be easy, since that would be myObject. SetLine $ {i} "or similar, but how can we do the same in plain Java?

    SomeObject myObject = new SomeObject();
    List<String> lines = new ArrayList<>(){{
    add("Line 1");
    add("Line 2");
    add("Line 3");
    add("Line 4");
    add("Line 5");
    add("Line 6");
}};

for(int i=0; i< lines.size(); i++){
    if(!StringUtils.isBlank(lines.get(i))){
        String line = lines.get(i);
        // line1
        if(i==0){
            myObject.setLine1(line);
        }
        // line2
        if(i==1){
            myObject.setLine2(line);
        }
        // line3
        if(i==2){
            myObject.setLine3(line);
        }
        // line4
        if(i==3){
            myObject.setLine4(line);
        }
        // line5
        if(i==4){
            myObject.setLine5(line);
        }
        //line6
        if(i==5){
            myObject.setLine6(line);
        }
    }
}

, : , "" , . - , MyObject , , (.. / "setLineX", ).

+4
2

myObject. , , . - ?

class MyObject{
    private Map<Integer,String> lines = new HashMap<>();

    public void setLine(int lineNumber, String line){
        lines.put(lineNumber,line);
    }

    public String getLine(int lineNumber){
        lines.get(lineNumber);
    }

}

for(int i=0;i<lineListSize;i++){
    myOjbect.setLine(i,lineList.get(i));
}

EDIT: , List.

class MyObject{
    private List<String> lines = new ArrayList<>();

    public void setLine(int lineNumber, String line){
        lines.add(lineNumber,line);
    }

    public String getLine(int lineNumber){
        lines.get(lineNumber);
    }

}

EDIT2: .

class MyObject{
    ... 
    // Collection, getter and setter omitted
    ...
    public void addAllLines(List<String> lines){
       // implementation varies depending on collection,
       // but List would simply be "this.lines.addAll(lines);" 
    }

    public List<String> getAllLines(){
       // Again, depends on the type of Collection used by 
       // this instance.
       // Probably want to return a copy if thread safety is an issue.
    }
}
+7

( , Java 8) myObject String. , .

private static List<Consumer<Tuple<SomeObject, String>> consumers = Arrays.asList(
    new Consumer<>() {
        public void consume(Tuple<SomeObject, String> tuple) {
            tuple._1.setLine1(tuple._2);
        }
    },
    new Consumer<>() {
        public void consume(Tuple<SomeObject, String> tuple) {
            tuple._1.setLine2(tuple._2);
        }
    },
    ... and so on
);

final int numConsumers = consumers.size();
for(int i=0; i < numConsumers && i < lines.size(); i++){
    String line = lines.get(i);
    if(!StringUtils.isBlank(line)){
        consumers.get(i).consume(new Tuple<>(myObject, line));
    }
}

, Tuple -

public class Tuple<A, B> {
    public final A _1;
    public final B _2;

    public Tuple(A a, B a) {
        this._1 = a;
        this._2 = b;
    }
}

Java 8, :

private static final List<BiConsumer<MyObject, String>> CONSUMERS = Arrays.asList(
        (o, s) -> o.setLine1(s),
        (o, s) -> o.setLine2(s),
        (o, s) -> o.setLine3(s)

);

Tuple:

consumers.get(i).consume(myObject, line);

:

public class ExampleClass {

    private static final List<BiConsumer<MyObject, String>> CONSUMERS = Arrays.asList(
        (o, s) -> o.setLine1(s),
        (o, s) -> o.setLine2(s),
        (o, s) -> o.setLine3(s)
        // and so on
    );

    public void exampleMethod() {
        SomeObject myObject = new SomeObject();
        List<String> lines = Arrays.asList("Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6");

        for(int i = 0, numConsumers = consumers.size(), numLines = lines.size(); i < numConsumers && i < numLines; i++){
            String line = lines.get(i);
            if(!StringUtils.isBlank(line)){
                consumers.get(i).consume(myObject, line);
            }
        }
    }
}
+1

Source: https://habr.com/ru/post/1589199/


All Articles