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

no.mnemonic.commons.container.providers.SimpleBeanProvider Maven / Gradle / Ivy

package no.mnemonic.commons.container.providers;

import no.mnemonic.commons.utilities.collections.MapUtils;

import java.util.*;
import java.util.stream.Collectors;

public class SimpleBeanProvider implements BeanProvider {

  private final Map components = new HashMap<>();

  public SimpleBeanProvider(Iterable components) {
    if (components == null) throw new IllegalArgumentException("No components provided!");
    components.forEach(c->this.components.put(UUID.randomUUID().toString(), c));
  }

  @Override
  public  Optional getBean(Class ofType) {
    //noinspection unchecked
    return components.values().stream()
        .filter(c->ofType.isAssignableFrom(c.getClass()))
        .map(c->(T)c)
        .findAny();
  }

  @Override
  public  Map getBeans(Class ofType) {
    //noinspection unchecked
    return MapUtils.map(
        components.entrySet().stream()
        .filter(e->ofType.isAssignableFrom(e.getValue().getClass()))
            .map(e -> MapUtils.Pair.T(e.getKey(), (T) e.getValue()))
            .collect(Collectors.toSet())
    );
  }

  @Override
  public Map getBeans() {
    return Collections.unmodifiableMap(components);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy