//Constant接口,继承了Comparable
public interface Constant<T extends Constant<T>> extends Comparable<T> {

//ID
int id();

//名字
String name();
}


//一个池对象,内部只能存放Constant类型
public abstract class ConstantPool<T extends Constant<T>> {

//线程安全的MAP
private final ConcurrentMap<String, T> constants = PlatformDependent.newConcurrentHashMap();

//线程安全原子自增计数器
private final AtomicInteger nextId = new AtomicInteger(1);


public T valueOf(Class<?> firstNameComponent, String secondNameComponent) {
if (firstNameComponent == null) {
throw new NullPointerException("firstNameComponent");
}
if (secondNameComponent == null) {
throw new NullPointerException("secondNameComponent");
}

//类名#secondNameComponent
return valueOf(firstNameComponent.getName() + '#' + secondNameComponent);
}


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


private T getOrCreate(String name) {
//根据name在map中查询
T constant = constants.get(name);
//如果为空
if (constant == null) {
//则调用抽象方法传入自增ID和名称创建对象
final T tempConstant = newConstant(nextId(), name);
//把创建好的对象放入map中缓存,以name作为key
constant = constants.putIfAbsent(name, tempConstant);

//这里说明name做key 之前并没有数据
if (constant == null) {
//返回新创建的对象
return tempConstant;
}
}

//返回旧数据
return constant;
}

//判断name当key是否在map当中
public boolean exists(String name) {
checkNotNullAndNotEmpty(name);
return constants.containsKey(name);
}


//创建一个对象,如果之前name在map当中存在旧值则抛出异常
public T newInstance(String name) {
checkNotNullAndNotEmpty(name);
return createOrThrow(name);
}


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;
}
}

//逻辑与getOrCreate类似,区别就是当map当中以name为key存在旧值则直接抛异常
throw new IllegalArgumentException(String.format("'%s' is already in use", name));
}

private static String checkNotNullAndNotEmpty(String name) {
ObjectUtil.checkNotNull(name, "name");

if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}

return name;
}

//抽象方法子类实现
protected abstract T newConstant(int id, String name);

//自增计数器
@Deprecated
public final int nextId() {
return nextId.getAndIncrement();
}
}


package io.netty.util;


@SuppressWarnings("UnusedDeclaration") // 'T' is used only at compile time
public final class AttributeKey<T> extends AbstractConstant<AttributeKey<T>> {

//池对象,重写了newConstant方法,创建AttributeKey对象
private static final ConstantPool<AttributeKey<Object>> pool = new ConstantPool<AttributeKey<Object>>() {
@Override
protected AttributeKey<Object> newConstant(int id, String name) {
return new AttributeKey<Object>(id, name);
}
};


//根据name从池中获取AttributeKey对象,相同name返回相同的AttributeKey
@SuppressWarnings("unchecked")
public static <T> AttributeKey<T> valueOf(String name) {
return (AttributeKey<T>) pool.valueOf(name);
}

//根据name从池中判断name关联的对象是否存在
public static boolean exists(String name) {
return pool.exists(name);
}

//根据name创建新对象,如果name关联旧对象直接抛异常
@SuppressWarnings("unchecked")
public static <T> AttributeKey<T> newInstance(String name) {
return (AttributeKey<T>) pool.newInstance(name);
}

//firstNameComponent.getName()+#+secondNameComponent
@SuppressWarnings("unchecked")
public static <T> AttributeKey<T> valueOf(Class<?> firstNameComponent, String secondNameComponent) {
return (AttributeKey<T>) pool.valueOf(firstNameComponent, secondNameComponent);
}

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