Dzisiaj zajmiemy się dwoma ciekawymi interfejsami funkcjonalnymi. Pierwszy z nich to Supplier oraz BooleanSupplier. Każdy z tych interfejsów posiada jedną metodę abstrakcyjną. Zaczynajmy więc.

Supplier

Czyli tak naprawdę dostawca. Posiada abstrakcyjną metodę get(), która po wywołaniu zwróci nam obiekt, który zdefiniowaliśmy w naszej lambdzie. Na początek przyjrzyjmy się jak wygląda dokładnie ten interfejs:

@FunctionalInterface
public interface Supplier<T> {
​
  /**
   * Dostarcza określony wynik
   */
  T get();
}

Jak widzimy, cały interfejs jet bardzo prosty. Zobaczmy jak można go użyć w praktyce:

    List<Book> list = new ArrayList<>();
​
    Supplier<Book> supplier = Book::new;
​
    for (int i = 0; i < 100; i++) {
      list.add(supplier.get());
    }
​
    list.forEach(e-> System.out.println(e));

Na początek tworzymy pustą listę elementów Book. Definiujemy nasze wyrażenie lambda w oparciu o Supplier, którego zadaniem jest tworzenie nowego obiektu Book. W pętli kręcę się 100 razy i dodaję do mojej listy kolejne książki za pomocą mojego dostawcy. Wywołanie metody get() realizuje stworzenie nowego elementu. Dla udowodnienia wszystkiego wypisuję wszystkie elementy 😉

Wynik:

Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}
Book{title='null', price=0.0, cover='null'}

BooleanSupplier

BooleanSupplier ma jedno zadanie, które realizuje za pomocą abstrakcyjnej metody getAsBoolean().

@FunctionalInterface
public interface BooleanSupplier {
​
  /**
   * Dostarcza wynik w postaci boolean
   */
  boolean getAsBoolean();
}

Zobaczmy jak możemy go użyć. Tworzę obiekt książka, a zaraz potem wyrażenie lambda oparte na BooleanSupplier. W tym wyrażeniu zwracam losowo true lub false. Ostatecznie booleanSupplier.getAsBoolean() służy mi do wypełnienia pola isPromotion w klasie Book.

    Book book = new Book(19.99, "Czysty kod", "twarda");
​
    BooleanSupplier booleanSupplier = () -> {
      Random random = new Random();
      return random.nextBoolean();
    };
​
    book.setPromotion(booleanSupplier.getAsBoolean());

Wynik:

false

To tyle jeśli chodzi o te interfejsy funkcjonalne. Pamiętajcie, że kod z przykładami jest oczywiście na githubie.