14

I am generating a CSV file along with CTL file for use with sqlldr. The CTL file needs to know the names of the columns that I want to load, and my CSV file needs to know the default values of these fields.

/*
 * Models a line in the CSV file
 */
public class CSVRecord {
  ...
}

/*
 * Models the CTL file
 */
public class ControlFile {
    ...
}

These 2 classes are initialised and used inside CSVExportFile and I have 2 approaches:

1. Enum

public enum Columns {
    ID("1"),
    NAME("Bob"),
    ...
}

2. HashMap

public class CSVExportFile {
    private HashMap<String, String> columns;

    public CSVExportFile() {
        columns = new HashMap<String, String>();
        columns.put("ID", "1");
        columns.put("Name", "Bob");
        ...
    }
}

The HashMap reduces the scope of the columns and will mean that they can only be used inside CSVExportFile. I do not plan to extend this functionality (all classes will be final), so I'm not sure that my enum gains me anything.

What are the arguments for/against each approach, is this a specific case where one is superior, or is one way always superior?

1
  • If it can be fixed at compile time, it might as well be fixed at compile time (i.e. an enum). Then the compiler can check you are getting your identifiers right in the rest of your code.
    – khelwood
    Jul 17, 2015 at 10:25

4 Answers 4

9

I would always use an enum here as enum have an innate ordering while Maps do not.

By using an enum you can generate the CTL file from the enum itself and use the enum values as factories to populate your csv file.

class MyObj {

    final String foreName;
    final String surname;

    public MyObj(String foreName, String surname) {
        this.foreName = foreName;
        this.surname = surname;
    }

    public String getForeName() {
        return foreName;
    }

    public String getSurname() {
        return surname;
    }

}

enum Column {

    Forename {

                @Override
                String fromMyObj(MyObj it) {
                    return it.getForeName();
                }
            },
    Surname {

                @Override
                String fromMyObj(MyObj it) {
                    return it.getSurname();
                }
            },;

    abstract String fromMyObj(MyObj it);

    static String asSelectStatement(Set<Column> columns, String tableName) {
        return join(columns, ",", "SELECT ", " FROM " + tableName);
    }

    static String asCSVHeader(Set<Column> columns) {
        return join(columns, ",");
    }

    static String asCSV(Set<Column> columns, MyObj it) {
        return join(columns, (Column a) -> a.fromMyObj(it), ",");
    }

    private static String join(Set<Column> columns, String between) {
        return join(columns, new StringJoiner(between));
    }

    private static String join(Set<Column> columns, String between, String prefix, String suffix) {
        return join(columns, new StringJoiner(between, prefix, suffix));
    }

    private static String join(Set<Column> columns, StringJoiner joined) {
        return join(columns, (Column a) -> a.name(), joined);
    }

    private static String join(Set<Column> columns, Function<Column, String> as, String between) {
        return join(columns, as, new StringJoiner(between));
    }

    private static String join(Set<Column> columns, Function<Column, String> as, String between, String prefix, String suffix) {
        return join(columns, as, new StringJoiner(between, prefix, suffix));
    }

    private static String join(Set<Column> columns, Function<Column, String> as, StringJoiner joined) {
        for (Column c : columns) {
            joined.add(as.apply(c));
        }
        return joined.toString();
    }

    // Also simple to auto-populate prepared statements, build INSERT statements etc.
}

public void test() {
    Set<Column> columns = EnumSet.of(Column.Forename, Column.Surname);
    System.out.println("As Select: " + Column.asSelectStatement(columns, "MyTable"));
    System.out.println("As CSV Header: " + Column.asCSVHeader(columns));
    MyObj it = new MyObj("My Forename", "My Surname");
    System.out.println("As CSV: " + Column.asCSV(columns, it));
}
2

