com.github.robozonky.common.extensions.StrategyLoader Maven / Gradle / Ivy
/*
* Copyright 2019 The RoboZonky Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.robozonky.common.extensions;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.BiFunction;
import java.util.stream.Stream;
import com.github.robozonky.api.strategies.InvestmentStrategy;
import com.github.robozonky.api.strategies.PurchaseStrategy;
import com.github.robozonky.api.strategies.ReservationStrategy;
import com.github.robozonky.api.strategies.SellStrategy;
import com.github.robozonky.api.strategies.StrategyService;
import com.github.robozonky.util.StreamUtil;
import io.vavr.Lazy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Implements Java's {@link ServiceLoader} to provide suitable strategy implementations.
*/
public final class StrategyLoader {
private static final Logger LOGGER = LogManager.getLogger(StrategyLoader.class);
private static final Lazy> LOADER =
ExtensionsManager.INSTANCE.getServiceLoader(StrategyService.class);
private StrategyLoader() {
// no instances
}
static Optional processStrategyService(final StrategyService service, final String strategy,
final BiFunction> getter) {
try {
return getter.apply(service, strategy);
} catch (final Exception ex) {
LOGGER.error("Failed reading strategy.", ex);
return Optional.empty();
}
}
static Optional load(final String strategy, final Iterable loader,
final BiFunction> provider) {
return StreamUtil.toStream(loader)
.map(iss -> processStrategyService(iss, strategy, provider))
.flatMap(o -> o.map(Stream::of).orElse(Stream.empty()))
.findFirst();
}
public static Optional toInvest(final String strategy) {
LOGGER.debug("Reading investment strategy.");
return load(strategy, LOADER.get(), StrategyService::toInvest);
}
public static Optional toSell(final String strategy) {
LOGGER.debug("Reading selling strategy.");
return load(strategy, LOADER.get(), StrategyService::toSell);
}
public static Optional toPurchase(final String strategy) {
LOGGER.debug("Reading purchasing strategy.");
return load(strategy, LOADER.get(), StrategyService::toPurchase);
}
public static Optional forReservations(final String strategy) {
LOGGER.debug("Reading strategy for reservations.");
return load(strategy, LOADER.get(), StrategyService::forReservations);
}
}