
package org.ts.ddcs.dataStream;

import lombok.NoArgsConstructor;


import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 数据缓存
 *
 * @author swp
 */
@NoArgsConstructor
public abstract class DataStreamCache {
    private TestState state = TestState.LOOK;
    private byte[] dataBuff;
    private int readSize;
    private Map<String, Object> keyStore = new HashMap<>();

    public byte[] getDataBuff() {
        return this.dataBuff;
    }

    public void setDataBuff(byte[] dataBuff) {
        this.dataBuff = dataBuff;
    }

    public void resetStore() {
        this.keyStore.clear();
    }

    public TestState getState() {
        return this.state;
    }

    public void setState(TestState testState) {
        this.state = testState;
    }

    public int remainDataSize() {
        if (null != this.dataBuff) {
            if (this.readSize == this.dataBuff.length) {
                return 0;
            }
            return this.dataBuff.length - this.readSize;
        }
        return 0;
    }

    public void readIndex(int readSize) {
        this.readSize = readSize;
    }

    public void readSize(int readSize) {
        this.readSize = this.readSize + readSize;
    }

    public byte getByte() throws NullPointerException, IndexOutOfBoundsException {
        return this.dataBuff[this.readSize];
    }

    public byte[] getBytes(int length) throws NullPointerException, IndexOutOfBoundsException {
        byte[] buf = new byte[length];
        System.arraycopy(this.dataBuff, this.readSize, buf, 0, length);
        return buf;
    }

    public byte getByte(int pos) throws NullPointerException, IndexOutOfBoundsException {
        return this.dataBuff[pos];
    }

    public byte[] getBytes(int pos, int length) throws NullPointerException, IndexOutOfBoundsException {
        byte[] buf = new byte[length];
        System.arraycopy(this.dataBuff, pos, buf, 0, length);
        return buf;
    }

    public void addValue(String key, Object value) {
        this.keyStore.put(key, value);
    }

    public Set<String> keys() {
        return this.keyStore.keySet();
    }

    public Object getValue(String key) {
        return this.keyStore.get(key);
    }

    public Object getItemValue(String key) {
//        Object itemTemplateCacheObj = this.keyStore.get(BusinessConstant.SW_DATA_STREAM_CACHE_VALUE);
//        if (null != itemTemplateCacheObj) {
//            if (itemTemplateCacheObj instanceof DataItemTemplateCache) {
//                DataItemTemplateCache itemTemplateCache = (DataItemTemplateCache) itemTemplateCacheObj;
//                return itemTemplateCache.getValue(key);
//            }
//        }
        return null;
    }

    public abstract void setPacketBuff(byte[] buff);

    public abstract byte[] getPacketBuff();

    public abstract void setPacketLen(int len);

    public abstract int getPacketLen();

    public abstract void setBodyBuff(byte[] buff);

    public abstract byte[] getBodyBuff();

    public abstract int getBodyLen();

    public abstract byte[] getDeviceCodeBuff();
}
