License     Codehaus     OpenEJB     OpenJMS     OpenORB     Tyrex     
 

Main
  Home
  About
  Features
  Download
  Maven 2 support
  API
  DTD & Schemas
  Recent changes
  RSS news feed

Development/Support
  Mailing Lists
  SVN/JIRA
  Contributing
  Support
  Prof. services

Related projects
  Spring ORM support
  Spring XML factories

XML
  Using XML
  XML Mapping
  XML FAQ
  XML HOW-TOs
  Custom Handlers
  Best practice

XML Code Generator
  Code Generator
  Properties
  Custom bindings
  Ant task
  Schema Support
  Example

JDO
  Introduction
  Using JDO
  JDO Config
  Types
  JDO Mapping
  JDO FAQ
  JDO Examples
  JDO HOW-TOs
  Other Features
  JDO sample JAR

Advanced JDO
  Caching
  OQL
  Trans. & Locks
  Design
  KeyGen
  Long Trans.
  Nested Attrs.
  Pooling Examples
  LOBs
  Best practice

More
  Presentations
  The Examples
  3rd Party Tools
  JDO Tests
  XML Tests
  Configuration
  Tips & Tricks
  Full JavaDoc
  CastorWiki
 
 

About
  License
  Contributors
  Marketplace
  Status, Todo
  Changelog
  Library
  Contact
  Project Name

  



How to prevent a collection from being exposed by getters/setters


Intended Audience
Prerequisites
Steps
Naming conventions


Intended Audience

Anyone who does not want to expose their collection fields.

Prerequisites

Enumerations 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.

Steps

Consider the following container object:

public class ObjectWithCollection {
    protected Vector strings = new Vector();
    
    public void addString(String string) {
        strings.add(string);
    }
    
    public Iterator iterateStrings() {
        return strings.iterator();
    }
}

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:

<mapping>
    <class name="ObjectWithContainer">
        <field name="strings" type="string" collection="vector" 
                  get-method="iterateStrings" set-method="addString"/>
    </class>
</mapping>

Naming conventions

Please 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.

 
   
  
   
 


Copyright © 1999-2005 ExoLab Group, Intalio Inc., and Contributors. All rights reserved.
 
Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. XML, XML Schema, XSLT and related standards are trademarks or registered trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web Consortium. All other product names mentioned herein are trademarks of their respective owners.