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

io.deephaven.engine.table.impl.util.SizedSafeCloseable Maven / Gradle / Ivy

There is a newer version: 0.37.1
Show newest version
/**
 * Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
 */
package io.deephaven.engine.table.impl.util;

import io.deephaven.util.SafeCloseable;

import java.util.function.IntFunction;

public final class SizedSafeCloseable implements SafeCloseable {
    private final IntFunction supplier;
    private T closeable;
    private int capacity;

    public SizedSafeCloseable(IntFunction supplier) {
        this.supplier = supplier;
    }

    public T get() {
        return closeable;
    }

    public T ensureCapacity(int capacity) {
        if (capacity > this.capacity) {
            if (closeable != null) {
                closeable.close();
            }
            closeable = supplier.apply(capacity);
            this.capacity = capacity;
        }
        return closeable;
    }

    @Override
    public void close() {
        if (closeable != null) {
            closeable.close();
            capacity = 0;
            closeable = null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy