Setting Values

The following methods allow you to add, remove, and modify entries and sections in a config file:

- {@link oaj.config.Config}
  - {@link oaj.config.Config#set(String,Object) set(String,Object)}
  - {@link oaj.config.Config#set(String,Object,Serializer) set(String,Object,Serializer)}
  - {@link oaj.config.Config#set(String,Object,Serializer,ConfigMod,String,List) set(String,Object,Serializer,ConfigMod,String,List)}
  - {@link oaj.config.Config#set(String,Object,Serializer,ConfigMod[],String,List) set(String,Object,Serializer,ConfigMod[],String,List)}
  - {@link oaj.config.Config#set(String,String) set(String,String)}
  - {@link oaj.config.Config#remove(String) remove(String)}
  - {@link oaj.config.Config#setSection(String,List) setSection(String,List)}
  - {@link oaj.config.Config#setSection(String,List,Map) setSection(String,List,Map)}
  - {@link oaj.config.Config#removeSection(String) removeSection(String)}

// Construct the sample config file programmatically Config config = Config.create("MyConfig.cfg").build() .set("key1", 1) .set("key2", true) .set("key3", new int[]{1,2,3}) .set("key4", new URI("http://foo")) .set("Section1/key1", 2) .set("Section1/key2", false) .set("Section1/key3", new int[]{4,5,6}) .set("Section1/key4", new URI("http://bar")) .commit();

The method {@link oaj.config.Config#set(String,Object,Serializer,ConfigMod[],String,List)} can be used for adding comments and pre-lines to entries, and specifying encoded values.

// Set an encoded value with some comments. config.set("key1", 1, null, ConfigMod.ENCODED, "Same-line comment", Arrays.asList( "# Comment 1", "", "# Comment 2" ) );

# Comment 1 # Comment 2 key1 = 1 # Same-line comment

The last 4 arguments in {@link oaj.config.Config#set(String,Object,Serializer,ConfigMod[],String,List)} are optional in that if you pass null, the current value will not be overwritten. To unset the same-line comment, you should pass in a blank string. To remove pre-lines, you should pass in an empty list.

Sections can be added with optional pre-lines using the setSection methods:

// Set an encoded value with some comments. config.setSection("NewSection", Arrays.asList( "# Section comment 1", "", "# Section comment 2" ) );

# Section comment 1 # Section comment 2 [NewSection]

Changes made to the configuration are transactional in nature. They are kept in memory until you call the {@link oaj.config.Config#commit()} method. Until then, you still see the modified values when you call any of the getters, but the modified values exist only in memory.

Changes can be rolled back using the {@link oaj.config.Config#rollback()} method.