/*
 * Copyright 2016-2020 Chronicle Software
 *
 * https://chronicle.software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.openhft.chronicle.bytes.ref;

import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.BytesStore;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Closeable;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;

@SuppressWarnings("rawtypes")
public abstract class AbstractReference implements Byteable, Closeable {

    @Nullable
    protected BytesStore bytes;
    protected long offset;

    @Override
    public void bytesStore(@NotNull final BytesStore bytes, final long offset, final long length) throws IllegalStateException, IllegalArgumentException, BufferOverflowException, BufferUnderflowException {
        acceptNewBytesStore(bytes);
        this.offset = offset;
    }

    @Nullable
    @Override
    public BytesStore bytesStore() {
        return bytes;
    }

    @Override
    public long offset() {
        return offset;
    }

    @Override
    public abstract long maxSize();

    protected void acceptNewBytesStore(@NotNull final BytesStore bytes) {
        if (this.bytes != null) {
            this.bytes.release();
        }
        this.bytes = bytes.bytesStore();
        this.bytes.reserve();
    }

    @Override
    public void close() {
        if (this.bytes != null) {
            this.bytes.release();
            this.bytes = null;
        }
    }

    public long address() {
        return bytesStore().addressForRead(offset);
    }
}

@@ -769,16 +769,120 @@
 sStore;%0A
+import net.openhft.chronicle.core.io.AbstractCloseable;%0Aimport net.openhft.chronicle.core.io.Closeable;%0A
 import o
@@ -915,16 +915,16 @@
 otNull;%0A
-
 import o
@@ -963,34 +963,8 @@
 e;%0A%0A
-import java.io.Closeable;%0A
 impo
@@ -1073,16 +1073,16 @@
 types%22)%0A
-
 public a
@@ -1113,16 +1113,42 @@
 ference 
+extends AbstractCloseable 
 implemen
@@ -2009,20 +2009,30 @@
    p
-ublic void c
+rotected void performC
 lose
@@ -2237,11 +2237,134 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void finalize() throws Throwable %7B%0A        warnIfNotClosed();%0A        super.finalize();%0A    %7D
 %0A%7D%0A

add AbstractCloseable OpenHFT/Chronicle-Core#116