net.sf.staccatocommons.lang.value
Class RelevantState<A>

java.lang.Object
  extended by net.sf.staccatocommons.lang.predicate.AbstractPredicate2<A,A>
      extended by net.sf.staccatocommons.lang.value.RelevantState<A>
Type Parameters:
A - the type of object this RelevantState applies to
All Implemented Interfaces:
Comparator<A>, net.sf.staccatocommons.defs.Applicable<A,net.sf.staccatocommons.defs.predicate.Predicate<A>>, net.sf.staccatocommons.defs.Applicable2<A,A,Boolean>, net.sf.staccatocommons.defs.Evaluable2<A,A>, net.sf.staccatocommons.defs.partial.NullSafeAware<net.sf.staccatocommons.defs.predicate.Predicate2<A,A>>, net.sf.staccatocommons.defs.predicate.Predicate2<A,A>
Direct Known Subclasses:
AbstractTuple.TupleState

@Transparent
public abstract class RelevantState<A>
extends AbstractPredicate2<A,A>
implements Comparator<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.

Author:
flbulgarelli
See Also:
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

RelevantState

public RelevantState(int relevantAttributesCount,
                     @NonNull
                     org.apache.commons.lang.builder.ToStringStyle toStringStyle)
Creates a new RelevantState

Parameters:
relevantAttributesCount - number of significant properties that determine the value of an object of type A
toStringStyle -

RelevantState

public RelevantState(int relevantAttributesCount)
Creates a new RelevantState, using NamedTupleToStringStyle as toStringStyle

Parameters:
relevantAttributesCount - number of significant properties that determine the value of an object of type A
Method Detail

hashCode

public int hashCode(@NonNull
                    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

Parameters:
object -
Returns:
a hash code computed using the relevant fields of the given object

toString

public String toString(@NonNull
                       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

Parameters:
object -
Returns:
a to string implementation for object, using the style provided by getToStringStyle()

compareTo

public int compareTo(@NonNull
                     A object,
                     A other)
Compares the given 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

Parameters:
object -
other -
Returns:
0 if both arguments are the same, or the result of a field by field comparison

compare

public int compare(A o1,
                   A o2)
Specified by:
compare in interface Comparator<A>

equals

public boolean equals(@NonNull
                      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

Parameters:
object -
other -
Returns:
the result of a BasicEquals test if it is enough to determine equality, or a field by field comparison, otherwise
See Also:
BasicEquals

eval

public boolean eval(A arg0,
                    A arg1)
Specified by:
eval in interface net.sf.staccatocommons.defs.Evaluable2<A,A>

collectState

protected abstract void collectState(@NonNull
                                     A object,
                                     @NonNull
                                     RelevantState.StateCollector s)
Collects the attributes that conform the relevant state of the given object, by adding them to the given RelevantState.StateCollector

Parameters:
object -
s - the RelevantState.StateCollector to which

getToStringStyle

public org.apache.commons.lang.builder.ToStringStyle getToStringStyle()
The ToStringStyle used to create toString representations

Returns:
the toStringStyle


Copyright © 2010-2012 Staccatocommons. All Rights Reserved.