Class ComparisonValueExtractor<T,E extends Number>

All Implemented Interfaces:
CanonicallyNamed, ExternalizableLite, PortableObject, QueryMapComparator, Remote.Function<T,E>, Remote.ToDoubleFunction<T>, Remote.ToIntFunction<T>, Remote.ToLongFunction<T>, ValueExtractor<T,E>, Serializable, Comparator, Function<T,E>, ToDoubleFunction<T>, ToIntFunction<T>, ToLongFunction<T>

public class ComparisonValueExtractor<T,E extends Number> extends AbstractCompositeExtractor<T,E>
A synthetic ValueExtractor that returns a result of comparison between two values extracted from the same target. In a most general case, the extracted value represents an Integer value calculated accordingly to the contract of Comparable.compareTo(T) or Comparator.compare(T, T) methods. However, in more specific cases, when the compared values are of common numeric type, the ComparisonValueExtractor will return a numeric difference between those values. The Java type of the comparing values will dictate the Java type of the result.

For example, lets assume that a cache contains business objects that have two properties: SellPrice and BuyPrice (both double). Then, to query for all objects that have SellPrice less than BuyPrice we would use the following:

 ValueExtractor extractDiff = new ComparisonValueExtractor(
   new ReflectionExtractor("getSellPrice"),
   new ReflectionExtractor("getBuyPrice"));
 Filter filter = new LessFilter(extractDiff, new Double(0.0));
 Set entries = cache.entrySet(filter);
 
Since:
Coherence 3.4
Author:
gg 2008.02.15
See Also:
  • Field Details

    • m_comparator

      protected Comparator m_comparator
      An underlying Comparator object (optional).
  • Constructor Details

    • ComparisonValueExtractor

      public ComparisonValueExtractor()
      Default constructor (necessary for the ExternalizableLite interface).
    • ComparisonValueExtractor

      public ComparisonValueExtractor(String sMethod1, String sMethod2)
      Construct a ComparisonValueExtractor based on two method names. Note: values returned by both methods must be Comparable.
      Parameters:
      sMethod1 - the name of the first method to invoke via reflection
      sMethod2 - the name of the second method to invoke via reflection
    • ComparisonValueExtractor

      public ComparisonValueExtractor(String sMethod1, String sMethod2, Comparator<? super E> comp)
      Construct a ComparisonValueExtractor based on two method names and a Comparator object.
      Parameters:
      sMethod1 - the name of the first method to invoke via reflection
      sMethod2 - the name of the second method to invoke via reflection
      comp - the comparator used to compare the extracted values (optional)
    • ComparisonValueExtractor

      public ComparisonValueExtractor(ValueExtractor<T,E> ve1, ValueExtractor<T,E> ve2)
      Construct a ComparisonValueExtractor based on two specified extractors. Note: values returned by both extractors must be Comparable.
      Parameters:
      ve1 - the ValueExtractor for the first value
      ve2 - the ValueExtractor for the second value
    • ComparisonValueExtractor

      public ComparisonValueExtractor(ValueExtractor<T,E> ve1, ValueExtractor<T,E> ve2, Comparator<? super E> comp)
      Construct a ComparisonValueExtractor based on two specified extractors and a Comparator object.
      Parameters:
      ve1 - the ValueExtractor for the first value
      ve2 - the ValueExtractor for the second value
      comp - the comparator used to compare the extracted values (optional)
  • Method Details

    • getComparator

      public Comparator getComparator()
      Return a Comparator used by this extractor.
      Returns:
      a Comparator used by this extractor; null if the natural value comparison should be used
    • extract

      public E extract(Object oTarget)
      Extract the value from the passed object. The returned value may be null. For intrinsic types, the returned value is expected to be a standard wrapper type in the same manner that reflection works; for example, int would be returned as a java.lang.Integer.
      Specified by:
      extract in interface ValueExtractor<T,E extends Number>
      Overrides:
      extract in class AbstractExtractor<T,E extends Number>
      Parameters:
      oTarget - the object to extract the value from
      Returns:
      the extracted value; null is an acceptable value
    • readExternal

      public void readExternal(DataInput in) throws IOException
      Restore the contents of this object by loading the object's state from the passed DataInput object.
      Specified by:
      readExternal in interface ExternalizableLite
      Overrides:
      readExternal in class AbstractCompositeExtractor<T,E extends Number>
      Parameters:
      in - the DataInput stream to read data from in order to restore the state of this object
      Throws:
      IOException - if an I/O exception occurs
    • writeExternal

      public void writeExternal(DataOutput out) throws IOException
      Save the contents of this object by storing the object's state into the passed DataOutput object.
      Specified by:
      writeExternal in interface ExternalizableLite
      Overrides:
      writeExternal in class AbstractCompositeExtractor<T,E extends Number>
      Parameters:
      out - the DataOutput stream to write the state of this object to
      Throws:
      IOException - if an I/O exception occurs
    • readExternal

      public void readExternal(PofReader in) throws IOException
      Restore the contents of a user type instance by reading its state using the specified PofReader object.
      Specified by:
      readExternal in interface PortableObject
      Overrides:
      readExternal in class AbstractCompositeExtractor<T,E extends Number>
      Parameters:
      in - the PofReader from which to read the object's state
      Throws:
      IOException - if an I/O error occurs
    • writeExternal

      public void writeExternal(PofWriter out) throws IOException
      Save the contents of a POF user type instance by writing its state using the specified PofWriter object.
      Specified by:
      writeExternal in interface PortableObject
      Overrides:
      writeExternal in class AbstractCompositeExtractor<T,E extends Number>
      Parameters:
      out - the PofWriter to which to write the object's state
      Throws:
      IOException - if an I/O error occurs