com.loocme.sys.datastruct.WeekObject Maven / Gradle / Ivy
package com.loocme.sys.datastruct;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.JSONObject;
import com.loocme.sys.constance.DateFormatConst;
import com.loocme.sys.exception.WeektypeCastException;
import com.loocme.sys.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
/**
* @author liuchi
*/
@Slf4j
public class WeekObject extends Var implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
private transient Map priMap = new HashMap(16);
public WeekObject()
{
super();
}
public WeekObject(Object value)
{
super();
this.setValue(value);
}
@SuppressWarnings("unchecked")
void setValue(Object value)
{
if (null == value)
{
this.priMap = new HashMap(16);
return;
}
if (!(value instanceof Map))
{
try
{
throw new WeektypeCastException("C0");
}
catch (WeektypeCastException e)
{
log.error("类型转换失败. ", e);
e.printStackTrace();
}
return;
}
Map tempMap = (Map) value;
Iterator tempMapKeyIt = tempMap.keySet().iterator();
while (tempMapKeyIt.hasNext())
{
String key = tempMapKeyIt.next();
this.priMap.put(key, Var.fromObject(tempMap.get(key)));
}
}
public void setValue(String key, Object value)
{
this.priMap.put(key, Var.fromObject(value));
}
Var getValue(String key)
{
if (this.priMap.containsKey(key))
{
return this.priMap.get(key);
}
return null;
}
Var removeValue(String key)
{
Var wt = this.priMap.get(key);
if (null != wt)
{
this.priMap.remove(key);
}
return wt;
}
@Override
public boolean isObject()
{
return true;
}
@Override
public boolean isArray()
{
return false;
}
@Override
public boolean isBasicType()
{
return false;
}
@Override
public boolean isNull()
{
return null == this.priMap || this.priMap.isEmpty();
}
@Override
public void foreach(IVarForeachHandler handler)
{
if (this.isNull())
{
return;
}
if (null == handler)
{
return;
}
Iterator keyIt = this.priMap.keySet().iterator();
while (keyIt.hasNext())
{
String key = keyIt.next();
handler.execute(key, this.priMap.get(key));
}
}
@Override
public void addAll(Var var)
{
if (var.isObject())
{
WeekObject obj = (WeekObject) var;
this.priMap.putAll(obj.priMap);
}
}
JSONObject toJson()
{
JSONObject jobj = new JSONObject();
Iterator tempMapKeyIt = this.priMap.keySet().iterator();
while (tempMapKeyIt.hasNext())
{
String key = tempMapKeyIt.next();
Var value = this.priMap.get(key);
if (value.isBasicType())
{
jobj.put(key, ((WeekSimple) value).getValue());
}
else if (value.isObject())
{
jobj.put(key, ((WeekObject) value).toJson());
}
else if (value.isArray())
{
jobj.put(key, ((WeekArray) value).toJson());
}
else if (value.isNull())
{
jobj.put(key, null);
}
else
{
jobj.put(key, value.toString());
}
}
return jobj;
}
@Override
public String toString()
{
return this.toJson().toString(SerializerFeature.WriteMapNullValue);
}
@Override
public String toXml(boolean simple)
{
StringBuilder sb = new StringBuilder();
Iterator tempMapKeyIt = this.priMap.keySet().iterator();
while (tempMapKeyIt.hasNext())
{
String key = tempMapKeyIt.next();
Var value = this.priMap.get(key);
if (value.isNull())
{
StringUtil.appendXmlElement(sb, key, "");
}
else if (value.isBasicType())
{
StringUtil.appendXmlElement(sb, key, ((WeekSimple) value).toXml(simple));
}
else if (value.isObject())
{
StringUtil.appendXmlElement(sb, key, ((WeekObject) value).toXml(simple));
}
else if (value.isArray())
{
StringUtil.appendXmlElement(sb, key, ((WeekArray) value).toXml(simple));
}
}
return sb.toString();
}
@Override
public void clear()
{
this.priMap = new HashMap(16);
}
@Override
public Object getObject()
{
Map retMap = new HashMap(16);
Iterator it = this.priMap.keySet().iterator();
while (it.hasNext())
{
String key = it.next();
retMap.put(key, this.priMap.get(key).getObject());
}
return retMap;
}
private static final FastJsonConfig JSON_CONFIG = new FastJsonConfig();
static
{
JSON_CONFIG.setDateFormat(DateFormatConst.YMDHMS_);
JSON_CONFIG.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy