001// SPDX-License-Identifier: GPL-3.0-or-later
002
003package es.uvigo.esei.sing.textproc.step;
004
005import java.util.ServiceLoader;
006
007/**
008 * Contains common functionality for available processing step services.
009 *
010 * @author Alejandro González García
011 * @implNote The implementation of this class is thread-safe.
012 */
013public final class ProcessingStepServices {
014        private static final ThreadLocal<ServiceLoader<ProcessingStepService>> CACHED_SERVICE_LOADERS;
015
016        static {
017                CACHED_SERVICE_LOADERS = new ThreadLocal<>() {
018                        @Override
019                        protected ServiceLoader<ProcessingStepService> initialValue() {
020                                return ServiceLoader.load(ProcessingStepService.class);
021                        }
022                };
023        }
024
025        /**
026         * Returns a service loader for iterating over every available processing step
027         * service. This method is efficient, in the sense that it avoids creating new
028         * service loader objects if it's not necessary.
029         *
030         * @return The described service loader.
031         */
032        public static ServiceLoader<ProcessingStepService> getServiceLoader() {
033                return CACHED_SERVICE_LOADERS.get();
034        }
035}