How to add descriptions to java.util.Properties?

I’ve only recently discovered the java.util.Properties class and grew quite fond of it. Since properties are only a simple mapping of key-value pairs, is there a convenient way of adding a little description String to them and call it for example like properties.get("key").getDescription()?

Answer

No. The closest you can do is to have a naming convention such as

key=value
key.description=value

and then use

properties.get("key.description")

You can of course hide that convention by wrapping the properties into your own class:

class Entry {
    private String value;
    private String description;

    ...
}

class Config { 
    private Properties properties;

    public Entry get(String key) {
        // get the value for key
        // get the value for key.description
        // create and return an Entry
    }
}

But if you really want structured data, you should use JSON, YAML or XML rather than properties.

Leave a Reply

Your email address will not be published. Required fields are marked *