Repository: apache/kafka
File: clients/src/main/java/org/apache/kafka/common/metrics/stats/SampledStat.java (Java)

Code before:
    public void record(MetricConfig config, double value, long timeMs) {
        Sample sample = current(timeMs);
        if (sample.isComplete(timeMs, config))
            sample = advance(config, timeMs);
        update(sample, config, value, timeMs);
        sample.eventCount += 1;
        sample.lastEventMs = timeMs;
    }

    private Sample advance(MetricConfig config, long timeMs) {
        // keep one extra placeholder for "overlapping sample" (see purgeObsoleteSamples() logic)
        int maxSamples = config.samples() + 1;
        this.current = (this.current + 1) % maxSamples;
        if (this.current >= samples.size()) {
            Sample sample = newSample(timeMs);
            this.samples.add(sample);
            return sample;
        } else {
            Sample sample = current(timeMs);
            sample.reset(timeMs);
            return sample;
        }
    }

    protected Sample newSample(long timeMs) {
        return this.timeWindowMs != -1 ? new Sample(this.initialValue, timeMs) : new Sample(this.initialValue, timeMs, this.timeWindowMs);
    }

    @Override
    public double measure(MetricConfig config, long now) {
        purgeObsoleteSamples(config, now);
        return combine(this.samples, config, now);
    }

    public Sample current(long timeMs) {
        if (samples.isEmpty())
            this.samples.add(newSample(timeMs));
        return this.samples.get(this.current);
    }

    public Sample oldest(long now) {
        if (samples.isEmpty())
            this.samples.add(newSample(now));
        Sample oldest = this.samples.get(0);
        for (int i = 1; i < this.samples.size(); i++) {
            Sample curr = this.samples.get(i);
            if (curr.startTimeMs < oldest.startTimeMs)
                oldest = curr;
        }
        return oldest;
    }

Review comment (suggestion):
Sorry, I mistakenly wrote the other way in the commit 
```suggestion
        return this.timeWindowMs > 0 ? new Sample(this.initialValue, timeMs, this.timeWindowMs) : new Sample(this.initialValue, timeMs);
```

Code after:
    public void record(MetricConfig config, double value, long timeMs) {
        Sample sample = current(timeMs);
        if (sample.isComplete(timeMs, config))
            sample = advance(config, timeMs);
        update(sample, config, value, timeMs);
        sample.eventCount += 1;
        sample.lastEventMs = timeMs;
    }

    private Sample advance(MetricConfig config, long timeMs) {
        // keep one extra placeholder for "overlapping sample" (see purgeObsoleteSamples() logic)
        int maxSamples = config.samples() + 1;
        this.current = (this.current + 1) % maxSamples;
        if (this.current >= samples.size()) {
            Sample sample = newSample(timeMs);
            this.samples.add(sample);
            return sample;
        } else {
            Sample sample = current(timeMs);
            sample.reset(timeMs);
            return sample;
        }
    }

    protected Sample newSample(long timeMs) {
        return this.timeWindowMs > 0 ? new Sample(this.initialValue, timeMs, this.timeWindowMs) : new Sample(this.initialValue, timeMs);
    }

    @Override
    public double measure(MetricConfig config, long now) {
        purgeObsoleteSamples(config, now);
        return combine(this.samples, config, now);
    }

    public Sample current(long timeMs) {
        if (samples.isEmpty())
            this.samples.add(newSample(timeMs));
        return this.samples.get(this.current);
    }

    public Sample oldest(long now) {
        if (samples.isEmpty())
            this.samples.add(newSample(now));
        Sample oldest = this.samples.get(0);
        for (int i = 1; i < this.samples.size(); i++) {
            Sample curr = this.samples.get(i);
            if (curr.startTimeMs < oldest.startTimeMs)
                oldest = curr;
        }
        return oldest;
    }