License     Codehaus     OpenEJB     OpenJMS     OpenORB     Tyrex     

Old releases
  General
  Release 1.2
  Release 1.3rc1

Main
  Home
  About
  Features
  Download
  Dependencies
  Reference guide
  Publications
  JavaDoc
  Maven 2 support
  Maven 2 archetypes
  DTD & Schemas
  Recent HTML changes
  News Archive
  RSS news feed
  Project Wiki

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

Related projects
  Spring ORM support
  Spring XML factories
  WS frameworks

XML
  XML

XML Code Generator
  XML Code Generator

JDO
  Introduction
  First steps
  Using JDO
  JDO Config
  Types
  JDO Mapping
  JDO FAQ
  JDO Examples
  JDO HOW-TOs
  Tips & Tricks
  Other Features
  JDO sample JAR

Tools
  Schema generator

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

DDL Generator
  Using DDL Generator
  Properties
  Ant task
  Type Mapping

More
  The Examples
  3rd Party Tools
  JDO Tests
  XML Tests
  Configuration
 
 

About
  License
  User stories
  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.