com.varmateo.yawg.core.CollectiveTemplateService Maven / Gradle / Ivy
/**************************************************************************
*
* Copyright (c) 2016-2020 Yawg project contributors.
*
**************************************************************************/
package com.varmateo.yawg.core;
import java.util.Optional;
import java.util.function.Function;
import io.vavr.collection.Seq;
import io.vavr.control.Option;
import com.varmateo.yawg.api.YawgException;
import com.varmateo.yawg.spi.Template;
import com.varmateo.yawg.spi.TemplateService;
/**
*
*/
/* default */ final class CollectiveTemplateService
implements TemplateService {
private final Function> _templateFetcher;
private CollectiveTemplateService(final Seq services) {
if ( services.isEmpty() ) {
_templateFetcher = name -> Optional.empty();
} else {
_templateFetcher = name -> prepareTemplateFromServices(services, name);
}
}
/**
*
*/
public static TemplateService create(final Seq services) {
return new CollectiveTemplateService(services);
}
/**
* {@inheritDoc}
*/
@Override
public Optional prepareTemplate(final String name) {
return _templateFetcher.apply(name);
}
/**
*
*/
private static Optional prepareTemplateFromServices(
final Seq services,
final String name) {
final Option result = services
.map(service -> service.prepareTemplate(name))
.map(Option::ofOptional)
.filter(Option::isDefined)
.map(Option::get)
.headOption();
if ( !result.isDefined() ) {
throw CollectiveTemplateServiceException.unsupportedTemplateFormat(name);
}
return result.toJavaOptional();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy