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

com.github.zhengframework.configuration.parser.FileConfigurationParserSelector Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.github.zhengframework.configuration.parser;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.ArrayUtils;

public class FileConfigurationParserSelector implements FileConfigurationParser {

  private final List parsers;
  private final String[] supportFileTypes;

  public FileConfigurationParserSelector(
      List parsers) {
    this.parsers = Objects.requireNonNull(parsers);

    supportFileTypes = parsers.stream().map(FileConfigurationParser::supportFileTypes)
        .flatMap(Arrays::stream)
        .distinct().toArray(String[]::new);
  }

  @Override
  public String[] supportFileTypes() {
    return supportFileTypes;
  }

  @Override
  public Map parse(String fileName, InputStream inputStream) {
    String s = Objects.requireNonNull(fileName);
    String type = s.substring(s.lastIndexOf("."));
    Optional first = parsers.stream()
        .filter(p -> ArrayUtils.contains(p.supportFileTypes(), type))
        .findFirst();
    if (first.isPresent()) {
      return first.get().parse(fileName, inputStream);
    }
    throw new IllegalStateException("not find any FileConfigurationParser for type: " + type);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy