You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Chen Guoping1 <ch...@163.com> on 2020/05/18 07:55:41 UTC

[CSV] Refactor the withXXX methods in CSVFomat by adding a new constructor

 hi, all
   I found that all the withXXX methods in CSVFomat class need to call the constructor to generate a new CSVFomat object.
Once a new format attribute is added, you need to change the call of nearly 30 constructors.May be we can add
a map to record all the attributes of CSVFormat class and a constructor, the input parameter is map, and the withXXX methods
call the constructor using the map as input parameter. You only need to modify the attributes of the map that need to be modified.
Then when we add a new attribute to CSVFomat, we do not need to modify the withXXX methods in so many places.


The following is the demo:


private CSVFomat(HashMap<String, Object> formatAttrMap) {
    this.delimiter = formatAttrMap.get("delimiter ").toString().charAt(0);
    //todo add other attributes
    this. formatAttrMap = (HashMap<String, Object>) formatAttrMap.clone();
}
private HashMap<String, Object> getFormatAttrMap() {
    return this. formatAttrMap ;
}
public CSVFormat withDelimiter(final char delimiter) {
    if (isLineBreak(delimiter)) {
        throw new IllegalArgumentException("The delimiter cannot be a line break");
    }
    HashMap<String, Object> formatAttrMap = getFormatAttrMap();
    formatAttrMap.put("delimiter", delimiter);
    return new CSVFormat(formatAttrMap);
}