All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.featherfly.constant.ConstantPool Maven / Gradle / Ivy

The newest version!

package cn.featherfly.constant;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import cn.featherfly.constant.configuration.ConstantParameter;
import cn.featherfly.constant.description.ConstantClassDescription;

/**
 * 

* 常量处理器. *

* * @author 钟冀 */ public final class ConstantPool { private static ConstantPool constantPool; private final Map, Object> constants = new HashMap<>(); private final Map, ConstantClassDescription> constantDescriptions = new HashMap<>(); /** * 构造方法 * * @param configuration ConstantConfiguration */ ConstantPool() { } /** *

* 获取默认ConstantPool *

* * @return ConstantPool */ public static ConstantPool getDefault() { checkInit(); return constantPool; } /** *

* 初始化默认池. *

*/ static ConstantPool init() { if (constantPool == null) { synchronized (ConstantPool.class) { if (constantPool == null) { constantPool = new ConstantPool(); } } } return constantPool; } /** *

* 获得指定类型的常量对象. *

* * @param 泛型 * @param type 指定类型 * @return 指定类型的常量对象 */ @SuppressWarnings("unchecked") public T getConstant(Class type) { return (T) constants.get(type); } /** *

* 判断是否已经存在指定的常量配置类. *

* * @param type 常量配置类 * @return 是否已经存在指定的常量配置类 */ public boolean hasConstant(Class type) { return constants.containsKey(type); } /** *

* 返回常量对象集合. *

* * @return 常量对象集合 */ public Collection getConstants() { return constants.values(); } /** *

* 获得指定类型的常量描述. *

* * @param type 指定类型 * @return 指定类型的常量描述 */ public ConstantClassDescription getConstantDescription(Class type) { return constantDescriptions.get(type); } /** * 获取ConstantParameter * * @return ConstantParameter */ public ConstantParameter getConstantParameter() { return getConstant(ConstantParameter.class); } /** *

* 返回常量描述集合. *

* * @return 常量描述集合 */ public Collection getConstantDescriptions() { return new ArrayList<>(constantDescriptions.values()); } /** *

* 添加常量对象到池中. *

* * @param constant 常量对象 * @param constantClassDescription 常量描述信息 */ void addConstant(Object constant, ConstantClassDescription constantClassDescription) { if (constant != null) { constants.put(constant.getClass(), constant); constantDescriptions.put(constant.getClass(), constantClassDescription); } } /** *

* 返回是否初始化 *

* * @return 是否初始化 */ public static boolean isInit() { return constantPool != null; } // ******************************************************************** // private method // ******************************************************************** //检查是否初始化 private static void checkInit() { if (!isInit()) { throw new ConstantException("常量池未初始化"); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy