    001package visrec.ri.spi;
    002
    003import visrec.ri.BufferedImageImageFactory;
    004
    005import javax.visrec.ImageFactory;
    006import javax.visrec.spi.ImageFactoryService;
    007import java.awt.image.BufferedImage;
    008import java.util.HashMap;
    009import java.util.Map;
    010import java.util.Objects;
    011import java.util.Optional;
    012
    013/**
    014 * Default implementation of {@link ImageFactoryService} which serves the implementations of {@link ImageFactory}.
    015 *
    016 * @author Kevin Berendsen
    017 */
    018public final class DefaultImageFactoryService implements ImageFactoryService {
    019
    020    private static final Map<Class<?>, ImageFactory<?>> imageFactories;
    021    static {
    022        imageFactories = new HashMap<>();
    023        imageFactories.put(BufferedImage.class, new BufferedImageImageFactory());
    024    }
    025
    026    /**
    027     * Get the {@link ImageFactory} by image type.
    028     * @param imageCls image type in {@link Class} object which is able to
    029     *                 be processed by the image factory implementation.
    030     * @param <T> image type.
    031     * @return {@link ImageFactory} wrapped in {@link Optional}. If the {@link ImageFactory} could not be
    032     * found then the {@link Optional} would contain null.
    033     */
    034    @Override
    035    public <T> Optional<ImageFactory<T>> getByImageType(Class<T> imageCls) {
    036        Objects.requireNonNull(imageCls, "imageCls == null");
    037        ImageFactory<?> imageFactory = imageFactories.get(imageCls);
    038        return Optional.ofNullable((ImageFactory<T>) imageFactory);
    039    }
    040}