package io.netty.util;

import io.netty.util.Constant;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import java.util.Objects;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes.dex */
public abstract class ConstantPool<T extends Constant<T>> {
    private final ConcurrentMap<String, T> constants = PlatformDependent.newConcurrentHashMap();
    private final AtomicInteger nextId = new AtomicInteger(1);

    private static String checkNotNullAndNotEmpty(String str) {
        ObjectUtil.checkNotNull(str, "name");
        if (str.isEmpty()) {
            throw new IllegalArgumentException("empty name");
        }
        return str;
    }

    private T createOrThrow(String str) {
        if (this.constants.get(str) == null) {
            T t2 = (T) newConstant(nextId(), str);
            if (this.constants.putIfAbsent(str, t2) == null) {
                return t2;
            }
        }
        throw new IllegalArgumentException(String.format("'%s' is already in use", str));
    }

    private T getOrCreate(String str) {
        T t2 = this.constants.get(str);
        if (t2 != null) {
            return t2;
        }
        T t3 = (T) newConstant(nextId(), str);
        T tPutIfAbsent = this.constants.putIfAbsent(str, t3);
        return tPutIfAbsent == null ? t3 : tPutIfAbsent;
    }

    public boolean exists(String str) {
        checkNotNullAndNotEmpty(str);
        return this.constants.containsKey(str);
    }

    public abstract T newConstant(int i2, String str);

    public T newInstance(String str) {
        checkNotNullAndNotEmpty(str);
        return (T) createOrThrow(str);
    }

    @Deprecated
    public final int nextId() {
        return this.nextId.getAndIncrement();
    }

    public T valueOf(Class<?> cls, String str) {
        Objects.requireNonNull(cls, "firstNameComponent");
        Objects.requireNonNull(str, "secondNameComponent");
        return (T) valueOf(cls.getName() + '#' + str);
    }

    public T valueOf(String str) {
        checkNotNullAndNotEmpty(str);
        return (T) getOrCreate(str);
    }
}
