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

com.bluecatcode.common.io.CloseableReference Maven / Gradle / Ivy

The newest version!
package com.bluecatcode.common.io;

import javax.annotation.Nullable;
import java.io.Closeable;
import java.io.IOException;

public class CloseableReference implements Closeable {

    private final T reference;
    private final Closer closer;

    public CloseableReference(@Nullable T reference, Closer closer) {
        this.reference = reference;
        if (closer == null) {
            throw new IllegalArgumentException("Expected closer to be non-null");
        }
        this.closer = closer;
    }

    public T get() {
        return reference;
    }

    @Override
    public void close() throws IOException {
        try {
            if (reference != null) {
                closer.close(reference);
            }
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy