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

org.apache.bookkeeper.common.collections.RecyclableArrayList Maven / Gradle / Ivy

// Originally copied from netty project, version 4.1.17-Final, heavily modified
/*
 * Copyright 2013 The Netty Project
 *
 * The Netty Project licenses this file to you 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 org.apache.bookkeeper.common.collections;

import io.netty.util.Recycler.Handle;
import java.util.ArrayList;

/**
 * A simple list which is recyclable.
 */
public final class RecyclableArrayList extends ArrayList {

    private static final int DEFAULT_INITIAL_CAPACITY = 8;

    /**
     * An ArrayList recycler.
     */
    public static class Recycler
        extends io.netty.util.Recycler> {
        @Override
        protected RecyclableArrayList newObject(
                Handle> handle) {
            return new RecyclableArrayList(handle, DEFAULT_INITIAL_CAPACITY);
        }

        public RecyclableArrayList newInstance() {
            return get();
        }
    }

    private final Handle> handle;

    /**
     * Default non-pooled instance.
     */
    public RecyclableArrayList() {
        super();
        this.handle = null;
    }

    private RecyclableArrayList(Handle> handle, int initialCapacity) {
        super(initialCapacity);
        this.handle = handle;
    }

    public void recycle() {
        clear();
        if (handle != null) {
            handle.recycle(this);
        }
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy