public class Pojomatic extends Object
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);
}
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);
...
}
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);
}
...
}
Pojomator
Modifier and Type | Method | Description |
---|---|---|
static boolean |
areCompatibleForEquals(Class<?> classA,
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> |
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,
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(Class<T> pojoClass) |
Get the
Pojomator for pojoClass . |
static <T> String |
toString(T pojo) |
Compute the
toString representation for a POJO. |
public static <T> String toString(T pojo) throws NoPojomaticPropertiesException
toString
representation for a POJO.T
- the type of the POJOpojo
- the POJO - must not be nulltoString
representation of pojo
.NoPojomaticPropertiesException
- if pojo
's class has no properties annotated for
use with PojomaticPojomator.doToString(Object)
public static <T> int hashCode(T pojo) throws NoPojomaticPropertiesException
hashCode
for a POJO.T
- the type of the POJOpojo
- the POJO - must not be nullhashCode
for pojo
.NoPojomaticPropertiesException
- if pojo
's class has no properties annotated for
use with PojomaticPojomator.doHashCode(Object)
public static <T> boolean equals(T pojo, Object other) throws NoPojomaticPropertiesException
pojo
and other
are equal to each other in the sense of
Object
's equals
method.T
- the type of the POJOpojo
- the POJO - must not be nullother
- the object to compare to for equalitypojo
and other
are equal to each other in the sense of
Object
's equals
method.NoPojomaticPropertiesException
- if pojo
's class has no properties annotated for
use with PojomaticPojomator.doEquals(Object, Object)
public static boolean areCompatibleForEquals(Class<?> classA, Class<?> classB)
classA
and classB
are compatible for equality as specified
by the documentation for Pojomator.isCompatibleForEquality(Class)
.classA
- the first class to check for compatibility for equalityclassB
- the second class to check for compatibility for equalitytrue
if the two classes are compatible for equality, or false
otherwise.public static <T,S extends T> Differences diff(T pojo, S other) throws NullPointerException, NoPojomaticPropertiesException
pojo
and other
among the properties
examined by equals(Object, Object)
for type T
.T
- the static type of the first object to compareS
- the static type of the first object to comparepojo
- the instance to diff againstother
- the instance to diffinstance
and other
among the properties examined by equals(Object, Object)
for type T
.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).public static <T> Pojomator<T> pojomator(Class<T> pojoClass) throws NoPojomaticPropertiesException
Pojomator
for pojoClass
. While the same instance will be returned every time
for a given value of pojoClass
, highly performance-sensitive applications may want to cache the value
returned in a static variable on the class in question. Note that a static Pojomator for a parent class
will miss any additional properties when used on a child class.T
- the type represented by pojoClass
pojoClass
- the class to create a Pojomator
for.Pojomator<T>
NoPojomaticPropertiesException
- if pojoClass
has no properties annotated for use
with PojomaticCopyright © 2008–2018. All rights reserved.