package org.nl.acs.opc;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LENOVO
 */
public abstract class BlockedRunable implements Runnable {
    private String threadName;
    private Date startTime;
    private Map<String, BlockedRunable> index = new HashMap();

    public BlockedRunable() {
    }

    public BlockedRunable(Map<String, BlockedRunable> index) {
        this.index = index;
    }

    /**
     * 获取code
     * @return
     */
    public abstract String getCode();

    /**
     * subRun
     * @throws Exception
     */
    public abstract void subRun() throws Exception;

    @Override
    public void run() {
        try {
            this.setStartTime(new Date());
            this.setThreadName(Thread.currentThread().getName());
            this.subRun();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (this.index.keySet().contains(this.getCode())) {
                this.index.remove(this.getCode());
            }

        }

    }

    public String getThreadName() {
        return this.threadName;
    }

    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }

    public Date getStartTime() {
        return this.startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Map<String, BlockedRunable> getIndex() {
        return this.index;
    }

    public void setIndex(Map<String, BlockedRunable> index) {
        this.index = index;
    }

    @Override
    public int hashCode() {
        int prime = 1;
        int result = 1;
        result = 31 * result + (this.getCode() == null ? 0 : this.getCode().hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (this.getClass() != obj.getClass()) {
            return false;
        } else {
            BlockedRunable other = (BlockedRunable) obj;
            if (this.getCode() == null) {
                if (other.getCode() != null) {
                    return false;
                }
            } else if (!this.getCode().equals(other.getCode())) {
                return false;
            }

            return true;
        }
    }
}

