NullIfEitherArgIsNull.java

  1. package no.motif.f.base;

  2. import no.motif.f.Fn2;

  3. /**
  4.  * A convenient base implementation of {@link Fn2} which yields
  5.  * <code>null</code> if one or both arguments is <code>null</code>.
  6.  * When both arguments are non-null, they are passed to
  7.  * {@link #orElse(Object, Object)}, which
  8.  * can be implemented without any regard to possible null-pointers.
  9.  *
  10.  * @see Fn2
  11.  */
  12. public abstract class NullIfEitherArgIsNull<I1, I2, O> implements Fn2<I1, I2, O> {

  13.     @Override
  14.     public final O $(I1 first, I2 second) {
  15.         return (first == null || second == null) ? null : orElse(first, second);
  16.     }

  17.     /**
  18.      * The function implementation.
  19.      * @param first the first function argument, guarantied not to be <code>null</code>.
  20.      * @param second the second function argument, guarantied not to be <code>null</code>.
  21.      * @return the function result.
  22.      */
  23.     protected abstract O orElse(I1 first, I2 second);

  24. }