netty笔记-ChannelOption

1. ChannelOption的类图继承关系

netty笔记-ChannelOption_第1张图片

2.Constant

定义了id和Name

public interface Constant> extends Comparable {

    /**
     * Returns the unique number assigned to this {@link Constant}.
     */
    int id();

    /**
     * Returns the name of this {@link Constant}.
     */
    String name();
}

AbstractConstant是Constant的默认抽象类实现

public abstract class AbstractConstant> implements Constant {

    private static final AtomicLong uniqueIdGenerator = new AtomicLong();
    private final int id;
    private final String name;
    private final long uniquifier;

    /**
     * Creates a new instance.
     */
    protected AbstractConstant(int id, String name) {
        this.id = id;
        this.name = name;
        this.uniquifier = uniqueIdGenerator.getAndIncrement();
    }

    @Override
    public final String name() {
        return name;
    }

    @Override
    public final int id() {
        return id;
    }

    @Override
    public final String toString() {
        return name();
    }

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

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

    @Override
    public final int compareTo(T o) {
        if (this == o) {
            return 0;
        }

        @SuppressWarnings("UnnecessaryLocalVariable")
        AbstractConstant other = o;
        int returnCode;

        returnCode = hashCode() - other.hashCode();
        if (returnCode != 0) {
            return returnCode;
        }

        if (uniquifier < other.uniquifier) {
            return -1;
        }
        if (uniquifier > other.uniquifier) {
            return 1;
        }

        throw new Error("failed to compare two different constants");
    }

}

3.ChannelOption

ChannelOption继承了AbstractConstant,但其构造函数是私有的

public class ChannelOption extends AbstractConstant> {

    private ChannelOption(int id, String name) {
        super(id, name);
    }
}

其对外提供了valueOf静态方法

    private static final ConstantPool> pool = new ConstantPool>() {
        @Override
        protected ChannelOption newConstant(int id, String name) {
            return new ChannelOption(id, name);
        }
    };

    /**
     * Returns the {@link ChannelOption} of the specified name.
     */
    @SuppressWarnings("unchecked")
    public static  ChannelOption valueOf(String name) {
        return (ChannelOption) pool.valueOf(name);
    }

    public static final ChannelOption SO_BROADCAST = valueOf("SO_BROADCAST");
    public static final ChannelOption SO_KEEPALIVE = valueOf("SO_KEEPALIVE");
    public static final ChannelOption SO_SNDBUF = valueOf("SO_SNDBUF");
    public static final ChannelOption SO_RCVBUF = valueOf("SO_RCVBUF");
    public static final ChannelOption SO_REUSEADDR = valueOf("SO_REUSEADDR");
    public static final ChannelOption SO_LINGER = valueOf("SO_LINGER");
    public static final ChannelOption SO_BACKLOG = valueOf("SO_BACKLOG");
    public static final ChannelOption SO_TIMEOUT = valueOf("SO_TIMEOUT");
 
 

4.ConstantPool

ConstantPool是对Constant对象的缓存池管理器

public abstract class ConstantPool> {

private final ConcurrentMap constants = PlatformDependent.newConcurrentHashMap();
    /**
     * 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;
    }
}

参考:
https://blog.csdn.net/wzq6578702/article/details/78374596

你可能感兴趣的:(netty笔记-ChannelOption)