Serializable
, Cloneable
public abstract class AbstractDto extends Object implements Serializable, Cloneable
objects
are purely used for data
representation, without any relevant object functionality. Traditionally, people often use the JavaBeans convention
for such objects, meaning for every attribute, there will be a private field defined plus a getter and/or a setter
method. This is rather cumbersome to program for a class where you're only interested in the data and (considering
there are getters and setters) mutability is not a real issue. There are libraries such a Lombok to help you with
this by generating this code for you.
However, what is inherently wrong with plain public fields in this case? What do the getters and setters actually buy you in 'percieved' encapsulation?
However, wouldn't it be nice to be able to use simply public fields and no methods whatsoever, but still be able to
use the objects in hash-based
collections, call equals()
and toString()
methods or even clone
the object?
Well, now you can. Just extend this AbstractDTO object, add public fields and you're ready with your data transfer object.
This abstract base class explicitly does support the use of public fields. What if you need to 'override' a field with a getter / setter method? No problem! This abstract superclass will detect property accessor methods and use those when applicable over the public fields.
Modifier | Constructor | Description |
---|---|---|
protected |
AbstractDto() |
Default constructor.
|
Modifier and Type | Method | Description |
---|---|---|
AbstractDto |
clone() |
Implementation of
clone based on the public fields and property accessor methods of the
concrete subclass. |
boolean |
equals(Object other) |
Implementation of
equals based on the public fields and property accessor methods
of the concrete subclass. |
protected boolean |
equals(Object objectA,
Object objectB) |
Equals implementation that provides specific equality implementations for
Map , List ,
Array and BigDecimal types. |
int |
hashCode() |
Implementation of
hashCode based on the public fields and property accessor methods
of the concrete subclass. |
protected int |
hashCode(Object obj) |
Hashcode implementation that returns the hashCode for various objects, so they match the equals definition.
|
String |
toString() |
Implementation of
toString based on the public fields and property accessor methods
of the concrete subclass. |
protected AbstractDto()
This constructor was made protected to discourage trying to instantiate this abstract class directly.
public boolean equals(Object other)
equals
based on the public fields and property accessor methods
of the concrete subclass.
BigDecimals: Please note that BigDecimal
property values will not be compared
by the standard equals()
implementation of BigDecimal, as that will declare the same number with
different precision to be unequal (e.g. "0" is unequal to "0.00" by default). This implementation uses the
compareTo()
method of BigDecimal
instead, yielding
mathematical "0" and "0.00" equal to each-other. Please note that although that is technically not correct, it
is very useful for e.g. monetary value comparisons etc.
public int hashCode()
hashCode
based on the public fields and property accessor methods
of the concrete subclass.
BigDecimals: Please note that this method contains a workaround for BigDecimal
property values,
so their hashCode
outcome will be compatible with the equals(Object)
outcome. This is
simply achieved by using the hash code of the double value
instead.
public String toString()
toString
based on the public fields and property accessor methods
of the concrete subclass. This method is delegated to the dedicated ToStringBuilder
helper class that
also supports reflective toString resolution.
The names and values of all public properties will be added
to the resulting String.
toString
in class Object
ToStringBuilder
.ToStringBuilder
public AbstractDto clone()
protected int hashCode(Object obj)
obj
- The object to calculate the hash code for (might be null
for properties without a value).protected boolean equals(Object objectA, Object objectB)
Map
, List
,
Array
and BigDecimal
types.
The Map
implementation was added to be able to simply determine equality of all reflected properties of
concrete DTO object instances (which are returned as a Map
from String
to Object
).
BigDecimal
values are evaluated based on their BigDecimal.compareTo(BigDecimal)
results.
Example: BigDecimals "0"
and "0.00"
are mathematically /scientifically unequal due to different
precisions, but represent the same numerical value. Equals will therefore return true
in this case.
objectA
- The first object to be compared for equality
(might be null
for properties without a value).objectB
- The second object to be compared for equality
(might be null
for properties without a value).true
if the two objects are equal to each-other.Copyright © 2016–2020 Talsma ICT. All rights reserved.