I would prefer Enum - using a type will give you flexibility to extend and change your implementation, using abstraction and still keeping it encapsulated in your class. To use an example, what if at some later point you decide, that you need to format your dates differently for CTL file? With enum you can implement that with abstraction, what matters, when you have ten dates and one hundred columns:

public enum Column {
    ID("1"),
    NAME("Bob"),
    DATE_OF_BIRTH("1980-01-01", "yyyy-MM-dd", "yyyyMMdd");
    private String defaultValue;
    private String ctlDefaultValue;

    Column(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    Column(String defaultValue, String csvFormat, String ctlFormat) {
        this(defaultValue);
        try {
            this.ctlDefaultValue = new SimpleDateFormat(ctlFormat)
                .format(new SimpleDateFormat(csvFormat)
                .parseObject(defaultValue));
        } catch (ParseException e) {
            this.ctlDefaultValue = "";
        }
    }

    public String valueForCTL() {
        return ctlDefaultValue == null ? defaultValue : ctlDefaultValue;
    }

    public String valueForCsv() {
        return defaultValue;
    }

    public static void main(String[] args) {
        System.out.println(DATE_OF_BIRTH.valueForCTL());
        System.out.println(DATE_OF_BIRTH.valueForCsv());
    }
}

You may also wish to store a type of value for some reason, then you need only to add new property to your enum. With map approach, you would need actually a second map or to define a type to be used as map value.

And what if you discover, that you need different order for CSV and CTL? Well, with Map (Sorted one) it should be easy to create a differently sorted copy, but you can go with enum with the same ease:

public enum ColumnDifferentOrder{
    ID("1", 3),
    NAME("Bob", 2),
    DATE_OF_BIRTH("1980-01-01", 1);

    private String defaultValue;
    private int csvOrder;

    ColumnDifferentOrder(String defaultValue, int csvOrder) {
        this.defaultValue = defaultValue;
        this.csvOrder = csvOrder;
    }

    public static ColumnDifferentOrder[] orderForCsv() {
        ColumnDifferentOrder[] columns  = ColumnDifferentOrder.values();
        Arrays.sort(columns, new Comparator<ColumnDifferentOrder>() {
            @Override
            public int compare(ColumnDifferentOrder o1, ColumnDifferentOrder o2) {
                return o1.csvOrder - o2.csvOrder;
            }
        });
        return columns;
    }

    public static ColumnDifferentOrder[] orderForCtl() {
        return ColumnDifferentOrder.values();
    }


    public static void main(String[] args) {
        System.out.println(Arrays.toString(ColumnDifferentOrder.orderForCsv()));
        System.out.println(Arrays.toString(ColumnDifferentOrder.orderForCtl()));

    }
}

The only thing I can thought of, where Map will be better, is when you actually do not want to iterate, but access chosen value - Map will get it faster.

1

I would create a class with properties for column names like:

public class CSVRecord {
  private int id;
  private String name;

  // getters and setters here.
}

Where id and name are actual columns in csv file.

Then create a list of your records List<CSVRecord> csvRecordList.

1

When designing application, you always think about what can possibly change in the future. It is possible, that the order of your columns will change, or you will get more columns. The main thing that you gain when using a Map (you should also program to the Map interface, not HashMap) is the ability to store this configuration easily outside your application.

Separating configuration from application logic can be very useful and even if you are not planning this at this moment, you may want to do this at some point in the future.

Using enums give you more static control over your code so it makes it slightly easier to avoid mistakes (but you should be writing tests regardless).

In summary:

Positives Of Map:

  • Easy to change configuration
  • Configuration can be stored outside of the system
  • Easier to extend
  • Can write a general code / framework for this task

Negatives of Map:

  • Slightly easier to make a mistake

Positives of Enum:

  • Easier not to make msitakes, static checking

Negatives of Enum:

  • You fix your configuration at complie time
  • Harder to extend

For the Map solution, I actually recommend SortedMap interface rather than HashMap with TreeMap being the actual implementation. If the order matters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.