public void removeEach()

in protonj2/src/main/java/org/apache/qpid/protonj2/engine/util/UnsettledMap.java [300:343]


    public void removeEach(int first, int last, Consumer<Delivery> action) {
        Objects.requireNonNull(action);

        if (totalEntries == 0) {
            return;
        }

        boolean foundFirst = false;
        boolean foundLast = false;
        int removeStart = 0;
        int removeEnd = 0;

        for (int i = 0; i <= current && !foundLast; ++i) {
            if (!foundFirst && !buckets[i].isInRange(first)) {
                continue;
            }

            final UnsettledBucket<Delivery> bucket = buckets[i];

            for (int j = removeStart = removeEnd = bucket.readOffset; j < bucket.writeOffset && !foundLast; ) {
                final Delivery delivery = bucket.entryAt(j);
                final int deliveryId = deliveryIdSupplier.getDeliveryId(delivery);

                foundFirst = foundFirst || deliveryId == first;
                foundLast = deliveryId == last;

                if (foundFirst) {
                    action.accept(delivery);
                    removeEnd = ++j;
                } else {
                    removeStart = removeEnd = ++j;
                }
            }

            // We found first so this iteration did clear some elements from the current bucket
            // and we need to check that this removal cleared a full bucket which means the index
            // needs to shift back one since we will have recycled the empty bucket. When the last
            // index is found we should also attempt to compact the bucket with the previous one
            // to reduce fragmentation.
            if (foundFirst) {
                i = removeRange(i, removeStart, removeEnd, foundLast) ? --i : i;
            }
        }
    }