All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.permazen.util.CloseableIteratorWrapper Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package io.permazen.util;

import com.google.common.collect.ForwardingIterator;

import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

class CloseableIteratorWrapper extends ForwardingIterator implements CloseableIterator {

    private final Iterator iterator;
    private final AutoCloseable resource;
    private final AtomicBoolean closed = new AtomicBoolean();

    CloseableIteratorWrapper(Iterator iterator, AutoCloseable resource) {
        this.iterator = iterator;
        this.resource = resource;
    }

    @Override
    protected Iterator delegate() {
        return this.iterator;
    }

    @Override
    public void close() {
        if (this.closed.compareAndSet(false, true) && resource != null) {
            try {
                this.resource.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy