| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| How to prevent a collection from being exposed by getters/settersIntended Audience Prerequisites Steps Naming conventions Intended AudienceAnyone who does not want to expose their collection fields. PrerequisitesEnumerations or iterators can be used to list all elements of a container without providing means of modifications (well, Iterators provide a remove method, but its up to the implementation if this method really does anything). Castor can use enumerations or iterators instead of a getter method to marshal a collection. By using add methods collections can also be unmarshalled without a setter method. StepsConsider the following container object:
To provide better data encapsulation, only the addString() and iterateStrings() methods are made available publically, and as a result, the collection strings is not exposed via getters or setters. The mapping file for above ObjectWithCollection - with the intention to instruct Castor to use the method iterateString() - looks as follows:
Naming conventionsPlease note that for this mechanism to work, the method returning an java.util.Iterator for your collection member has to start with the prefix iterate. The same mechanism works for methods returning java.util.Enumeration as well. In this case, the method prefix needs to be enum, and the method specified needss to return java.util.Enumeration. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||