class DefaultLineChartReducer implements LineChartReducer {

  @Override
  public List<SeriesData<Long>> reduceData(@NotNull List<SeriesData<Long>> dataList, @NotNull LineConfig config) {
    if (config.getDataBucketInterval() > 0) {
      // If the line chart is showing bars, we don't want to reduce points - because repeating the
      // same value multiple times in a row should generate new bars!
      // TODO(b/73784793): Remove this code once we refactor a new BarChart class
      return dataList;
    }

    List<SeriesData<Long>> reduced = new ArrayList<>();
    for (SeriesData<Long> data: dataList) {
      while (reduced.size() >= 2) {
        SeriesData<Long> preLast = reduced.get(reduced.size() - 2);
        SeriesData<Long> last = reduced.get(reduced.size() - 1);

        if (preLast.value.equals(last.value) &&
            (config.isStepped() || last.value.equals(data.value))) {
          reduced.remove(reduced.size() - 1);
        } else {
          break;
        }
      }
      reduced.add(data);
    }
    return reduced;
  }

}