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

commons.box.app.MetaBean Maven / Gradle / Ivy

The newest version!
package commons.box.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.TypeUtils;
import commons.box.util.MBs;
import commons.box.util.Maps;
import commons.box.util.Types;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

/**
 * 基于配置式的 bean 解析器
 */
public interface MetaBean extends Map, Cloneable, HasKey, DataName, DataLabel, DataObject {
    /**
     * 内部使用的 innerMap 不应直接使用,应通过 asMap 获取对象的属性值
     *
     * @return
     */
    @Nonnull
    public Map innerMap();

    /** 此对象对应的配置 是 Meta Bean Config 的 key 有效的 mb 应该总是返回非空的 config */
    @Nonnull
    public String config();

    /** 获取配置对象 默认实现是每次均通过工厂查找对应配置 下级实现类应当缓存字段值 */
    @Nullable
    default MetaBeanConfig beanConfig() {
        return MBs.config(this.config());
    }

    /** 默认实现 label=key */
    @Nonnull
    @Override
    default String label() {
        return this.key();
    }

    /** 默认实现 name=key */
    @Nonnull
    @Override
    default String name() {
        return this.key();
    }


    /** 转换成 map */
    @Nonnull
    default Map asMap() {
        return Maps.immmap(this.innerMap());
    }

    @Nonnull
    default Map toInfoMap() {
        Map map = new LinkedHashMap<>();
        map.put("$_mb_name", this.name());
        map.put("$_mb_config", this.config());
        map.put("$_mb_key", this.key());
        map.put("$_mb_label", this.label());
        map.put("data", this.innerMap());
        return Maps.immmap(map);
    }

    /**
     * 获取属性值
     *
     * @param key
     * @return
     */
    default Serializable getValue(@Nonnull String key) {
        return this.get(key);
    }

    default boolean has(@Nonnull String key) {
        return this.containsKey(key);
    }

    default Set props() {
        return this.keySet();
    }

    /**
     * 删除对象存储的值
     *
     * @param key
     */
    default void removeValue(String key) {
        if (key != null) this.remove(key);
    }

    /**
     * 设置值
     *
     * @param key
     * @param val
     */
    default void setValue(String key, Serializable val) {
        if (key != null) this.put(key, val);
    }

    /**
     * 设置值 本方法可级联调用
     *
     * @param key
     * @param val
     */
    default MetaBean val(String key, Serializable val) {
        this.setValue(key, val);
        return this;
    }

    /**
     * 由于 meta bean 可能会被 封装,此方法返回实际对应的 metaBean 实例(无包装形式)
     *
     * @return
     */
    default MetaBean raw() {
        return (this instanceof MetaBeanObject) ? ((MetaBeanObject) this).mb() : this;
    }

    /** 按类型返回属性值 */
    default Boolean asBoolean(String key) {
        Object value = this.get(key);
        return (value == null) ? null : Types.castToBoolean(value);
    }

    /** 按类型返回属性值 */
    default  T asEnum(String key, Class type) {
        Object value = this.get(key);
        return (value == null || type == null) ? null : Types.castToEnum(value, type);
    }

    /** 按类型返回属性值 */
    default byte[] asBytes(String key) {
        Object value = this.get(key);
        return (value == null) ? null : Types.castToBytes(value);
    }

    /** 按类型返回属性值 */
    default boolean asBooleanValue(String key) {
        Object value = this.get(key);
        Boolean booleanVal = Types.castToBoolean(value);
        if (booleanVal == null) return false;
        return booleanVal;
    }

    /** 按类型返回属性值 */
    default Byte asByte(String key) {
        Object value = this.get(key);
        if (value == null) return null;
        return Types.castToByte(value);
    }

    /** 按类型返回属性值 */
    default byte asByteValue(String key) {
        Byte byteVal = Types.castToByte(this.get(key));
        if (byteVal == null) return 0;
        return byteVal;
    }

    /** 按类型返回属性值 */
    default Short asShort(String key) {
        Object value = this.get(key);
        return Types.castToShort(value);
    }

    /** 按类型返回属性值 */
    default short asShortValue(String key) {
        Object value = this.get(key);
        Short shortVal = Types.castToShort(value);
        if (shortVal == null) return 0;
        return shortVal;
    }

    /** 按类型返回属性值 */
    default Integer asInteger(String key) {
        Object value = this.get(key);
        return Types.castToInt(value);
    }

