| 1 | --- |
| 2 | layout: docs |
| 3 | title: Accessible and Mutable |
| 4 | prev_section: reload |
| 5 | next_section: debugging |
| 6 | permalink: /docs/accessible-mutable/ |
| 7 | --- |
| 8 | |
| 9 | By default, object created by OWNER are immutable and promote information hiding. |
| 10 | |
| 11 | This means that once the Config object is created its properties cannot be modified, and cannot be accessed in any other |
| 12 | way than using the methods that are properties mapping methods. |
| 13 | |
| 14 | Those limitations are imposed by design, but sometime users may find this problematic. So here they come in the play the |
| 15 | interfaces [Mutable] and [Accessible]. |
| 16 | |
| 17 | [Mutable]: http://owner.aeonbits.org/apidocs/latest/org/aeonbits/owner/Mutable.html |
| 18 | [Accessible]: http://owner.aeonbits.org/apidocs/latest/org/aeonbits/owner/Accessible.html |
| 19 | |
| 20 | This is the hierarchy of the `Mutable` and `Accessible` interfaces: |
| 21 | |
| 22 |  |
| 23 | |
| 24 | The Mutable interface |
| 25 | --------------------- |
| 26 | |
| 27 | The [Mutable] interface allows the developer for runtime modifications of the properties contained in the Config object. |
| 28 | |
| 29 | Example: |
| 30 | |
| 31 | ```java |
| 32 | |
| 33 | interface MyConfig extends Mutable { |
| 34 | @DefaultValue("18") |
| 35 | public Integer minAge(); |
| 36 | public Integer maxAge(); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | MyConfig cfg = ConfigFactory.create(MyConfig.class); |
| 41 | |
| 42 | // this comes from the @DefaultValue |
| 43 | assertEquals(Integer.valueOf(18), cfg.minAge()); |
| 44 | |
| 45 | // now we change the minAge to 21 using setProperty |
| 46 | String oldValue = cfg.setProperty("minAge", "21"); |
| 47 | assertEquals("18", oldValue); // the old value was 18 |
| 48 | assertEquals(Integer.valueOf(21), cfg.minAge()); // now is 21 |
| 49 | |
| 50 | // now we remove the minAge property |
| 51 | oldValue = cfg.removeProperty("minAge"); |
| 52 | assertEquals("21", oldValue); // the old value is 21 |
| 53 | assertNull(cfg.minAge()); // now is null |
| 54 | ``` |
| 55 | |
| 56 | In the above example we saw `setProperty` and `removeProperty` in action, but the Mutable interface adds even more |
| 57 | methods like `clear()`, `load(InputStream)` and `load(Reader)`, and it should allow you to achieve complete write access |
| 58 | to the properties contained inside a Config object. |
| 59 | |
| 60 | The Accessible interface |
| 61 | ------------------------ |
| 62 | |
| 63 | As the [Mutable] interface allows for write access to the properties contained inside a Config object, the [Accessible] |
| 64 | interface allows for read access. |
| 65 | |
| 66 | Example: |
| 67 | |
| 68 | ```java |
| 69 | interface MyConfig extends Accessible { |
| 70 | @DefaultValue("Bohemian Rapsody - Queen") |
| 71 | String favoriteSong(); |
| 72 | |
| 73 | @Key("salutation.text") |
| 74 | @DefaultValue("Good Morning") |
| 75 | String salutation(); |
| 76 | } |
| 77 | |
| 78 | MyConfig cfg = ConfigFactory.create(MyConfig.class); |
| 79 | assertEquals("Good Morning", cfg.getProperty("salutation.text")); |
| 80 | |
| 81 | // print all properties to a PrintWriter |
| 82 | cfg.list(System.out); |
| 83 | |
| 84 | // saves properties to an OutputStream |
| 85 | File tmp = File.createTempFile("owner-", ".tmp"); |
| 86 | cfg.store(new FileOutputStream(tmp), "no comments"); |
| 87 | |
| 88 | ``` |
| 89 | |
| 90 | As you can see, [Accessible] is not limited to the `getProperty()` method, but you can also use this |
| 91 | interface to `list()` or `store()` the properties. |