package commons.collections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import commons.collections.interfaces.Sized;
import basics.utl.SUtl;
/**
 * Stores words uniquely. Each word has a number (index position). Get the number through a
 * hashtable.
 * @author ks
 */
public class HashList implements Sized
{

private List<String>         list = new ArrayList<String>();
private Map<String, Integer> ndx  = new HashMap<String, Integer>();

public synchronized int getOrAdd (String t)
{
   int pos = -1;
   if (!ndx.containsKey(t))
   {
      pos = list.size();
      list.add(t);
      ndx.put(t, pos);
   }
   else pos = ndx.get(t);
   return pos;
}

public int size ()
{
   return list.size();
}

public long signs ()
{
   long signs = 0;
   for (String s : list)
      signs += s.length();
   return signs;
}

public int getPosition (String t)
{
   return !ndx.containsKey(t) ? -1 : ndx.get(t);
}

public String getTerm (int pos)
{
   return list.get(pos);
}

public boolean hasTerm (String term)
{
   return list.contains(term);
}

public List<String> getList ()
{
   return list;
}

public void setList (List<String> list)
{
   this.list = list;
}

public Map<String, Integer> getNdx ()
{
   return ndx;
}

public void setNdx (Map<String, Integer> ndx)
{
   this.ndx = ndx;
}

public String toString ()
{
   return SUtl.join(list, ", ");
}
}
