开发者

How to implement reference data Java/DB

开发者 https://www.devze.com 2023-02-13 04:45 出处:网络
I\'ve got a table with some factors that I need to incorporate into a Java program. At first I was thinking of hardcoding the n开发者_StackOverflowumber but it seems like a pain trying to create a dat

I've got a table with some factors that I need to incorporate into a Java program. At first I was thinking of hardcoding the n开发者_StackOverflowumber but it seems like a pain trying to create a data structure that will fit the factors. So I wanted to ask around and see if it would be better to implement this as reference data in a database, a flat file or in java. The number would change every six months and would be used for mathematical computations.

Thoughts?

How to implement reference data Java/DB


For slow-changing data like this, I would use an external config file. Based on the structure of your data, it seems that a CSV would work well, and would be easy for a business user to edit using Excel.

If it will change more often, you need to generate the data programmatically, or you want to provide a UI for editing the data, you could move it to a database.


You would have to create a data structure to contain the data regardless of how you store them. But the data structure for this kind of data does not have to be complex. It is just a list of values with attributes. You don't have to store them in a complex table-like structure.

Loading the data from a flat text file would also be quite easy when representing the data as a single list.

public class DataTable {

    private List<Entry> table = new ArrayList<Entry>();

    public double getValue(Sex sex, MaritalStatus maritalStatus, AgeInterval ageInterval, Type type) {
        for (Entry entry : table) {
            if (entry.sex == sex && entry.maritalStatus == maritalStatus && entry.ageInterval == ageInterval && entry.type == type) {
                return entry.value;
            }
        }
        throw new IllegalArgumentException("Unknown value");
    }

    public void load(String filename) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
            String line;
            while ((line = reader.readLine()) != null) {
                StringTokenizer t = new StringTokenizer(line, ":");
                table.add(new Entry(
                        Sex.valueOf(t.nextToken()),
                        MaritalStatus.valueOf(t.nextToken()),
                        AgeInterval.valueOf(t.nextToken()),
                        Type.valueOf(t.nextToken()),
                        Double.valueOf(t.nextToken())));
            }
        } catch (IOException e) {
            throw new IllegalStateException("Failed to read the data file", e);
        }
    }

}

enum Sex {M, F}
enum MaritalStatus {SINGLE, MARRIED}
enum AgeInterval {I16_21, I22_35, I35_55, I55}
enum Type {GD, NGD} // Whatever this is ...

class Entry {
    Sex sex;
    MaritalStatus maritalStatus;
    AgeInterval ageInterval;
    Type type;
    double value;

    Entry(Sex sex, MaritalStatus maritalStatus, AgeInterval ageInterval, Type type, double value) {
        this.sex = sex;
        this.maritalStatus = maritalStatus;
        this.ageInterval = ageInterval;
        this.type = type;
        this.value = value;
    }
}

The data file would look like this:

M:SINGLE:I16_21:GD:1.10
F:SINGLE:I16_21:GD:1.20
...


You could represent it as XML, but that might be a little heavy for such numeric data. But the XML would allow you to be fairly descriptive and self documenting. Then later you could easily parse this into Java(or another language of your choice).

Partial XML example:

<dataset>
  <gd>
    <16to21>
      <single>
        <male>1.10</male>
        <female>1.20</female>
      </single>
      <married>
        <male>0.90</male>
        <female>0.80</female>
      </married>
    </16to21>
    ...
  </gd>
  <ngd>
    ...
  </ngd>


One way that you could break up the fields is gender, age, marital_status, GD_VS_NGD, the data inside the table, and some identifier for the time period that you are using this data for unless you do not need to keep records of the data.

0

精彩评论

暂无评论...
验证码 换一张
取 消