org.mapstruct.ap.internal.util.Services Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapstruct-processor Show documentation
Show all versions of mapstruct-processor Show documentation
An annotation processor for generating type-safe bean mappers
/*
* 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;
}
}