class MOfNAggregationFilter implements DetectionFilter {

    @Override
    public void doFilter(@NonNull DetectorRequest detectorRequest,
                         @NonNull DetectorResponse detectorResponse,
                         @NonNull DetectionFilterChain chain) {
        chain.doFilter(detectorRequest, detectorResponse);
        DetectorResult result = detectorResponse.getDetectorResult();
        if (result instanceof OutlierDetectorResult) {
            buffer[bufferIndex++] = result.getAnomalyLevel();
            if (bufferIndex >= getN()) {
                bufferIndex = 0;
            }
            val outlierDetectorResult = (OutlierDetectorResult) result;
            val aggregatedResult = new OutlierDetectorResult()
                    .setAnomalyLevel(result.getAnomalyLevel())
                    .setPredicted(outlierDetectorResult.getPredicted())
                    .setThresholds(outlierDetectorResult.getThresholds())
                    .setTrusted(result.isTrusted());

            if (numAnomalies() >= getM()) {
                aggregatedResult.setAnomalyLevel(AnomalyLevel.STRONG);
            }
            detectorResponse.setDetectorResult(aggregatedResult);
        } else {
            log.warn(String.format("%s can only be used with %s types of DetectorResults. Skipping aggregation.",
                    this.getClass().getSimpleName(),
                    OutlierDetectorResult.class.getSimpleName()));
            chain.doFilter(detectorRequest, detectorResponse);
        }
    }

    @JsonCreator public  MOfNAggregationFilter(@JsonProperty("m") int m, @JsonProperty("n") int n);

}