package androidx.media3.common.util;

import java.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class TimedValueQueue<V> {
    private static final int INITIAL_BUFFER_SIZE = 10;
    private int first;
    private int size;
    private long[] timestamps;
    private V[] values;

    public TimedValueQueue() {
        this(10);
    }

    private void addUnchecked(long j4, V v3) {
        int i6 = this.first;
        int i7 = this.size;
        V[] vArr = this.values;
        int length = (i6 + i7) % vArr.length;
        this.timestamps[length] = j4;
        vArr[length] = v3;
        this.size = i7 + 1;
    }

    private void clearBufferOnTimeDiscontinuity(long j4) {
        if (this.size > 0) {
            if (j4 <= this.timestamps[((this.first + r0) - 1) % this.values.length]) {
                clear();
            }
        }
    }

    private void doubleCapacityIfFull() {
        int length = this.values.length;
        if (this.size < length) {
            return;
        }
        int i6 = length * 2;
        long[] jArr = new long[i6];
        V[] vArr = (V[]) newArray(i6);
        int i7 = this.first;
        int i8 = length - i7;
        System.arraycopy(this.timestamps, i7, jArr, 0, i8);
        System.arraycopy(this.values, this.first, vArr, 0, i8);
        int i9 = this.first;
        if (i9 > 0) {
            System.arraycopy(this.timestamps, 0, jArr, i8, i9);
            System.arraycopy(this.values, 0, vArr, i8, this.first);
        }
        this.timestamps = jArr;
        this.values = vArr;
        this.first = 0;
    }

    private static <V> V[] newArray(int i6) {
        return (V[]) new Object[i6];
    }

    private V popFirst() {
        Assertions.checkState(this.size > 0);
        V[] vArr = this.values;
        int i6 = this.first;
        V v3 = vArr[i6];
        vArr[i6] = null;
        this.first = (i6 + 1) % vArr.length;
        this.size--;
        return v3;
    }

    public synchronized void add(long j4, V v3) {
        clearBufferOnTimeDiscontinuity(j4);
        doubleCapacityIfFull();
        addUnchecked(j4, v3);
    }

    public synchronized void clear() {
        this.first = 0;
        this.size = 0;
        Arrays.fill(this.values, (Object) null);
    }

    public synchronized V poll(long j4) {
        return poll(j4, false);
    }

    public synchronized V pollFirst() {
        return this.size == 0 ? null : popFirst();
    }

    public synchronized V pollFloor(long j4) {
        return poll(j4, true);
    }

    public synchronized int size() {
        return this.size;
    }

    public TimedValueQueue(int i6) {
        this.timestamps = new long[i6];
        this.values = (V[]) newArray(i6);
    }

    private V poll(long j4, boolean z6) {
        V vPopFirst = null;
        long j6 = Long.MAX_VALUE;
        while (this.size > 0) {
            long j7 = j4 - this.timestamps[this.first];
            if (j7 < 0 && (z6 || (-j7) >= j6)) {
                break;
            }
            vPopFirst = popFirst();
            j6 = j7;
        }
        return vPopFirst;
    }
}
