package tools.devnull.boteco.plugins.definition;

import tools.devnull.boteco.BotException;
import tools.devnull.boteco.ServiceRegistry;
import tools.devnull.boteco.plugins.definition.spi.Definition;
import tools.devnull.boteco.plugins.definition.spi.DefinitionProvider;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static tools.devnull.boteco.Predicates.id;

public class Lookup {

  private final ServiceRegistry registry;
  private final String provider;
  private final String term;

  public Lookup(ServiceRegistry registry, String provider, String term) {
    this.registry = registry;
    this.provider = provider;
    this.term = term;
  }

  public Lookup(ServiceRegistry registry, String term) {
    this.registry = registry;
    this.provider = null;
    this.term = term;
  }

  public String term() {
    return term;
  }

  public List<Definition> lookup() {
    List<Definition> result;
    if (provider == null) {
      result = registry.locate(DefinitionProvider.class).all()
          .stream()
          .map(p -> p.lookup(term))
          .filter(Objects::nonNull)
          .collect(Collectors.toList());
    } else {
      DefinitionProvider definitionProvider = registry.locate(DefinitionProvider.class)
          .filter(id(provider))
          .one();
      if (definitionProvider == null) {
        throw new BotException("Provider " + provider + " not found");
      }
      Definition lookup = definitionProvider.lookup(term);
      if (lookup != null) {
        result = Collections.singletonList(lookup);
      } else {
        result = Collections.emptyList();
      }
    }
    return result;
  }

}

package tools.devnull.boteco.plugins.definition;

import tools.devnull.boteco.DomainException;
import tools.devnull.boteco.ServiceRegistry;
import tools.devnull.boteco.plugins.definition.spi.Definition;
import tools.devnull.boteco.plugins.definition.spi.DefinitionProvider;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static tools.devnull.boteco.Predicates.id;

public class Lookup {

  private final ServiceRegistry registry;
  private final String provider;
  private final String term;

  public Lookup(ServiceRegistry registry, String provider, String term) {
    this.registry = registry;
    this.provider = provider;
    this.term = term;
  }

  public Lookup(ServiceRegistry registry, String term) {
    this.registry = registry;
    this.provider = null;
    this.term = term;
  }

  public String term() {
    return term;
  }

  public List<Definition> lookup() {
    List<Definition> result;
    if (provider == null) {
      result = registry.locate(DefinitionProvider.class).all()
          .stream()
          .map(p -> p.lookup(term))
          .filter(Objects::nonNull)
          .collect(Collectors.toList());
    } else {
      DefinitionProvider definitionProvider = registry.locate(DefinitionProvider.class)
          .filter(id(provider))
          .one();
      if (definitionProvider == null) {
        throw new DomainException("Provider " + provider + " not found");
      }
      Definition lookup = definitionProvider.lookup(term);
      if (lookup != null) {
        result = Collections.singletonList(lookup);
      } else {
        result = Collections.emptyList();
      }
    }
    return result;
  }

}

use domain exception