org.pojomatic.internal
Class PojomatorImpl<T>

java.lang.Object
  extended by org.pojomatic.internal.PojomatorImpl<T>
All Implemented Interfaces:
Pojomator<T>

public class PojomatorImpl<T>
extends java.lang.Object
implements Pojomator<T>


Constructor Summary
PojomatorImpl(java.lang.Class<T> clazz)
          Creates an instance for clazz.
 
Method Summary
 Differences doDiff(T instance, T other)
          Compute the differences between instance and other among the properties examined by Pojomator.doEquals(Object, Object).
 boolean doEquals(T instance, java.lang.Object other)
          Compute whether instance and other are equal to each other in the sense of Object's equals method.
 int doHashCode(T instance)
          Compute the hashCode for a given instance of T.
 java.lang.String doToString(T instance)
          Compute the toString representation for a given instance of T.
 boolean isCompatibleForEquality(java.lang.Class<?> otherClass)
          Compute whether otherClass is compatible for equality with T.
 java.lang.String toString()
          Return a simple String representation of this Pojomator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PojomatorImpl

public PojomatorImpl(java.lang.Class<T> clazz)
              throws NoPojomaticPropertiesException
Creates an instance for clazz.

Parameters:
clazz - the class
Throws:
NoPojomaticPropertiesException - if clazz has no properties annotated for use with Pojomatic
Method Detail

doEquals

public boolean doEquals(T instance,
                        java.lang.Object other)
Description copied from interface: Pojomator
Compute whether instance and other are equal to each other in the sense of Object's equals method. For two instances to be considered equal, the first requirement is that their classes must be compatible for equality, as described in the documentation for Pojomator.isCompatibleForEquality(Class).

More precisely, if other is null, this method returns false. Otherwise, if isCompatibleForEquals(other.getClass()) would return false, then this method will return false. Otherwise, this method will return true provided that each property of instance which has a PojomaticPolicy other than TO_STRING or NONE is equal to the corresponding property of other in the following sense:

Specified by:
doEquals in interface Pojomator<T>
Parameters:
instance - the instance to test against - must not be null
other - the instance to test
Returns:
true if instance should be considered equal to other, and false otherwise.
See Also:
Object.equals(Object)

isCompatibleForEquality

public boolean isCompatibleForEquality(java.lang.Class<?> otherClass)
Description copied from interface: Pojomator
Compute whether otherClass is compatible for equality with T. Classes A and B are compatible for equality if they share a common superclass C, and for every class D which is a proper subclass of C and a superclass of A or B (including the classes A and B themselves), the following hold: If T is an interface or is annotated with SubclassCannotOverrideEquals, then all subclasses of T are automatically assumed by T's Pojomator to be compatible for equals with each other and with T. Note that in this case. to add an OverridesEquals annotation or additional properties for inclusion in equals to a subclass of T will result in a violation of the contract for Object.equals(Object).

Specified by:
isCompatibleForEquality in interface Pojomator<T>
Parameters:
otherClass - the class to check for compatibility for equality with T
Returns:
true if otherClass is compatible for equality with T, and false otherwise.

doHashCode

public int doHashCode(T instance)
Description copied from interface: Pojomator
Compute the hashCode for a given instance of T. This is done by computing the hashCode of each property which has a PojomaticPolicy of HASHCODE_EQUALS or ALL (using 0 when the property is null), and combining them in a fashion similar to that of List.hashCode().

Specified by:
doHashCode in interface Pojomator<T>
Parameters:
instance - the instance to compute the hashCode for - must not be null
Returns:
the hashCode of instance
See Also:
Object.hashCode()

doToString

public java.lang.String doToString(T instance)
Description copied from interface: Pojomator
Compute the toString representation for a given instance of T.

The format used depends on the PojoFormatter used for the POJO, and the PropertyFormatter of each property.

For example, suppose a class Person has properties firstName and lastName which are included in its String representation. No PojoFormatter or PropertyFormatter are specified, so the defaults are used. In particular, instances of DefaultPropertyFormatter will be created for firstName and lastName (referred to here as firstNameFormatter and lastNameFormatter, respectively). Let firstNameProperty and lastNameProperty refer to the instances of PropertyElement referring to the properties firstName and lastName respectively.

For a non-null Person instance, the String representation will be created by creating an instance of DefaultPojoFormatter for the Person class (referred to here as personFormatter), and then concatenating the results of following:

  1. personFormatter.getToStringPrefix(Person.class)
  2. personFormatter.getPropertyPrefix(firstNameProperty)
  3. firstNameFormatter.format(firstName)
  4. personFormatter.getPropertySuffix(firstNameProperty)
  5. personFormatter.getPropertyPrefix(lastNameProperty)
  6. lastNameFormatter.format(lastName)
  7. personFormatter.getPropertySuffix(lasttNameProperty)
  8. personFormatter.getToStringSuffix(Person.class)

Specified by:
doToString in interface Pojomator<T>
Parameters:
instance - the instance to compute the toString representation for - must not be null
Returns:
the toString representation of instance
See Also:
Object.toString(), Property.name()

toString

public java.lang.String toString()
Description copied from interface: Pojomator
Return a simple String representation of this Pojomator. This is meant to aid in debugging which properties are being used for which purposes. The contents and format of this representation are subject to change.

Specified by:
toString in interface Pojomator<T>
Overrides:
toString in class java.lang.Object
Returns:
a simple String representation of this Pojomator.

doDiff

public Differences doDiff(T instance,
                          T other)
Description copied from interface: Pojomator
Compute the differences between instance and other among the properties examined by Pojomator.doEquals(Object, Object). Assuming that instance and other are both non-null and have types which are compatible for equals, it is guaranteed that invoking Differences.areEqual() on the returned object will return true iff instance.equals(other).

Specified by:
doDiff in interface Pojomator<T>
Parameters:
instance - the instance to diff against
other - the instance to diff
Returns:
the differences between instance and other among the properties examined by Pojomator.doEquals(Object, Object).
See Also:
Pojomator.doEquals(Object, Object)


Copyright © 2008-2010. All Rights Reserved.