class StatsService {

  @VisibleForTesting
  public List<FeatureNameStatistics> mergeStatistics(
      List<List<FeatureNameStatistics>> featureNameStatistics) {
    List<FeatureNameStatistics> unnestedList = new ArrayList<>();

    featureNameStatistics.forEach(unnestedList::addAll);
    Map<Path, List<FeatureNameStatistics>> groupByPath =
        unnestedList.stream()
            .collect(Collectors.groupingBy(FeatureNameStatistics::getPath, Collectors.toList()));

    List<FeatureNameStatistics> merged = new ArrayList<>();
    for (Path key : groupByPath.keySet()) {
      List<FeatureNameStatistics> featureNameStatisticsForKey = groupByPath.get(key);
      if (featureNameStatisticsForKey.size() == 1) {
        merged.add(featureNameStatisticsForKey.get(0));
      } else {
        switch (featureNameStatisticsForKey.get(0).getType()) {
          case INT:
          case FLOAT:
            merged.add(mergeNumStatistics(featureNameStatisticsForKey));
            break;
          case STRING:
            merged.add(mergeCategoricalStatistics(groupByPath.get(key)));
            break;
          case BYTES:
            merged.add(mergeByteStatistics(groupByPath.get(key)));
            break;
          case STRUCT:
            merged.add(mergeStructStats(groupByPath.get(key)));
            break;
          default:
            throw new IllegalArgumentException(
                "Statistics are only supported for string, boolean, bytes and numeric features");
        }
      }
    }
    return merged;
  }

  @Autowired public  StatsService(
      StoreRepository storeRepository,
      FeatureStatisticsRepository featureStatisticsRepository,
      FeatureSetRepository featureSetRepository);

}