org.daisy.streamline.api.tasks.TaskGroupFactory Maven / Gradle / Ivy
package org.daisy.streamline.api.tasks;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* Provides an interface for task group factories. The purpose of this
* interface is to expose an implementation of a task group.
* A task group factory implementation provides task groups for
* a any number of supported specifications.
*
*
* To comply with this interface, an implementation must be thread safe and
* address both the possibility that only a single instance is created and used
* throughout and that new instances are created as desired.
*
*
* @author Joel Håkansson
*/
public interface TaskGroupFactory {
/**
* Returns true if this factory can create instances for the specified locale.
* @param specification the specification to test
* @return true if this factory can create instances for the specified specification, false otherwise
*/
public default boolean supportsSpecification(TaskGroupSpecification specification) {
for (TaskGroupInformation i : listAll()) {
if (specification.matches(i)) {
return true;
}
}
return false;
}
/**
* Returns true if this factory provided this information (in other words, is
* equal to a provided information).
* @param specification the information to test
* @return true if this factory can provided this information, false otherwise
*/
public boolean supportsSpecification(TaskGroupInformation specification);
/**
* Returns a new task group.
* @param specification the specification for the task group
* @return returns a new task group
*/
public TaskGroup newTaskGroup(TaskGroupSpecification specification);
/**
* Lists information about supported task groups.
* @return returns a set of information
*/
public Set listAll();
/**
* Lists information about supported task groups that supports the specified locale.
* @param locale the locale
* @return returns a set of information for the specified locale
*/
public default Set list(String locale) {
//TODO: use streams
Objects.requireNonNull(locale);
Set ret = new HashSet<>();
for (TaskGroupInformation info : listAll()) {
if (info.matchesLocale(locale)) {
ret.add(info);
}
}
return ret;
}
/**
* Informs the implementation that it was discovered and instantiated using
* information collected from a file within the META-INF/services
directory.
* In other words, it was created using SPI (service provider interfaces).
*
* This information, in turn, enables the implementation to use the same mechanism
* to set dependencies as needed.
*
* If this information is not given, an implementation
* should avoid using SPIs and instead use
* declarative services
* for dependency injection as specified by OSGi. Note that this also applies to
* several newInstance() methods in the Java API.
*
* The class that created an instance with SPI must call this method before
* putting it to use.
*/
public default void setCreatedWithSPI() {}
}