Class ConditionalFunction<T>

  • Type Parameters:
    T - the returned value type.
    All Implemented Interfaces:
    java.io.Serializable, Function<T>, Functor, NullaryFunctor

    public final class ConditionalFunction<T>
    extends java.lang.Object
    implements Function<T>, java.io.Serializable
    A Function similiar to Java's "ternary" or "conditional" operator (? :). Given a predicate p and functions f and g, evaluates to p.test() ? f.evaluate() : g.evaluate().

    Note that although this class implements Serializable, a given instance will only be truly Serializable if all the underlying functors are. Attempts to serialize an instance whose delegates are not all Serializable will result in an exception.

    Version:
    $Revision$ $Date$
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Function<? extends T> elseFunc
      the function executed if the condition is not satisfied.
      private static int HASH_SHIFT
      Base hash integer used to shift hash.
      private Predicate ifPred
      the condition to be evaluated.
      private static long serialVersionUID
      serialVersionUID declaration.
      private Function<? extends T> thenFunc
      the function executed if the condition is satisfied.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object that)
      Indicates whether some other object is "equal to" this functor.
      boolean equals​(ConditionalFunction<?> that)
      Learn whether another ConditionalFunction is equal to this.
      T evaluate()
      Evaluate this function.
      int hashCode()
      Returns a hash code for this functor adhering to the general Object.hashCode contract.
      java.lang.String toString()
      Returns a human readable description of this functor.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        serialVersionUID declaration.
        See Also:
        Constant Field Values
      • HASH_SHIFT

        private static final int HASH_SHIFT
        Base hash integer used to shift hash.
        See Also:
        Constant Field Values
      • ifPred

        private final Predicate ifPred
        the condition to be evaluated.
      • thenFunc

        private final Function<? extends T> thenFunc
        the function executed if the condition is satisfied.
      • elseFunc

        private final Function<? extends T> elseFunc
        the function executed if the condition is not satisfied.
    • Constructor Detail

      • ConditionalFunction

        public ConditionalFunction​(Predicate ifPred,
                                   Function<? extends T> thenFunc,
                                   Function<? extends T> elseFunc)
        Create a new ConditionalFunction.
        Parameters:
        ifPred - if
        thenFunc - then
        elseFunc - else
    • Method Detail

      • evaluate

        public T evaluate()
        Evaluate this function.
        Specified by:
        evaluate in interface Function<T>
        Returns:
        the T result of this evaluation
      • equals

        public boolean equals​(java.lang.Object that)
        Indicates whether some other object is "equal to" this functor. This method must adhere to general Object.equals contract. Additionally, this method can return true only if the specified Object implements the same functor interface and is known to produce the same results and/or side-effects for the same arguments (if any).

        While implementators are strongly encouraged to override the default Object implementation of this method, note that the default Object implementation does in fact adhere to the functor equals contract.

        Specified by:
        equals in interface Functor
        Overrides:
        equals in class java.lang.Object
        Parameters:
        that - the object to compare this functor to
        Returns:
        true iff the given object implements this functor interface, and is known to produce the same results and/or side-effects for the same arguments (if any).
        See Also:
        Functor.hashCode()
      • equals

        public boolean equals​(ConditionalFunction<?> that)
        Learn whether another ConditionalFunction is equal to this.
        Parameters:
        that - ConditionalFunction to test
        Returns:
        boolean
      • hashCode

        public int hashCode()
        Returns a hash code for this functor adhering to the general Object.hashCode contract. Implementators are strongly encouraged but not strictly required to override the default Object implementation of this method.
        Specified by:
        hashCode in interface Functor
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        a hash code for this functor
        See Also:
        Functor.equals(java.lang.Object)
      • toString

        public java.lang.String toString()
        Returns a human readable description of this functor. Implementators are strongly encouraged but not strictly required to override the default Object implementation of this method.
        Specified by:
        toString in interface Functor
        Overrides:
        toString in class java.lang.Object
        Returns:
        a human readable description of this functor