|
||||||||||
| PREV NEXT | FRAMES NO FRAMES | |||||||||
Object.equals(Object), Object.hashCode() and
Object.toString() by
annotating properties on a Plain Old Java Object, or POJO.
See:
Description
| Packages | |
|---|---|
| org.pojomatic | |
| org.pojomatic.annotations | |
| org.pojomatic.diff | |
| org.pojomatic.formatter | |
| org.pojomatic.internal | The classes in this package are intended for internal use only. |
Pojomatic is a library which allows simple implementation of the three methods
Object.equals(Object), Object.hashCode() and
Object.toString() by
annotating properties on a Plain Old Java Object, or POJO.
There are two steps to "pojomate" a POJO class: annotating, and implementing
the three methods.
AutoProperty and
PojoFormat, while property-specific behavior can be
controlled by the Property and
PropertyFormat annotations. A recommended practice is
to provide an AutoProperty annotation at the class level, and then override the behavior it
specifies on a per-property basis as needed. This minimizes both the number of annotations needed,
as well as the number of additional steps needed when adding new properties to a class.
Pojomatic:
@Override public int hashCode() {
return Pojomatic.hashCode(this);
}
@Override public String toString() {
return Pojomatic.toString(this);
}
@Override public boolean equals(Object o) {
return Pojomatic.equals(this, o);
}
AutoProperty annotation:
@AutoProperty //all fields are included by default
public class Common {
...
}
autoDetect=AutoDetectPolicy.METHOD to the
AutoProperty annotation.
By default, all properties are used in the implementations of the equals, hashCode
and toString methods; this can be changed via the
policy parameter to
AutoProperty.
Additionally, one can override this choice on a per-property basis by use of the
Property
annotation. For example, if you have a class with a mutable field which you do not wish to include
in the hashCode calculation, you can accomplish this via:
@AutoProperty
public class Employee {
private final String firstName;
private final String lastName;
@Property(policy=PojomaticPolicy.EQUALS_TO_STRING)
private String securityLevel;
...
}
toString implementation provided by Pojomatic defaults to using
DefaultPojoFormatter; you can specify your
own formatting by providing an alternate implementation of
PojoFormatter and using the
PojoFormat annotation. In addition to controling the
overall format of toString, the formatting of individual properties can be controlled by
a PropertyFormat annotation referencing an
implementation of PropertyFormatter. In addition
to DefaultPropertyFormatter, this library
also provides an AccountNumberFormatter for
formatting properties with sensitive data.
Taking all of this together, a Customer POJO using pojomatic might look like:
@AutoProperty
public class Customer {
private final String firstName;
private final String lastName;
@PropertyFormat(AccountNumberFormatter.class)
private final String accountNumber;
public Customer(String accountNumber, String firstName, String lastName) {
this.accountNumber = accountNumber;
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public String getAccountNumber() { return accountNumber; }
@Override public int hashCode() {
return Pojomatic.hashCode(this);
}
@Override public String toString() {
return Pojomatic.toString(this);
}
@Override public boolean equals(Object o) {
return Pojomatic.equals(this, o);
}
}
|
||||||||||
| PREV NEXT | FRAMES NO FRAMES | |||||||||