Netty源码分析-AttributeKey

 


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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值