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

com.github.javaclub.toolbox.model.AttributesDO Maven / Gradle / Ivy

/*
 * @(#)AttributesDO.java	 2022-12-15
 *
 * Copyright (c) 2022 - 2099. All Rights Reserved.
 *
 */

package com.github.javaclub.toolbox.model;

import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
import com.github.javaclub.toolbox.ToolBox.Strings;

/**
 * AttributesDO
 *
 * @author Gerald Chen
 * @version $Id: AttributesDO.java 2022-12-15 14:18:57 Exp $
 */
public class AttributesDO implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * json格式的扩展字段内容,层级不要过多。字段名为:attributes
	 */
	private Map attributesMap = new TreeMap<>();
	
	public Map attributesMap() {
		return attributesMap;
	}

	public Object attr(String key) {
		if (null != attributesMap) {
			return attributesMap.get(key);
		}
		return null;
	}

	/**
	 * 重置清空 attributesMap
	 */
	public int clearAttributesMap() {
		if (null != attributesMap) {
			int count = attributesMap.size();
			attributesMap = null;
			return count;
		}
		return 0;
	}

	public String getAttributes() {
		return JSONObject.toJSONString(attributesMap);
	}

	public void setAttributes(String attributes) {
		if (Strings.isNotBlank(attributes)) {
			this.attributesMap = toJsonMap(attributes);
		}
	}
	
	public void addAttribute(String key, Object value) {
		if (null == attributesMap) {
			attributesMap = new TreeMap<>();
		}
		attributesMap.put(key, value);
	}

	public void removeAttribute(String key) {
		if (null == attributesMap) {
			attributesMap = new TreeMap<>();
		}
		attributesMap.remove(key);
	}

	public void putAll(Map params) {
		if (null == attributesMap) {
			attributesMap = new TreeMap<>();
		}
		attributesMap.putAll(params);
	}

	Map toJsonMap(String text) {
		if (Strings.isBlank(text)) {
			return new TreeMap();
		}
		Map map = (Map) JSONObject.parseObject(text, Map.class);
		return null == map ? new TreeMap() : new TreeMap(map);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy