eu.maveniverse.maven.mima.runtime.standalonesisu.internal.SisuLookup Maven / Gradle / Ivy
package eu.maveniverse.maven.mima.runtime.standalonesisu.internal;
import com.google.inject.Key;
import com.google.inject.name.Names;
import eu.maveniverse.maven.mima.context.Lookup;
import java.util.Iterator;
import java.util.Optional;
import javax.inject.Named;
import javax.inject.Provider;
import org.eclipse.sisu.BeanEntry;
import org.eclipse.sisu.inject.MutableBeanLocator;
public class SisuLookup implements Lookup {
private final MutableBeanLocator locator;
public SisuLookup(MutableBeanLocator locator) {
this.locator = locator;
}
private Optional lookupInternal(Key key) {
final Iterable extends BeanEntry> entries = locator.locate(key);
final Iterator extends BeanEntry> iterator = entries.iterator();
final Provider provider = iterator.hasNext() ? iterator.next().getProvider() : null;
if (provider == null) {
return Optional.empty();
}
return Optional.of(provider.get());
}
@Override
public Optional lookup(Class type) {
return lookupInternal(Key.get(type, Named.class));
}
@Override
public Optional lookup(Class type, String name) {
return lookupInternal(Key.get(type, Names.named(name)));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy