io.permazen.util.CloseableIteratorWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of permazen-util Show documentation
Show all versions of permazen-util Show documentation
Common utility classes used by Permazen.
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