Home / Class/ ConstantPool Class — netty Architecture

ConstantPool Class — netty Architecture

Architecture documentation for the ConstantPool class in ConstantPool.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  dd817d0d_ff5b_9f57_8655_9c034cfca957["ConstantPool"]
  9ad7e534_05cd_29c3_8762_54b6275ed7e0["ConstantPool.java"]
  dd817d0d_ff5b_9f57_8655_9c034cfca957 -->|defined in| 9ad7e534_05cd_29c3_8762_54b6275ed7e0
  d03f97fe_ccbb_0c5b_4b0c_013193e0378b["T()"]
  dd817d0d_ff5b_9f57_8655_9c034cfca957 -->|method| d03f97fe_ccbb_0c5b_4b0c_013193e0378b
  43e01f00_9afe_4635_8f23_09857224c541["exists()"]
  dd817d0d_ff5b_9f57_8655_9c034cfca957 -->|method| 43e01f00_9afe_4635_8f23_09857224c541
  8459a9b4_08b9_02b1_ced1_9114116e589a["nextId()"]
  dd817d0d_ff5b_9f57_8655_9c034cfca957 -->|method| 8459a9b4_08b9_02b1_ced1_9114116e589a

Relationship Graph

Source Code

common/src/main/java/io/netty/util/ConstantPool.java lines 31–116

public abstract class ConstantPool<T extends Constant<T>> {

    private final ConcurrentMap<String, T> constants = new ConcurrentHashMap<>();

    private final AtomicInteger nextId = new AtomicInteger(1);

    /**
     * Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
     */
    public T valueOf(Class<?> firstNameComponent, String secondNameComponent) {
        return valueOf(
                checkNotNull(firstNameComponent, "firstNameComponent").getName() +
                '#' +
                checkNotNull(secondNameComponent, "secondNameComponent"));
    }

    /**
     * Returns the {@link Constant} which is assigned to the specified {@code name}.
     * If there's no such {@link Constant}, a new one will be created and returned.
     * Once created, the subsequent calls with the same {@code name} will always return the previously created one
     * (i.e. singleton.)
     *
     * @param name the name of the {@link Constant}
     */
    public T valueOf(String name) {
        return getOrCreate(checkNonEmpty(name, "name"));
    }

    /**
     * Get existing constant by name or creates new one if not exists. Threadsafe
     *
     * @param name the name of the {@link Constant}
     */
    private T getOrCreate(String name) {
        T constant = constants.get(name);
        if (constant == null) {
            final T tempConstant = newConstant(nextId(), name);
            constant = constants.putIfAbsent(name, tempConstant);
            if (constant == null) {
                return tempConstant;
            }
        }

        return constant;
    }

    /**
     * Returns {@code true} if a {@link AttributeKey} exists for the given {@code name}.
     */
    public boolean exists(String name) {
        return constants.containsKey(checkNonEmpty(name, "name"));
    }

    /**
     * Creates a new {@link Constant} for the given {@code name} or fail with an
     * {@link IllegalArgumentException} if a {@link Constant} for the given {@code name} exists.
     */
    public T newInstance(String name) {
        return createOrThrow(checkNonEmpty(name, "name"));
    }

    /**
     * Creates constant by name or throws exception. Threadsafe
     *
     * @param name the name of the {@link Constant}
     */
    private T createOrThrow(String name) {
        T constant = constants.get(name);
        if (constant == null) {
            final T tempConstant = newConstant(nextId(), name);
            constant = constants.putIfAbsent(name, tempConstant);
            if (constant == null) {
                return tempConstant;
            }
        }

        throw new IllegalArgumentException(String.format("'%s' is already in use", name));
    }

    protected abstract T newConstant(int id, String name);

Frequently Asked Questions

What is the ConstantPool class?
ConstantPool is a class in the netty codebase, defined in common/src/main/java/io/netty/util/ConstantPool.java.
Where is ConstantPool defined?
ConstantPool is defined in common/src/main/java/io/netty/util/ConstantPool.java at line 31.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free