Functionの結果をBiConsumerで実行する(3)

2回も投稿した
Functionの結果をBiConsumerで実行する - Oboe吹きプログラマの黙示録
Functionの結果をBiConsumerで実行する(2) - Oboe吹きプログラマの黙示録
だが、まだまだ改良の余地がある。

andThen なる and で連結の書き方をもっと短くできるはずである。
Objects.requireNonNull による NULLチェックなど除去してしまい、
以下のようにする。

import java.util.function.BiConsumer;
import java.util.function.Function;

@FunctionalInterface
public interface ApplyBiConsumer<T, U> {
   void accept(T t, U u);

   static <T, U, V> ApplyBiConsumer<T, U> of(Function<T, V> f, BiConsumer<U, V> b) {
      return (t,u)->{
         b.accept(u, f.apply(t));
      };
   }
   default <V> ApplyBiConsumer<T, U> and(Function<T, V> f, BiConsumer<U, V> b) {
      return (l, r) -> {
         accept(l, r);
         of(f, b).accept(l, r);
      };
   }
}

これなら、and による連結も
サンプル

import lombok.Data;
@Data
public class Foo{
   private String name;
   private int id;
   private LocalDate birthday;
}

import lombok.Data;
@Data
public class Person{
   private String name;
   private int id;
   private LocalDate birthday;
   private String info;
}
Foo foo;       // Foo のインスタンス
Person person; // Person のインスタンス

// Foo の方に値セット済

ApplyBiConsumer.of(Foo::getName, Person::setName)
.and(Foo::getId, Person::setId)
.and(Foo::getBirthday, Person::setBirthday)
.accept(foo, person);

とかなり簡潔に書けるようになる。