Rich Maes
Rich Maes

Reputation: 1222

JAVAFX architecture for storing user data

I am very new to JAVAFX. I just started looking at how to store user files and in my case I don't want to use XML. I am creating a new version of a tool that in the past was done in perl. The user files were text based, and were done with a proprietary definition.

As an example

PROJECT_NAME some_device;
DATA_BUS_WIDTH 32;

LINE_TYPE_A name_A mask:0xFFFFFFFF default:0x00000000 "Some documentation about this line type
 SUB_LINE_TYPE_A_0  sub_name_0  PROP0 "Some documentation about what the sub line does";
 SUB_LINE_TYPE_A_1  sub_name_1  PROP0 "Some documentation about what the sub line does";
LINE_TYPE_B name_B PROP_2 Size:0x1000 "Some documentation about this line type - important Linetype B has different properties than the previous line type A"
 SUB_LINE_TYPE_B_0  sub_name_0  "Some documentation about what the sub line does";
LINE_TYPE_C name_C  Other PROPs "And more documentation"

What I am thinking about doing is creating a document class, and then creating an array, that would hold each of the lines. But the rub is that the document class would hold an array of objects where there are three (or even more) types of objects. A LINE_TYPE_A, LINE_TYPE_B, etc.. objects, where there are different properties for each type. I am familiar with creating an array of One type of object. Creating an array of multiple types, seems odd to me, but it seems like there should be a way. As I rolled through the list, I would have to be able to look at each item and say, you're a TYPE A or your a TYPE C, so I could work with the data appropriately.

Is this the right thing to do to create a custom document format? Or is there something else I should be doing? Again, though, I do want to stay away from XML.

Upvotes: 0

Views: 176

Answers (1)

AlmasB
AlmasB

Reputation: 3407

There are a few ways you can go about structuring this:

A) Define a data structure, say DataLine, to hold your data. It would contain all the information in a particular line: TYPE, name, etc. Then continue with what you wanted to do:

class Document {
    //array or list or some other collection type of `DataLine`
    DataLine[] lines;

    void doStuff() {
        for (DataLine line : lines) {
            // line.getType(), line.getName(), etc
        }
    }
}

B) Define an inheritance based structure that would isolate common fields / query methods, e.g.

// abstract class if you have some common methods or interface
abstract class DataLine {

    abstract DataType getType();
}

// some specific data that belongs to only TypeA
class DataLineTypeA extends / implements DataLine {

}

class Document {
    //array or list or some other collection type of `DataLine`
    DataLine[] lines;

    void doStuff() {
        for (DataLine line : lines) {
            // can also check with getType() if you have it stored
            if (line instanceof DataLineTypeA) {
                DataLineTypeA typeA = (DataLineTypeA) line;
                // do stuff with typeA specific methods
            }

            // etc.
        }
    }
}

Finally, you either create your own data parser if you have a formal definition of your data, or use an intermediate format like JSON. Alternatively, you can make data persistent by using the default Java serialization mechanism.

Upvotes: 1

Related Questions