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

org.rx.core.Disposable Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.core;

import lombok.SneakyThrows;
import org.rx.annotation.ErrorCode;
import org.rx.exception.ApplicationException;

import static org.rx.core.Extends.values;

public abstract class Disposable implements AutoCloseable {
    private volatile boolean closed;

    public boolean isClosed() {
        return closed;
    }

    protected abstract void freeObjects() throws Throwable;

    @ErrorCode
    protected void checkNotClosed() {
        if (closed) {
            throw new ApplicationException(values(this.getClass().getSimpleName()));
        }
    }

    @SneakyThrows
    @Override
    public synchronized void close() {
        if (closed) {
            return;
        }
        //todo fields may be null
        freeObjects();
        closed = true;
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy