com.mythosis.beandiff
Class DiffGenerator

java.lang.Object
  extended by com.mythosis.beandiff.DiffGenerator

public class DiffGenerator
extends java.lang.Object

Author:
juramirez@softekpr.com

Constructor Summary
DiffGenerator()
           
 
Method Summary
 java.util.Map<java.lang.String,java.lang.String> diff(java.lang.String tag, java.lang.Object original, java.lang.Object current)
          Calculates the difference between two objects.
static void main(java.lang.String[] args)
           
 DataResolver registerDataResolver(java.lang.String forType, DataResolver resolver)
          Registers a DataResolver to resolve data of type forType.
 java.util.Map<java.lang.String,java.lang.String> resolveObject(java.lang.String tag, java.lang.Object object)
          Resolves an object using Diffable fields as appropriate.
 void unregisterDataResolver(java.lang.String forType)
          Unregisters a DataResolver.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DiffGenerator

public DiffGenerator()
Method Detail

diff

public java.util.Map<java.lang.String,java.lang.String> diff(java.lang.String tag,
                                                             java.lang.Object original,
                                                             java.lang.Object current)
Calculates the difference between two objects.

This method uses the Diffable and DiffField annotations to hierarchically contruct a tree-like map with the differences between two objects. Specifically, the map contains an entry for each difference between the two objects, where the key indicates where the difference ocurrs, and the value indicates the original value. The key names are constructed using a starting tag with the field names appended.

The Diffable annotation is used to tell DiffGenerator's diff() method that that class is prepared for it. The DiffField annotation tells the diff() method that that field should be included when calculating the difference.

Parameters:
tag - initial key name for difference map
original - original object
current - new object
Returns:
a Map<String, String> with the differences between the original and new objects, where the keys are the fields where the differences occur, and the values are the original values.
Throws:
java.lang.IllegalArgumentException - If the two objects to compare are not of the same class.
See Also:
Diffable, DiffField

resolveObject

public java.util.Map<java.lang.String,java.lang.String> resolveObject(java.lang.String tag,
                                                                      java.lang.Object object)
Resolves an object using Diffable fields as appropriate.

This method is used internally by the diff() method to add the correct values when the current object is null at any given point in the comparation.

Parameters:
tag - initial key name for map
object - object to be resolved
Returns:
a map with all the data in the object, according to normal DiffGenerator rules

registerDataResolver

public DataResolver registerDataResolver(java.lang.String forType,
                                         DataResolver resolver)
Registers a DataResolver to resolve data of type forType.

The diff() method can resolve data, using a DataResolver. The field's DiffField annotation can define a data type for the field, which the diff() method will then lookup in its registered resolvers, and pass the value found in the actual field to this resolver, and use the result for the actual difference calculation.

Parameters:
forType - the user-defined and application-specific field/data type to register a resolver for
resolver - the resolver to register for the field/data type
Returns:
the DataResolver previously registered for this data type, if any, or null otherwise
See Also:
DataResolver, unregisterDataResolver(String), DiffField

unregisterDataResolver

public void unregisterDataResolver(java.lang.String forType)
Unregisters a DataResolver.

Parameters:
forType - the field/data type for which to unregister the resolver
See Also:
DataResolver, registerDataResolver(String, DataResolver), DiffField

main

public static void main(java.lang.String[] args)
                 throws java.lang.NoSuchFieldException
Throws:
java.lang.NoSuchFieldException