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

tech.tablesaw.io.ReaderRegistry Maven / Gradle / Ivy

The newest version!
package tech.tablesaw.io;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class ReaderRegistry {

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

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

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

  public void registerOptions(Class optionsType, DataReader reader) {
    optionTypesRegistry.put(optionsType.getCanonicalName(), reader);
  }

  public void registerExtension(String extension, DataReader reader) {
    extensionsRegistry.put(extension, reader);
  }

  public void registerMimeType(String mimeType, DataReader reader) {
    mimeTypesRegistry.put(mimeType, reader);
  }

  @SuppressWarnings("unchecked")
  public  DataReader getReaderForOptions(T options) {
    String clazz = options.getClass().getCanonicalName();
    DataReader reader = (DataReader) optionTypesRegistry.get(clazz);
    if (reader == null) {
      throw new IllegalArgumentException("No reader registered for class " + clazz);
    }
    return reader;
  }

  public Optional> getReaderForExtension(String extension) {
    return Optional.ofNullable(extensionsRegistry.get(extension));
  }

  public Optional> getReaderForMimeType(String mimeType) {
    return Optional.ofNullable(mimeTypesRegistry.get(mimeType));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy