gpt4 book ai didi

用于静态值的 Java Enum 或 HashMap

转载 作者:搜寻专家 更新时间:2023-10-30 21:28:13 26 4
gpt4 key购买 nike

我正在生成一个 CSV 文件和 CTL 文件以供 sqlldr 使用。 CTL 文件需要知道我要加载的列的名称,而我的 CSV 文件需要知道这些字段的默认值。

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

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

这两个类在 CSVExportFile 中初始化和使用,我有两种方法:

1。枚举

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");
...
}
}

HashMap 缩小了列的范围,意味着它们只能在 CSVExportFile 中使用。我不打算扩展此功能(所有类都是 final),所以我不确定我的 enum 是否对我有任何帮助。

支持/反对每种方法的论点是什么,这是一个更优的特定案例,还是一种方法总是更优?

最佳答案

我总是会在这里使用 enum,因为 enum 有一个天生的顺序,而 Map 没有。

通过使用 enum,您可以从枚举本身生成 CTL 文件,并使用 enum 值作为工厂来填充您的 csv 文件。

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));
}

关于用于静态值的 Java Enum 或 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31473613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com