package edu.neu.ccs.pyramid.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chengli on 12/15/15.
 */
public class Translator<T> {
    private Map<T,Integer> objToIndex;
    private Map<Integer,T> indexToObj;
    private int startIndex;
    private int currentIndex;

    public Translator(int startIndex) {
        this.objToIndex = new HashMap<>();
        this.indexToObj = new HashMap<>();
        this.startIndex = startIndex;
        this.currentIndex = startIndex;
    }

    public Translator() {
        this(0);
    }

    public synchronized void add(T obj){
        if (!objToIndex.containsKey(obj)){
            objToIndex.put(obj,currentIndex);
            indexToObj.put(currentIndex,obj);
            currentIndex ++;
        }
    }

    public synchronized void addAll(Collection<T> all){
        all.forEach(this::add);
    }

    public int getIndex(T obj){
        return objToIndex.get(obj);
    }

    public T getObj(int index){
        return indexToObj.get(index);
    }

    public int getStartIndex() {
        return startIndex;
    }

    public int size(){
        return this.indexToObj.size();
    }


    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Translator{");
        sb.append("indexToObj=").append(indexToObj);
        sb.append('}');
        return sb.toString();
    }
}

@@ -28,16 +28,45 @@
 .util;%0A%0A
+import java.io.Serializable;%0A
 import j
@@ -205,17 +205,93 @@
 ator%3CT%3E 
-%7B
+implements Serializable%7B%0A    private static final long serialVersionUID = 1L;
 %0A    pri

serialize Translator