net.sf.mmm.util.pojo.descriptor.impl
Class DefaultPojoDescriptorEnhancer

java.lang.Object
  extended by net.sf.mmm.util.component.base.AbstractComponent
      extended by net.sf.mmm.util.component.base.AbstractLoggableComponent
          extended by net.sf.mmm.util.pojo.descriptor.impl.DefaultPojoDescriptorEnhancer
All Implemented Interfaces:
PojoDescriptorEnhancer

@Singleton
@Named
public class DefaultPojoDescriptorEnhancer
extends AbstractLoggableComponent
implements PojoDescriptorEnhancer

This is an implementation of the PojoDescriptorEnhancer interface that scans all PropertyDescriptors for accessing Collections, Map s or arrays. For such PojoPropertyDescriptors additional accessors that are NOT already present are created and added.

In a first step the name of the PojoPropertyDescriptor is singularized. If a different singular form is determined, the accessors of the singular-named PojoPropertyDescriptor are copied to the original PojoPropertyDescriptor if such accessors do NOT already exist.
In a second step accessors that are (still) NOT present in the original PojoPropertyDescriptor are created and added as virtual delegates on the container-typed getter (and according setter for array-resizing if available). This will happen for the following modes:

For example the method
List<Foo> getChildren()
is available via the PojoPropertyDescriptor named children. Further the method
void addChild(Foo child)
is available via the PojoPropertyDescriptor named child. This enhancer makes the addChild-method also available via children which is the plural form of child. Further it will add the virtual accessors as described above so it behaves as if the following method would also be present:
 Foo getChild(int index) {
   return getChildren().get(index);
 }
 
 void setChild(int index, Foo child) {
   getChildren().set(index, child);
 }
 
 void removeChild(Foo child) {
   getChildren().remove(child);
 }
 
 int getChildCount() {
   return getChildren().size();
 }
 
This makes it easier for generic access and allows ultimate flexibility since the explicit methods of a POJO always overrule the virtual accessors.

Since:
1.1.0
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Field Summary
private  boolean addSingularAccessors
           
private  boolean addVirtualAccessors
           
private  PojoDescriptorDependencies dependencies
           
private  Singularizer singularizer
          The singularizer.
 
Constructor Summary
DefaultPojoDescriptorEnhancer()
          The constructor.
DefaultPojoDescriptorEnhancer(boolean addSingularAccessors, boolean addVirtualAccessors)
          The constructor.
 
Method Summary
private  void addVirtualAccessor(AbstractPojoPropertyDescriptor propertyDescriptor, PojoPropertyAccessor accessor)
          This method adds the given accessor to the given propertyDescriptor.
protected  void doInitialize()
          This method performs the actual initialization.
 void enhanceDescriptor(AbstractPojoDescriptor<?> descriptor)
          This method performs the generic enhancements on the given descriptor.
protected  PojoDescriptorDependencies getDependencies()
          This method gets the dependencies required for this descriptor.
protected  Singularizer getSingularizer()
          This method gets the Singularizer, which is used to determine singular forms so e.g.
 void setDependencies(PojoDescriptorDependencies dependencies)
          This method sets the dependencies.
 void setSingularizer(Singularizer singularizer)
          This method injects the Singularizer.
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableComponent
getLogger, setLogger
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractComponent
doInitialized, getInitializationState, initialize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

singularizer

private Singularizer singularizer
The singularizer.


addSingularAccessors

private final boolean addSingularAccessors
See Also:
DefaultPojoDescriptorEnhancer(boolean, boolean)

addVirtualAccessors

private final boolean addVirtualAccessors
See Also:
DefaultPojoDescriptorEnhancer(boolean, boolean)

dependencies

private PojoDescriptorDependencies dependencies
See Also:
getDependencies()
Constructor Detail

DefaultPojoDescriptorEnhancer

public DefaultPojoDescriptorEnhancer()
The constructor.


DefaultPojoDescriptorEnhancer

public DefaultPojoDescriptorEnhancer(boolean addSingularAccessors,
                                     boolean addVirtualAccessors)
The constructor.

Parameters:
addSingularAccessors - - if true each getter that points to an array, Collection or Map is scanned. If it has a plural form for that the singular form can be determined, the PojoPropertyAccessors of the singular PojoPropertyDescriptor are copied to the plural PojoPropertyDescriptor as long as no such PojoPropertyAccessor already exists.
addVirtualAccessors - - if true for each getter that points to an array, Collection or Map according virtual PojoPropertyAccessors are created and added as long as senseful and no such PojoPropertyAccessor already exists.
Method Detail

getSingularizer

protected Singularizer getSingularizer()
This method gets the Singularizer, which is used to determine singular forms so e.g. getChildren and addChild can be matched to the same property.

Returns:
the Singularizer to use.

setSingularizer

@Inject
public void setSingularizer(Singularizer singularizer)
This method injects the Singularizer.

Parameters:
singularizer - is the Singularizer to set.

getDependencies

protected PojoDescriptorDependencies getDependencies()
This method gets the dependencies required for this descriptor.

Returns:
the dependencies.

setDependencies

@Inject
public void setDependencies(PojoDescriptorDependencies dependencies)
This method sets the dependencies.

Parameters:
dependencies - are the PojoDescriptorDependencies to set.

doInitialize

protected void doInitialize()
This method performs the actual initialization. It is called when AbstractComponent.initialize() is invoked for the first time.
ATTENTION:
When you override this method from a sub-class you need to do a super.AbstractComponent.doInitialize().

Overrides:
doInitialize in class AbstractLoggableComponent

addVirtualAccessor

private void addVirtualAccessor(AbstractPojoPropertyDescriptor propertyDescriptor,
                                PojoPropertyAccessor accessor)
This method adds the given accessor to the given propertyDescriptor.

Parameters:
propertyDescriptor - is the descriptor of the property where to add the given accessor.
accessor - is the (virtual) accessor to add.

enhanceDescriptor

public void enhanceDescriptor(AbstractPojoDescriptor<?> descriptor)
This method performs the generic enhancements on the given descriptor.

Specified by:
enhanceDescriptor in interface PojoDescriptorEnhancer
Parameters:
descriptor - is the descriptor to enhance.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.