StrategyLoader { static <T> Optional<T> load(final String strategy, final Iterable<StrategyService> loader, final BiFunction<StrategyService, String, Optional<T>> provider) { return StreamUtil.toStream(loader) .map(iss -> processStrategyService(iss, strategy, provider)) .flatMap(Optional::stream) .findFirst(); } private  StrategyLoader(); static Optional<InvestmentStrategy> toInvest(final String strategy); static Optional<SellStrategy> toSell(final String strategy); static Optional<PurchaseStrategy> toPurchase(final String strategy); static Optional<ReservationStrategy> forReservations(final String strategy);  }
@Test void loading() { final InvestmentStrategy is = (availableLoans, portfolio, restrictions) -> Stream.empty(); final StrategyService iss = new StrategyService() { @Override public Optional<InvestmentStrategy> toInvest(final String strategy) { return Optional.of(is); } @Override public Optional<SellStrategy> toSell(final String strategy) { return Optional.empty(); } @Override public Optional<PurchaseStrategy> toPurchase(final String strategy) { return Optional.empty(); } @Override public Optional<ReservationStrategy> forReservations(final String strategy) { return Optional.empty(); } }; assertThat(StrategyLoader.load("", Collections.singleton(iss), StrategyService::toInvest)) .contains(is); }