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 profiling suggests that it is a bottleneck, one can do this by hand:

  private final static Pojomator<Manual> POJOMATOR = Pojomatic.pojomator(Manual.class);

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

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

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

See Also:
Pojomator

Method Summary
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 java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException - 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 java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException - 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 java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException - if pojo's class has no properties annotated for use with Pojomatic
See Also:
Pojomator.doEquals(Object, Object)

diff

public static <T,S extends T> Differences diff(T pojo,
                                               S other)
                        throws java.lang.IllegalArgumentException
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 - must not be null
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.IllegalArgumentException - if pojo's class has no properties annotated for use with Pojomatic

pojomator

public static <T> Pojomator<T> pojomator(java.lang.Class<T> pojoClass)
                              throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException - if pojoClass has no properties annotated for use with Pojomatic


Copyright © 2008-2009. All Rights Reserved.