1. ChannelOption的类图继承关系
2.Constant
定义了id和Name
public interface Constant<T extends Constant<T>> extends Comparable<T> {
/**
* 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<T extends AbstractConstant<T>> implements Constant<T> {
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<T> 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<T> extends AbstractConstant<ChannelOption<T>> {
private ChannelOption(int id, String name) {
super(id, name);
}
}
其对外提供了valueOf静态方法
private static final ConstantPool<ChannelOption<Object>> pool = new ConstantPool<ChannelOption<Object>>() {
@Override
protected ChannelOption<Object> newConstant(int id, String name) {
return new ChannelOption<Object>(id, name);
}
};
/**
* Returns the {@link ChannelOption} of the specified name.
*/
@SuppressWarnings("unchecked")
public static <T> ChannelOption<T> valueOf(String name) {
return (ChannelOption<T>) pool.valueOf(name);
}
public static final ChannelOption<Boolean> SO_BROADCAST = valueOf("SO_BROADCAST");
public static final ChannelOption<Boolean> SO_KEEPALIVE = valueOf("SO_KEEPALIVE");
public static final ChannelOption<Integer> SO_SNDBUF = valueOf("SO_SNDBUF");
public static final ChannelOption<Integer> SO_RCVBUF = valueOf("SO_RCVBUF");
public static final ChannelOption<Boolean> SO_REUSEADDR = valueOf("SO_REUSEADDR");
public static final ChannelOption<Integer> SO_LINGER = valueOf("SO_LINGER");
public static final ChannelOption<Integer> SO_BACKLOG = valueOf("SO_BACKLOG");
public static final ChannelOption<Integer> SO_TIMEOUT = valueOf("SO_TIMEOUT");
4.ConstantPool
ConstantPool是对Constant对象的缓存池管理器
public abstract class ConstantPool<T extends Constant<T>> {
private final ConcurrentMap<String, T> 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