All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mapstruct.ap.internal.util.Services Maven / Gradle / Ivy

There is a newer version: 1.6.3
Show newest version
/*
 * Copyright MapStruct Authors.
 *
 * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */
package org.mapstruct.ap.internal.util;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * A simple locator for SPI implementations.
 *
 * @author Christian Schuster
 */
public class Services {

    private Services() {
    }

    public static  Iterable all(Class serviceType) {
        return ServiceLoader.load( serviceType, Services.class.getClassLoader() );
    }

    public static  T get(Class serviceType, T defaultValue) {

        Iterator services = ServiceLoader.load( serviceType, Services.class.getClassLoader() ).iterator();

        T result;
        if ( services.hasNext() ) {
            result = services.next();
        }
        else {
            result = defaultValue;
        }
        if ( services.hasNext() ) {
           throw new IllegalStateException(
               "Multiple implementations have been found for the service provider interface" );
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy