package io.netty.channel.pool;

import io.netty.channel.pool.ChannelPool;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.ReadOnlyIterator;
import java.io.Closeable;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractChannelPoolMap<K, P extends ChannelPool> implements ChannelPoolMap<K, P>, Iterable<Map.Entry<K, P>>, Closeable {
    private final ConcurrentMap<K, P> map = PlatformDependent.newConcurrentHashMap();

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        Iterator<K> it = this.map.keySet().iterator();
        while (it.hasNext()) {
            remove(it.next());
        }
    }

    @Override // io.netty.channel.pool.ChannelPoolMap
    public final boolean contains(K k2) {
        return this.map.containsKey(ObjectUtil.checkNotNull(k2, "key"));
    }

    @Override // io.netty.channel.pool.ChannelPoolMap
    public final P get(K k2) {
        P p = this.map.get(ObjectUtil.checkNotNull(k2, "key"));
        if (p != null) {
            return p;
        }
        P p2 = (P) newPool(k2);
        P pPutIfAbsent = this.map.putIfAbsent(k2, p2);
        if (pPutIfAbsent == null) {
            return p2;
        }
        p2.close();
        return pPutIfAbsent;
    }

    public final boolean isEmpty() {
        return this.map.isEmpty();
    }

    @Override // java.lang.Iterable
    public final Iterator<Map.Entry<K, P>> iterator() {
        return new ReadOnlyIterator(this.map.entrySet().iterator());
    }

    protected abstract P newPool(K k2);

    public final boolean remove(K k2) {
        P pRemove = this.map.remove(ObjectUtil.checkNotNull(k2, "key"));
        if (pRemove == null) {
            return false;
        }
        pRemove.close();
        return true;
    }

    public final int size() {
        return this.map.size();
    }
}
