org.pojomatic
Class Pojomatic

java.lang.Object
  extended by org.pojomatic.Pojomatic

public class Pojomatic
extends java.lang.Object

Static methods for implementing the Object.equals(Object), Object.hashCode() and Object.toString() methods on a annotated POJO. The actual work for a given class is done by a Pojomator created for that class. This class is careful to create only a single Pojomator per POJO class. The overhead for looking up the Pojomator by POJO class is light, so a typical use in a POJO class would be

  @Override public int hashCode() {
    return Pojomatic.hashCode(this);
  }

  @Override public boolean equals(Object other) {
    return Pojomatic.equals(this, other);
  }

  @Override public String toString() {
    return Pojomatic.toString(this);
  }

Under the covers, these methods are referencing a Pojomator instance which is created lazily and cached on a per-class basis. The performance penalty for this is negligible, but if an interface is annotated for Pojomation, using the Pojomator directly is required, since the Pojomator for a class will only reference properties in the class and it's superclasses, but not any implemented interfaces. To do this, first define a static constant POJOMATOR in the interface:

import org.pojomatic.annotations.AutoProperty;
import org.pojomatic.Pojomator;
import org.pojomatic.Pojomatic;

@AutoProperty
public interface Interface {
  static Pojomator<Interface> POJOMATOR = Pojomatic.pojomator(Interface.class);
  ...
}

and then delegate to POJOMATOR in the implementing classes:

public class Implementation implements Interface {
  @Override public int hashCode() {
    return POJOMATOR.doHashCode(this);
  }

  @Override public boolean equals(Object other) {
    return POJOMATOR.doEquals(this, other);
  }

  @Override public String toString() {
    return POJOMATOR.doToString(this);
  }
  ...
}

See Also:
Pojomator

Method Summary
static boolean areCompatibleForEquals(java.lang.Class<?> classA, java.lang.Class<?> classB)
          Compute whether classA and classB are compatible for equality as specified by the documentation for Pojomator.isCompatibleForEquality(Class).
static
<T,S extends T>
Differences
diff(T pojo, S other)
          Compute the differences between pojo and other among the properties examined by equals(Object, Object) for type T.
static
<T> boolean
equals(T pojo, java.lang.Object other)
          Compute whether pojo and other are equal to each other in the sense of Object's equals method.
static
<T> int
hashCode(T pojo)
          Compute the hashCode for a POJO.
static
<T> Pojomator<T>
pojomator(java.lang.Class<T> pojoClass)
          Get the Pojomator for pojoClass.
static
<T> java.lang.String
toString(T pojo)
          Compute the toString representation for a POJO.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toString

public static <T> java.lang.String toString(T pojo)
                                 throws NoPojomaticPropertiesException
Compute the toString representation for a POJO.

Type Parameters:
T - the type of the POJO
Parameters:
pojo - the POJO - must not be null
Returns:
the toString representation of pojo.
Throws:
NoPojomaticPropertiesException - if pojo's class has no properties annotated for use with Pojomatic
See Also:
Pojomator.doToString(Object)

hashCode

public static <T> int hashCode(T pojo)
                    throws NoPojomaticPropertiesException
Compute the hashCode for a POJO.

Type Parameters:
T - the type of the POJO
Parameters:
pojo - the POJO - must not be null
Returns:
the hashCode for pojo.
Throws:
NoPojomaticPropertiesException - if pojo's class has no properties annotated for use with Pojomatic
See Also:
Pojomator.doHashCode(Object)

equals

public static <T> boolean equals(T pojo,
                                 java.lang.Object other)
                      throws NoPojomaticPropertiesException
Compute whether pojo and other are equal to each other in the sense of Object's equals method.

Type Parameters:
T - the type of the POJO
Parameters:
pojo - the POJO - must not be null
other - the object to compare to for equality
Returns:
whether pojo and other are equal to each other in the sense of Object's equals method.
Throws:
NoPojomaticPropertiesException - if pojo's class has no properties annotated for use with Pojomatic
See Also:
Pojomator.doEquals(Object, Object)

areCompatibleForEquals

public static boolean areCompatibleForEquals(java.lang.Class<?> classA,
                                             java.lang.Class<?> classB)
Compute whether classA and classB are compatible for equality as specified by the documentation for Pojomator.isCompatibleForEquality(Class).

Parameters:
classA - the first class to check for compatibility for equality
classB - the second class to check for compatibility for equality
Returns:
true if the two classes are compatible for equality, or false otherwise.

diff

public static <T,S extends T> Differences diff(T pojo,
                                               S other)
                        throws java.lang.NullPointerException,
                               NoPojomaticPropertiesException
Compute the differences between pojo and other among the properties examined by equals(Object, Object) for type T.

Type Parameters:
T - the static type of the first object to compare
S - the static type of the first object to compare
Parameters:
pojo - the instance to diff against
other - the instance to diff
Returns:
the list of differences (possibly empty) between instance and other among the properties examined by equals(Object, Object) for type T.
Throws:
java.lang.NullPointerException - if pojo or other are null (this behavior may change in future releases).
NoPojomaticPropertiesException - if pojo's class has no properties annotated for use with Pojomatic, or if the types of pojo and other are not compatible for equality with each other (this behavior may change in future releases).

pojomator

public static <T> Pojomator<T> pojomator(java.lang.Class<T> pojoClass)
                              throws NoPojomaticPropertiesException
Get the Pojomator for pojoClass. The same instance will be returned every time for a given value of pojoClass.

Type Parameters:
T - the type represented by pojoClass
Parameters:
pojoClass - the class to create a Pojomator for.
Returns:
a Pojomator<T>
Throws:
NoPojomaticPropertiesException - if pojoClass has no properties annotated for use with Pojomatic


Copyright © 2008-2010. All Rights Reserved.