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

org.carrot2.language.LanguageComponents Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2020, Dawid Weiss, Stanisław Osiński.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * https://www.carrot2.org/carrot2.LICENSE
 */
package org.carrot2.language;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

/** A set of language-specific components. */
public final class LanguageComponents {
  private final String language;
  private final Map, Supplier> components;

  public LanguageComponents(String language, Map, Supplier> suppliers) {
    this.language = language;
    this.components = suppliers;
  }

  public String language() {
    return language;
  }

  public  T get(Class componentClass) {
    Supplier supplier = components.get(componentClass);
    if (supplier == null) {
      throw new RuntimeException(
          String.format(
              Locale.ROOT,
              "This instance of LanguageComponents for language '%s' does not come with a supplier of component class '%s'.",
              language,
              componentClass.getName()));
    }
    return componentClass.cast(supplier.get());
  }

  public  LanguageComponents override(Class clazz, Supplier supplier) {
    Map, Supplier> clonedSuppliers = new LinkedHashMap<>(components);
    clonedSuppliers.put(clazz, supplier);
    return new LanguageComponents(language, clonedSuppliers);
  }

  public Set> components() {
    return components.keySet();
  }

  public static LanguageComponentsLoader loader() {
    return new LanguageComponentsLoader();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy