Advertisement
DulcetAirman

ConsumerFunction (Java 8)

Jan 15th, 2014
169
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.claude_martin.lambda;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.util.Objects;
  5. import java.util.function.Consumer;
  6. import java.util.function.Function;
  7. import java.util.function.Supplier;
  8. import java.util.function.UnaryOperator;
  9.  
  10. /**
  11.  * A {@link Function} that returns a <code>null</code> of type Void.
  12.  *
  13.  * <p>
  14.  * This is to demonstrate why a {@link Consumer} isn't also a {@link Function}.
  15.  * <p>
  16.  * This interface could come handy if you need a collection of functional methods that are all of
  17.  * type {@link Function} but you want to include some {@link Consumer}s.
  18.  * <p>
  19.  * {@link UnaryOperator} is a Function as it has a return value, but a {@link Consumer} does not
  20.  * return anything. There exists a member {@link #VOID} that references an instance of {@link Void}
  21.  * or null and can be used as the returned value.
  22.  * <p>
  23.  * The same is true for similar Types such as Supplier. There you'd have to pass something to
  24.  * {@link Supplier#get() get}.
  25.  */
  26. @FunctionalInterface
  27. public interface ConsumerFunction<T> extends Function<T, Void> {
  28.  
  29.   /**
  30.    * {@inheritDoc}
  31.    *
  32.    * @returns instance of {@link Void} or null
  33.    */
  34.   public Void apply(T t);
  35.  
  36.   /** @see Consumer#accept(Object) */
  37.   default public void accept(final T t) {
  38.     apply(t);
  39.   }
  40.  
  41.   /** Convert this object to a regular {@link Consumer}. */
  42.   default public Consumer<T> toConsumer() {
  43.     return ConsumerFunction.this::apply;
  44.   }
  45.  
  46.   /**
  47.    * This instance of {@link Void} can be used as the returned value. It is null if no instance
  48.    * could be created.
  49.    */
  50.   public static final Void VOID = ((Supplier<Void>) () -> {
  51.     try {
  52.       final Constructor<Void> constructor = Void.class.getDeclaredConstructor();
  53.       constructor.setAccessible(true);
  54.       return constructor.newInstance();
  55.     } catch (Throwable t) {
  56.       return null;
  57.     }
  58.   }).get();
  59.  
  60.   /** Convert some {@link Consumer} to a {@link Function}. */
  61.   public static <T> ConsumerFunction<T> toFunction(final Consumer<T> consumer) {
  62.     return t -> {
  63.       consumer.accept(t);
  64.       return VOID;
  65.     };
  66.   }
  67.  
  68.   /** @see Consumer#andThen(Consumer) */
  69.   default ConsumerFunction<T> andThen(final ConsumerFunction<? super T> after) {
  70.     Objects.requireNonNull(after);
  71.     return (T t) -> {
  72.       apply(t);
  73.       after.apply(t);
  74.       return VOID;
  75.     };
  76.   }
  77.  
  78.   /** @see Consumer#andThen(Consumer) */
  79.   default Consumer<T> andThen(final Consumer<? super T> after) {
  80.     Objects.requireNonNull(after);
  81.     return (T t) -> {
  82.       apply(t);
  83.       after.accept(t);
  84.     };
  85.   }
  86. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement