org.codehaus.mojo.pomtools.wrapper
Class ObjectWrapper

java.lang.Object
  extended by org.codehaus.mojo.pomtools.wrapper.modify.AbstractModifiableObject
      extended by org.codehaus.mojo.pomtools.wrapper.ObjectWrapper
All Implemented Interfaces:
Modifiable
Direct Known Subclasses:
ListWrapper, PropertyWrapper, StringValueWrapper

public class ObjectWrapper
extends AbstractModifiableObject

Version:
$Id: ObjectWrapper.java 2166 2006-07-18 21:32:16Z carlos $
Author:
David Hawkins

Field Summary
static java.lang.String FIELD_PATH_SEPARATOR
           
 
Constructor Summary
ObjectWrapper(Modifiable parentModifiable, ObjectWrapper parent, java.lang.Object objectToWrap, java.lang.String name, java.lang.Class objectToWrapClass)
          Constructs an ObjectWrapper that wraps the objectToWrap.
ObjectWrapper(ObjectWrapper parent, java.lang.Object objectToWrap, java.lang.String name)
          Constructs a new ObjectWrapper and does not specify a implClass.
ObjectWrapper(ObjectWrapper parent, java.lang.Object objectToWrap, java.lang.String name, java.lang.Class objectToWrapClass)
          Constructs a new ObjectWrapper and specifies the implClass that is used to create an empty object if the objectToWrap is null.
 
Method Summary
protected  java.lang.Object createDefaultInstance()
          Creates an instance of our wrapped class by calling its default constructor.
 BeanFields getFields()
          Returns the internal BeanFields used to describe this object.
 java.lang.Object getFieldValue(BeanField field)
          Returns the value for the wrapped object for the specified field.
 java.lang.Object getFieldValue(BeanField field, boolean createIfNull)
          Returns the value for the wrapped object for the specified field.
 java.lang.Object getFieldValue(java.lang.String fieldName)
          Returns the value for the wrapped object for the specified fieldName.
 java.lang.String getFullName()
          Returns the fully qualified name of the wrapped object.
protected  java.lang.Object getInternalWrappedObject()
          Returns the original wrapped object passed to or created in the constructor.
 java.lang.String getName()
          Returns the name of the field to which this object belongs.
 ObjectWrapper getParent()
           
 java.lang.String getValueLabel()
          Returns the toString() representation of the value with an appended annotation if this object has been modified.
 java.lang.Object getWrappedObject()
          Returns the original wrapped object with all modifications applied to it.
 boolean isEmpty()
          Iterates through each BeanField and determines if the value is empty.
 boolean isFieldModified(BeanField field)
           
 boolean isSameAsDefault()
          Returns whether the wrapped object is in the same state as it's default constructor.
 void setFieldValue(BeanField field, java.lang.Object value)
          Sets the value for specified field of the underlying wrapped object to the specified value.
 void setFieldValue(java.lang.String fieldName, java.lang.Object value)
          Sets the value for specified field of the underlying wrapped object to the specified value.
 void setModified(BeanField field)
          Sets the modified flag to true and adds the field to our modified fields.;
 void setModified(boolean modified)
          Clears our list of modified fields if setting to false;
 java.lang.String toString()
           
 
Methods inherited from class org.codehaus.mojo.pomtools.wrapper.modify.AbstractModifiableObject
addChild, isModified, setModified
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FIELD_PATH_SEPARATOR

public static final java.lang.String FIELD_PATH_SEPARATOR
See Also:
Constant Field Values
Constructor Detail

ObjectWrapper

public ObjectWrapper(ObjectWrapper parent,
                     java.lang.Object objectToWrap,
                     java.lang.String name,
                     java.lang.Class objectToWrapClass)
Constructs a new ObjectWrapper and specifies the implClass that is used to create an empty object if the objectToWrap is null. The Modifiable parent is set to the parent;

Parameters:
parent - the parent ObjectWrapper of this object
objectToWrap - the value object to wrap. This is a field of the parent
name - the field name that the objectToWrap is called within the parent
objectToWrapClass - the Class to use to create a new objectToWrap if the supplied one is null

ObjectWrapper

public ObjectWrapper(ObjectWrapper parent,
                     java.lang.Object objectToWrap,
                     java.lang.String name)
Constructs a new ObjectWrapper and does not specify a implClass. The objectToWrap cannot be null when using this constructor. ObjectWrapper needs an instance of an object to wrap and will create one if it is not supplied. The Modifiable parent is set to the parent;

Parameters:
parent - the parent ObjectWrapper of this object
objectToWrap - the value object to wrap. This is a field of the parent
name - the field name that the objectToWrap is called within the parent

ObjectWrapper

public ObjectWrapper(Modifiable parentModifiable,
                     ObjectWrapper parent,
                     java.lang.Object objectToWrap,
                     java.lang.String name,
                     java.lang.Class objectToWrapClass)
Constructs an ObjectWrapper that wraps the objectToWrap. ObjectWrapper needs an instance of an object to wrap will create one the supplied objectToWrap is null. The default constructor will be called on the implClass.

Parameters:
parentModifiable - allows separate specification of the parentModifiable
parent - the parent ObjectWrapper of this object
objectToWrap - the value object to wrap. This is a field of the parent
name - the field name that the objectToWrap is called within the parent
objectToWrapClass - the Class to use to create a new objectToWrap if the supplied one is null
Method Detail

createDefaultInstance

protected java.lang.Object createDefaultInstance()
Creates an instance of our wrapped class by calling its default constructor. TODO: This method could probably use caching when it is being called by isSameAsDefault()


getParent

public ObjectWrapper getParent()

getName

public java.lang.String getName()
Returns the name of the field to which this object belongs.

Returns:
this object's field name

getFullName

public java.lang.String getFullName()
Returns the fully qualified name of the wrapped object.

Objects are comprised of fields which have names. A fully qualified name is the this name of the field this object belongs to, as well as that field's owner and up.
For example:
If this objects name were "dependency", it could have a fullName of:
project.dependencies.dependency

Returns:
the full name of the wrapped object

getValueLabel

public final java.lang.String getValueLabel()
Returns the toString() representation of the value with an appended annotation if this object has been modified.

Returns:
toString() with an annotation of modified

getInternalWrappedObject

protected java.lang.Object getInternalWrappedObject()
Returns the original wrapped object passed to or created in the constructor.

Subclasses should excercise caution an never modify this object directly.

Returns:

getWrappedObject

public java.lang.Object getWrappedObject()
Returns the original wrapped object with all modifications applied to it. Note that this method will return null if the object is the same as the default constructor for the object.

Returns:
the wrapped object or null is the object isEmpty()

getFields

public final BeanFields getFields()
Returns the internal BeanFields used to describe this object.


isEmpty

public boolean isEmpty()
Iterates through each BeanField and determines if the value is empty.

If the value is an instance of ObjectWrapper, the isEmpty() method is called on that object.
If the value is a String, the StringUtils.isNotEmpty(java.lang.String) method is used.
The object is considered to be NOT empty if any field (other than String or ObjectWrapper) is non null.


isSameAsDefault

public boolean isSameAsDefault()
Returns whether the wrapped object is in the same state as it's default constructor. This is useful in maven to prevent writing a section that is really only populated with the defaults.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getFieldValue

public java.lang.Object getFieldValue(java.lang.String fieldName)
Returns the value for the wrapped object for the specified fieldName.

Parameters:
fieldName -
Throws:
java.lang.IllegalArgumentException - if the field cannot be found.

getFieldValue

public java.lang.Object getFieldValue(BeanField field)
Returns the value for the wrapped object for the specified field. This method creates a value if the specified field is null

Parameters:
field -
Returns:

getFieldValue

public java.lang.Object getFieldValue(BeanField field,
                                      boolean createIfNull)
Returns the value for the wrapped object for the specified field. Specifies whether to create the value if the underlying object's value is null. Turning off object creation is useful in testing isEmpty and equality in that it prevents having to do deep comparisons when the value is actually null.

Parameters:
field -
Returns:

setFieldValue

public void setFieldValue(java.lang.String fieldName,
                          java.lang.Object value)
Sets the value for specified field of the underlying wrapped object to the specified value.

Throws:
java.lang.IllegalArgumentException - if the field cannot be found.

setFieldValue

public void setFieldValue(BeanField field,
                          java.lang.Object value)
Sets the value for specified field of the underlying wrapped object to the specified value.


isFieldModified

public boolean isFieldModified(BeanField field)

setModified

public void setModified(BeanField field)
Sets the modified flag to true and adds the field to our modified fields.;


setModified

public void setModified(boolean modified)
Clears our list of modified fields if setting to false;

Specified by:
setModified in interface Modifiable
Overrides:
setModified in class AbstractModifiableObject


Copyright © 2010 The Codehaus. All Rights Reserved.