    /** 按类型返回属性值 */
    default int asIntValue(String key) {
        Object value = this.get(key);
        Integer intVal = Types.castToInt(value);
        if (intVal == null) return 0;
        return intVal;
    }

    /** 按类型返回属性值 */
    default Long asLong(String key) {
        Object value = this.get(key);
        return Types.castToLong(value);
    }

    /** 按类型返回属性值 */
    default long asLongValue(String key) {
        Object value = this.get(key);

        Long longVal = Types.castToLong(value);
        if (longVal == null) return 0L;
        return longVal;
    }

    /** 按类型返回属性值 */
    default Float asFloat(String key) {
        Object value = this.get(key);
        return Types.castToFloat(value);
    }

    /** 按类型返回属性值 */
    default float asFloatValue(String key) {
        Object value = this.get(key);
        Float floatValue = Types.castToFloat(value);
        if (floatValue == null) return 0F;
        return floatValue;
    }

    /** 按类型返回属性值 */
    default Double asDouble(String key) {
        Object value = this.get(key);
        return Types.castToDouble(value);
    }

    /** 按类型返回属性值 */
    default double asDoubleValue(String key) {
        Object value = this.get(key);

        Double doubleValue = Types.castToDouble(value);
        if (doubleValue == null) return 0D;
        return doubleValue;
    }

    /** 按类型返回属性值 */
    default BigDecimal asBigDecimal(String key) {
        Object value = this.get(key);
        return Types.castToBigDecimal(value);
    }

    /** 按类型返回属性值 */
    default BigInteger asBigInteger(String key) {
        Object value = this.get(key);
        return Types.castToBigInteger(value);
    }

    /** 按类型返回属性值 */
    default String asString(String key) {
        Object value = this.get(key);
        if (value == null) return null;
        return value.toString();
    }

    /** 按类型返回属性值 */
    default Date asDate(String key) {
        Object value = this.get(key);
        return Types.castToDate(value);
    }

    /** 按类型返回属性值 */
    default java.sql.Date asSqlDate(String key) {
        Object value = this.get(key);
        return Types.castToSqlDate(value);
    }

    /** 按类型返回属性值 */
    default java.sql.Timestamp asTimestamp(String key) {
        Object value = this.get(key);
        return Types.castToTimestamp(value);
    }

    /** 按类型返回属性值 */
    default  T asObject(String key, Class clazz) {
        Object obj = this.get(key);
        return TypeUtils.castToJavaBean(obj, clazz);
    }

    /** 按类型返回属性值 */
    default  T asObject(String key, Type type) {
        Object obj = this.get(key);
        return TypeUtils.cast(obj, type, ParserConfig.getGlobalInstance());
    }

    /** 按类型返回属性值 */
    default JSONObject asJSONObject(String key) {
        Object value = this.get(key);
        if (value instanceof JSONObject) return (JSONObject) value;
        else if (value instanceof String) return JSON.parseObject((String) value);
        else return (JSONObject) JSONObject.toJSON(value);
    }

    /** 按类型返回属性值 */
    default JSONArray asJSONArray(String key) {
        Object value = this.get(key);
        if (value instanceof JSONArray) return (JSONArray) value;
        if (value instanceof String) return (JSONArray) JSON.parse((String) value);
        return (JSONArray) JSONObject.toJSON(value);
    }

    //----------------------------------- 代理 map 方法 -----------------------------------
    @Override
    default int size() {
        return this.innerMap().size();
    }

    @Override
    default boolean isEmpty() {
        return this.innerMap().isEmpty();
    }

    @Override
    default boolean containsKey(Object key) {
        return this.innerMap().containsKey(key);
    }

    @Override
    default boolean containsValue(Object value) {
        return this.innerMap().containsValue(value);
    }

    @Override
    default Serializable get(Object key) {
        return this.innerMap().get(key);
    }

    @Override
    default Serializable put(String key, Serializable value) {
        return this.innerMap().put(key, value);
    }

    @Override
    default Serializable remove(Object key) {
        return this.innerMap().remove(key);
    }

    @Override
    default void putAll(Map m) {
        this.innerMap().putAll(m);
    }

    @Override
    default void clear() {
        this.innerMap().clear();
    }

    @Override
    default Set keySet() {
        return this.innerMap().keySet();
    }

    @Override
    default Collection values() {
        return this.innerMap().values();
    }

    @Override
    default Set> entrySet() {
        return this.innerMap().entrySet();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy