|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sf.staccatocommons.lang.predicate.AbstractPredicate2<A,A>
net.sf.staccatocommons.lang.value.RelevantState<A>
A - the type of object this RelevantState applies to@Transparent public abstract class RelevantState<A>
An abstract class that is capable of identifying the relevant attributes of an object and implementing with them value comparisons, hashing and string representation in a consisten way.
Normal usage of RelevantState is declaring an implementor per class
of type <A>, having a singleton instance of it, and then delegating
Object.equals(Object), Object.hashCode() etc, to it. For
example:
class Customer {
private String name;
private Date birthday;
private Date joinDate;
//...
public int hashCode() {
return state.hashCode(this);
}
public boolean equals(Object obj) {
return state.equals(this, obj);
}
public String toString() {
return state.toString(this);
}
private static RelevantState state = new RelevantState(3) {
protected void collectState(Customer o, StateCollector c) {
c.add(o.name).add(o.birthday).add(o.joinDate);
}
};
In order to do that, RelevantState instances must be
Transparent, allowing them to be shared by instances and threads.
RelevantState is completely reflection-free, and is built on top of
EqualsBuilder, ToStringBuilder, HashCodeBuilder and
CompareToBuilder.
Object.equals(Object),
Object.hashCode(),
Object.toString(),
Comparable.compareTo(Object)| Nested Class Summary | |
|---|---|
static interface |
RelevantState.StateCollector
An object for collecting the attributes of an object that are part of its relevant state |
| Nested classes/interfaces inherited from class net.sf.staccatocommons.lang.predicate.AbstractPredicate2 |
|---|
AbstractPredicate2.NullSafePredicate2, AbstractPredicate2.Soft<A,B> |
| Constructor Summary | |
|---|---|
RelevantState(int relevantAttributesCount)
Creates a new RelevantState, using NamedTupleToStringStyle
as toStringStyle |
|
RelevantState(int relevantAttributesCount,
org.apache.commons.lang.builder.ToStringStyle toStringStyle)
Creates a new RelevantState |
|
| Method Summary | |
|---|---|
protected abstract void |
collectState(A object,
RelevantState.StateCollector s)
Collects the attributes that conform the relevant state of the given object, by adding them to the given RelevantState.StateCollector |
int |
compare(A o1,
A o2)
|
int |
compareTo(A object,
A other)
Compares the given object with other, based on
the fields collected by collectState(Object, StateCollector). |
boolean |
equals(A object,
Object other)
Answers if the given object is equal to other,
based on the fields collected by
collectState(Object, StateCollector)
It is safe to use the result as an implementation of
Object.equals(Object) for object |
boolean |
eval(A arg0,
A arg1)
|
org.apache.commons.lang.builder.ToStringStyle |
getToStringStyle()
The ToStringStyle used to create toString representations |
int |
hashCode(A object)
Answers the hashcode for the given object, based on the attributes collected by collectState(Object, StateCollector)
It is safe to use the result as an implementation of
Object.hashCode() for object |
String |
toString(A object)
Answers a string representation for the given object, based on the attributes collected by collectState(Object, StateCollector)
It is safe to use the result as an implementation of
Object.toString() for object |
| Methods inherited from class net.sf.staccatocommons.lang.predicate.AbstractPredicate2 |
|---|
and, apply, apply, not, nullSafe, or, uncurry |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface java.util.Comparator |
|---|
equals |
| Constructor Detail |
|---|
public RelevantState(int relevantAttributesCount,
@NonNull
org.apache.commons.lang.builder.ToStringStyle toStringStyle)
RelevantState
relevantAttributesCount - number of significant properties that determine the value of an
object of type AtoStringStyle - public RelevantState(int relevantAttributesCount)
RelevantState, using NamedTupleToStringStyle
as toStringStyle
relevantAttributesCount - number of significant properties that determine the value of an
object of type A| Method Detail |
|---|
public int hashCode(@NonNull
A object)
collectState(Object, StateCollector)
It is safe to use the result as an implementation of
Object.hashCode() for object
object -
public String toString(@NonNull
A object)
collectState(Object, StateCollector)
It is safe to use the result as an implementation of
Object.toString() for object
object -
object, using the style
provided by getToStringStyle()
public int compareTo(@NonNull
A object,
A other)
object with other, based on
the fields collected by collectState(Object, StateCollector).
It is safe to use the result as an implementation of
Comparable.compareTo(Object) for object
object - other -
public int compare(A o1,
A o2)
compare in interface Comparator<A>
public boolean equals(@NonNull
A object,
Object other)
object is equal to other,
based on the fields collected by
collectState(Object, StateCollector)
It is safe to use the result as an implementation of
Object.equals(Object) for object
object - other -
BasicEquals test if it is enough to
determine equality, or a field by field comparison, otherwiseBasicEquals
public boolean eval(A arg0,
A arg1)
eval in interface net.sf.staccatocommons.defs.Evaluable2<A,A>
protected abstract void collectState(@NonNull
A object,
@NonNull
RelevantState.StateCollector s)
object, by adding them to the given RelevantState.StateCollector
object - s - the RelevantState.StateCollector to whichpublic org.apache.commons.lang.builder.ToStringStyle getToStringStyle()
ToStringStyle used to create toString representations
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||