org.springframework.web.servlet.tags.form
Class SelectedValueComparator

java.lang.Object
  extended by org.springframework.web.servlet.tags.form.SelectedValueComparator

abstract class SelectedValueComparator
extends java.lang.Object

Utility class for testing whether a candidate value matches a data bound value. Eagerly attempts to prove a comparison through a number of avenues to deal with issues such as instance inequality, logical (String-representation-based) equality and PropertyEditor-based comparison.

Full support is provided for comparing arrays, Collections and Maps.

Equality Contract

For single-valued objects equality is first tested using standard Java equality. As such, user code should endeavour to implement Object.equals(java.lang.Object) to speed up the comparison process. If Object.equals(java.lang.Object) returns false then an attempt is made at an exhaustive comparison with the aim being to prove equality rather than disprove it.

Special support is given for instances of LabeledEnum with a String-based comparison of the candidate value against the code of the LabeledEnum. This can be useful when a LabeledEnum is used to define a list of '<option>' elements in HTML.

Next, an attempt is made to compare the String representations of both the candidate and bound values. This may result in true in a number of cases due to the fact both values will be represented as Strings when shown to the user.

Next, if the candidate value is a String, an attempt is made to compare the bound value to result of applying the corresponding PropertyEditor to the candidate. This comparison may be executed twice, once against the direct String instances, and then against the String representations if the first comparison results in false.

Since:
2.0
Author:
Rob Harrop, Juergen Hoeller

Constructor Summary
SelectedValueComparator()
           
 
Method Summary
private static boolean collectionCompare(java.util.Collection boundCollection, java.lang.Object candidateValue, BindStatus bindStatus)
           
private static boolean exhaustiveCollectionCompare(java.util.Collection collection, java.lang.Object candidateValue, BindStatus bindStatus)
           
private static boolean exhaustiveCompare(java.lang.Object boundValue, java.lang.Object candidate, java.beans.PropertyEditor editor, java.util.Map<java.beans.PropertyEditor,java.lang.Object> convertedValueCache)
           
static boolean isSelected(BindStatus bindStatus, java.lang.Object candidateValue)
          Returns true if the supplied candidate value is equal to the value bound to the supplied BindStatus.
private static boolean mapCompare(java.util.Map boundMap, java.lang.Object candidateValue, BindStatus bindStatus)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SelectedValueComparator

SelectedValueComparator()
Method Detail

isSelected

public static boolean isSelected(BindStatus bindStatus,
                                 java.lang.Object candidateValue)
Returns true if the supplied candidate value is equal to the value bound to the supplied BindStatus. Equality in this case differs from standard Java equality and is described in more detail here.


collectionCompare

private static boolean collectionCompare(java.util.Collection boundCollection,
                                         java.lang.Object candidateValue,
                                         BindStatus bindStatus)

mapCompare

private static boolean mapCompare(java.util.Map boundMap,
                                  java.lang.Object candidateValue,
                                  BindStatus bindStatus)

exhaustiveCollectionCompare

private static boolean exhaustiveCollectionCompare(java.util.Collection collection,
                                                   java.lang.Object candidateValue,
                                                   BindStatus bindStatus)

exhaustiveCompare

private static boolean exhaustiveCompare(java.lang.Object boundValue,
                                         java.lang.Object candidate,
                                         java.beans.PropertyEditor editor,
                                         java.util.Map<java.beans.PropertyEditor,java.lang.Object> convertedValueCache)