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

com.loocme.sys.datastruct.WeekArray Maven / Gradle / Ivy

package com.loocme.sys.datastruct;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;

import com.alibaba.fastjson.JSONArray;

import com.loocme.sys.constance.DateFormatConst;
import com.loocme.sys.exception.WeektypeCastException;
import com.loocme.sys.util.ListUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author liuchi
 */
@Slf4j
public class WeekArray extends Var implements java.io.Serializable
{

    private static final long serialVersionUID = 1L;

    private transient List priList = new ArrayList();

    public WeekArray()
    {
        super();
    }

    public WeekArray(Object value)
    {
        super();
        this.setValue(value);
    }

    @SuppressWarnings("unchecked")
    void setValue(Object value)
    {
        if (null == value)
        {
            this.priList = new ArrayList();
            return;
        }

        if (value instanceof List)
        {
            List tempList = (List) value;
            for (int i = 0; i < tempList.size(); i ++)
            {
                this.setValue(i, tempList.get(i));
            }
        }
        else if (value instanceof Object[])
        {
            Object[] tempArr = (Object[]) value;
            for (int i = 0; i < tempArr.length; i ++)
            {
                this.setValue(i, tempArr[i]);
            }
        }
        else
        {
            try
            {
                throw new WeektypeCastException("C0");
            }
            catch (WeektypeCastException e)
            {
                log.error("类型转换失败. ", e);
                e.printStackTrace();
            }
        }
    }

    public void addValue(Object value)
    {
        this.priList.add(Var.fromObject(value));
    }

    public void setValue(int index, Object value)
    {
        for (int i = this.priList.size(); i <= index; i ++)
        {
            this.priList.add(Var.newUndefined());
        }
        this.priList.set(index, Var.fromObject(value));
    }

    public Var getValue(int index)
    {
        if (this.priList.size() - 1 >= index)
        {
            return this.priList.get(index);
        }
        return Var.newUndefined();
    }

    Var removeValue(int index)
    {
        Var wt = null;
        if (this.priList.size() - 1 >= index)
        {
            wt = this.priList.get(index);
            this.priList.remove(index);
        }
        return wt;
    }

    JSONArray toJson()
    {
        JSONArray jarr = new JSONArray();
        for (int i = 0; i < this.priList.size(); i ++)
        {
            Var value = this.priList.get(i);
            if (value.isBasicType())
            {
                jarr.add(((WeekSimple) value).getValue());
            }
            else if (value.isObject())
            {
                jarr.add(((WeekObject) value).toJson());
            }
            else if (value.isArray())
            {
                jarr.add(((WeekArray) value).toJson());
            }
            else if (value.isNull())
            {
                jarr.add(null);
            }
            else
            {
                jarr.add(value.toString());
            }
        }
        return jarr;
    }

    @Override
    public boolean isObject()
    {
        return false;
    }

    @Override
    public boolean isArray()
    {
        return true;
    }

    @Override
    public boolean isBasicType()
    {
        return false;
    }

    @Override
    public boolean isNull()
    {
        return this.priList.size() == 0;
    }
    
    @Override
    public void foreach(IVarForeachHandler handler)
    {
        if (isNull())
        {
            return;
        }
        
        if (null == handler)
        {
            return;
        }
        
        for (int i = 0; i < this.priList.size(); i++)
        {
            handler.execute(i + "", this.priList.get(i));
        }
    }

    @Override
    public String toString()
    {
        return this.toJson().toString(SerializerFeature.WriteMapNullValue);
    }

    @Override
    public String toXml(boolean simple)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < this.priList.size(); i ++)
        {
            Var value = this.priList.get(i);
            sb.append(value.toXml(simple));
        }
        return sb.toString();
    }

    @Override
    public void clear()
    {
        this.priList = new ArrayList();
    }

    @Override
    public Object getObject()
    {
        List retList = new ArrayList();
        for (int i = 0; i < this.priList.size(); i ++)
        {
            retList.add(this.priList.get(i).getObject());
        }
        return retList;
    }

    @Override
    public void addAll(Var var)
    {
        if (!var.isArray())
        {
            this.priList.add(var);
        }
        WeekArray arr = (WeekArray) var;
        if (ListUtil.isNotNull(arr.priList))
        {
            for (int i = 0; i < arr.priList.size(); i ++)
            {
                this.priList.add(arr.priList.get(i));
            }
        }
    }

    public int getSize()
    {
        return this.priList.size();
    }

    private static final FastJsonConfig JSON_CONFIG = new FastJsonConfig();
    static
    {
        JSON_CONFIG.setDateFormat(DateFormatConst.YMDHMS_);
        JSON_CONFIG.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty);
    }
}