net.sf.adatagenerator.modifiers
Class BeanFieldModifier<F,R,S extends R>

java.lang.Object
  extended by net.sf.adatagenerator.modifiers.BeanFieldModifier<F,R,S>
Type Parameters:
F - the field type
R - the bean type
All Implemented Interfaces:
Comparable<NamedInstance>, BeanModifier<S>, NamedInstance
Direct Known Subclasses:
StringFieldModifier

public class BeanFieldModifier<F,R,S extends R>
extends Object
implements BeanModifier<S>

A BeanModifier that changes just one field in a bean. The field is accessed and modified by reflection.

Author:
rphall

Field Summary
protected static Logger logger
           
 
Constructor Summary
BeanFieldModifier(Class<R> beanClass, String fieldNameStem, ValueModifier<F> valueModifier)
          Creates a field modifier that gets, modifies and sets a value for the specified field.
BeanFieldModifier(String name, String context, Class<R> beanClass, String fieldNameStem, ValueModifier<F> valueModifier)
          Creates a field modifier that gets, modifies and sets a value for the specified field.
BeanFieldModifier(String name, String context, String fieldNameStem, Method fieldAccessor, Method fieldMutator, ValueModifier<F> valueModifier)
          Creates a field modifier that sets and gets a field value using the specified field accessor and mutator, respectively.
 
Method Summary
 int compareContexts(NamedInstance namedInstance)
          Compares the context of this instance to the context of another instance using the context comparator of this instance.
 int compareTo(NamedInstance namedInstance)
          Compares this instance to another instance using the comparator of this instance.
 String getContext()
          The context in which this instance is named.
 Comparator<String> getContextComparator()
           
protected  Method getFieldAccessor()
           
protected  Method getFieldMutator()
           
 String getFieldNameStem()
           
 Class<F> getFieldType()
           
 String getName()
          Implementing classes should return a name that will be comparable within some context
 Comparator<NamedInstance> getNamedInstanceComparator()
           
protected  ValueModifier<F> getValueModifier()
           
 S modifyBean(S bean)
          Modifies the specified bean.
static
<F,R> R
modifyBean(String fieldNameStem, Method fieldAccessor, Method fieldMutator, ValueModifier<F> valueModifier, R bean)
           
protected static
<F,R> R
modifyBeanInternal(String fieldNameStem, Method fieldAccessor, Method fieldMutator, ValueModifier<F> valueModifier, R bean)
           
protected static
<F> void
validateArgs0(String fieldNameStem, Method fieldAccessor, Method fieldMutator)
          Preconditions to validateArgs1(String, Method, Method, ValueModifier).
protected static
<F> void
validateArgs1(String fieldNameStem, Method fieldAccessor, Method fieldMutator, ValueModifier<F> valueModifier)
          Preconditions to #BeanFieldModifier(String, Method, Method, ValueModifier) and modifyBean(String, Method, Method, ValueModifier, Object)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static Logger logger
Constructor Detail

BeanFieldModifier

public BeanFieldModifier(Class<R> beanClass,
                         String fieldNameStem,
                         ValueModifier<F> valueModifier)
Creates a field modifier that gets, modifies and sets a value for the specified field. The modifier is assigned a unique, non-null, random name with a null context.

Parameters:
beanClass - the bean type
fieldNameStem - the field name, that is, stripped of any prefix such as set, get or is
valueModifier - a non-null modifier consistent with the field accessor and mutator

BeanFieldModifier

public BeanFieldModifier(String name,
                         String context,
                         Class<R> beanClass,
                         String fieldNameStem,
                         ValueModifier<F> valueModifier)
Creates a field modifier that gets, modifies and sets a value for the specified field.

Parameters:
beanClass - the bean type
fieldNameStem - the field name, that is, stripped of any prefix such as set, get or is
valueModifier - a non-null modifier consistent with the field accessor and mutator

BeanFieldModifier

public BeanFieldModifier(String name,
                         String context,
                         String fieldNameStem,
                         Method fieldAccessor,
                         Method fieldMutator,
                         ValueModifier<F> valueModifier)
Creates a field modifier that sets and gets a field value using the specified field accessor and mutator, respectively. The return type of the accessor must be assignable to the parameter type of the mutator. If the specified field type is not null, this constructor will validate that the return type of the field accessor and the parameter type of the field mutator match the field type.

Parameters:
name - the name of this modifier instance
fieldNameStem - the field name, that is, stripped of any prefix such as set, get or is
fieldAccessor - the method used to get a field value of a bean
fieldMutator - the method used to set a field value of a bean
valueModifier - a non-null modifier consistent with the field accessor and mutator
Method Detail

validateArgs1

protected static <F> void validateArgs1(String fieldNameStem,
                                        Method fieldAccessor,
                                        Method fieldMutator,
                                        ValueModifier<F> valueModifier)
Preconditions to #BeanFieldModifier(String, Method, Method, ValueModifier) and modifyBean(String, Method, Method, ValueModifier, Object)


validateArgs0

protected static <F> void validateArgs0(String fieldNameStem,
                                        Method fieldAccessor,
                                        Method fieldMutator)
Preconditions to validateArgs1(String, Method, Method, ValueModifier). Reused by CompositeBeanFieldModifier.


getFieldAccessor

protected Method getFieldAccessor()

getFieldMutator

protected Method getFieldMutator()

getFieldType

public Class<F> getFieldType()

getFieldNameStem

public String getFieldNameStem()

getValueModifier

protected ValueModifier<F> getValueModifier()

modifyBean

public S modifyBean(S bean)
                       throws ModificationException
Description copied from interface: BeanModifier
Modifies the specified bean.

Specified by:
modifyBean in interface BeanModifier<S extends R>
Parameters:
bean - the object to modify
Returns:
the modified target (or a modified copy of the target)
Throws:
ModificationException - if modification fails

modifyBean

public static <F,R> R modifyBean(String fieldNameStem,
                                 Method fieldAccessor,
                                 Method fieldMutator,
                                 ValueModifier<F> valueModifier,
                                 R bean)
                    throws ModificationException
Throws:
ModificationException

modifyBeanInternal

protected static <F,R> R modifyBeanInternal(String fieldNameStem,
                                            Method fieldAccessor,
                                            Method fieldMutator,
                                            ValueModifier<F> valueModifier,
                                            R bean)
                               throws ModificationException
Throws:
ModificationException

getName

public String getName()
Description copied from interface: NamedInstance
Implementing classes should return a name that will be comparable within some context

Specified by:
getName in interface NamedInstance
Returns:
a value that identifies an instance in some context. This parameter may be null for an anonymous instance, but if it is non-null, the value will be a trimmed String

getContext

public String getContext()
Description copied from interface: NamedInstance
The context in which this instance is named. If another instance has different context from this instance, then implementing classes should ensure that NamedInstance.compareTo(NamedInstance) returns the same (non-zero) value that is the result of comparing the contexts.

Specified by:
getContext in interface NamedInstance
Returns:
may be null if the context is implicit and well understood, and trimmed otherwise

compareContexts

public int compareContexts(NamedInstance namedInstance)
Description copied from interface: NamedInstance
Compares the context of this instance to the context of another instance using the context comparator of this instance.

Specified by:
compareContexts in interface NamedInstance

getContextComparator

public Comparator<String> getContextComparator()
Specified by:
getContextComparator in interface NamedInstance

compareTo

public int compareTo(NamedInstance namedInstance)
Description copied from interface: NamedInstance
Compares this instance to another instance using the comparator of this instance.

Specified by:
compareTo in interface Comparable<NamedInstance>
Specified by:
compareTo in interface NamedInstance

getNamedInstanceComparator

public Comparator<NamedInstance> getNamedInstanceComparator()
Specified by:
getNamedInstanceComparator in interface NamedInstance


Copyright © 2011-2012. All Rights Reserved.